Ticket #2598: FilePathFieldEnhancement.diff

File FilePathFieldEnhancement.diff, 2.8 KB (added by scum, 18 years ago)

[patch] FilePathField web_path enhancement

  • db/models/fields/__init__.py

     
    613613        return os.path.normpath(f)
    614614
    615615class FilePathField(Field):
    616     def __init__(self, verbose_name=None, name=None, path='', match=None, recursive=False, **kwargs):
    617         self.path, self.match, self.recursive = path, match, recursive
     616    def __init__(self, verbose_name=None, name=None, path='', match=None, recursive=False, web_path=None, **kwargs):
     617        if not web_path:
     618            web_path = path
     619        self.path, self.match, self.recursive, self.web_path= path, match, recursive, web_path
    618620        Field.__init__(self, verbose_name, name, **kwargs)
    619621
    620622    def get_manipulator_field_objs(self):
    621         return [curry(forms.FilePathField, path=self.path, match=self.match, recursive=self.recursive)]
     623        return [curry(forms.FilePathField, path=self.path, match=self.match, recursive=self.recursive, web_path=self.web_path)]
    622624
    623625class FloatField(Field):
    624626    empty_strings_allowed = False
  • forms/__init__.py

     
    901901
    902902class FilePathField(SelectField):
    903903    "A SelectField whose choices are the files in a given directory."
    904     def __init__(self, field_name, path, match=None, recursive=False, is_required=False, validator_list=None):
     904    def __init__(self, field_name, path, match=None, recursive=False, web_path=None, is_required=False, validator_list=None):
    905905        import os
    906906        from django.db.models import BLANK_CHOICE_DASH
     907        path, web_path = os.path.normpath(path), os.path.normpath(web_path)
    907908        if match is not None:
    908909            import re
    909910            match_re = re.compile(match)
    910911        choices = not is_required and BLANK_CHOICE_DASH[:] or []
    911912        if recursive:
     913            ignore_length = len(path)+1
    912914            for root, dirs, files in os.walk(path):
    913915                for f in files:
    914916                    if match is None or match_re.search(f):
    915                         choices.append((os.path.join(root, f), f))
     917                        choices.append((os.path.join(web_path, root[ignore_length:], f), f))
    916918        else:
    917919            try:
    918920                for f in os.listdir(path):
    919921                    full_file = os.path.join(path, f)
    920922                    if os.path.isfile(full_file) and (match is None or match_re.search(f)):
    921                         choices.append((full_file, f))
     923                        choices.append((os.path.join(web_path, f), f))
    922924            except OSError:
    923925                pass
    924926        SelectField.__init__(self, field_name, choices, 1, is_required, validator_list)
Back to Top