Ticket #1642: svn.diff

File svn.diff, 4.2 KB (added by Nikolaus Schlemm <nikl@…>, 17 years ago)

a possible way to implement this, works fine here :)

  • django/db/models/fields/__init__.py

     
    2020BLANK_CHOICE_DASH = [("", "---------")]
    2121BLANK_CHOICE_NONE = [("", "None")]
    2222
     23# filters for chosing files and/or directories with FilePathField, use
     24#     * MATCH_FILES to select only files,
     25#     * MATCH_DIRECTORIES to select only directories and
     26#     * MATCH_FILES | MATCH_DIRECTORIES to select both
     27MATCH_FILES = 1
     28MATCH_DIRECTORIES = 2
     29
    2330# prepares a value for use in a LIKE query
    2431prep_for_like_query = lambda x: str(x).replace("\\", "\\\\").replace("%", "\%").replace("_", "\_")
    2532
     
    622629        return os.path.normpath(f)
    623630
    624631class FilePathField(Field):
    625     def __init__(self, verbose_name=None, name=None, path='', match=None, recursive=False, **kwargs):
    626         self.path, self.match, self.recursive = path, match, recursive
     632    def __init__(self, verbose_name=None, name=None, path='', match=None, match_type=MATCH_FILES, recursive=False, **kwargs):
     633        self.path, self.match, self.match_type, self.recursive = path, match, match_type, recursive
    627634        Field.__init__(self, verbose_name, name, **kwargs)
    628635
    629636    def get_manipulator_field_objs(self):
    630         return [curry(forms.FilePathField, path=self.path, match=self.match, recursive=self.recursive)]
     637        return [curry(forms.FilePathField, path=self.path, match=self.match, match_type=self.match_type, recursive=self.recursive)]
    631638
    632639class FloatField(Field):
    633640    empty_strings_allowed = False
  • django/forms/__init__.py

     
    907907
    908908class FilePathField(SelectField):
    909909    "A SelectField whose choices are the files in a given directory."
    910     def __init__(self, field_name, path, match=None, recursive=False, is_required=False, validator_list=None):
     910    def __init__(self, field_name, path, match=None, match_type=None, recursive=False, is_required=False, validator_list=None):
    911911        import os
    912912        from django.db.models import BLANK_CHOICE_DASH
     913        from django.db.models.fields import MATCH_DIRECTORIES, MATCH_FILES
    913914        if match is not None:
    914915            import re
    915916            match_re = re.compile(match)
    916917        choices = not is_required and BLANK_CHOICE_DASH[:] or []
    917918        if recursive:
    918919            for root, dirs, files in os.walk(path):
    919                 for f in files:
    920                     if match is None or match_re.search(f):
    921                         choices.append((os.path.join(root, f), f))
     920                if MATCH_DIRECTORIES == match_type & MATCH_DIRECTORIES:
     921                    for d in dirs:
     922                        if match is None or match_re.search(d):
     923                            choices.append((os.path.join(root, d), d))
     924                if MATCH_FILES == match_type & MATCH_FILES:
     925                    for f in files:
     926                        if match is None or match_re.search(f):
     927                            choices.append((os.path.join(root, f), f))
    922928        else:
    923929            try:
    924                 for f in os.listdir(path):
    925                     full_file = os.path.join(path, f)
    926                     if os.path.isfile(full_file) and (match is None or match_re.search(f)):
    927                         choices.append((full_file, f))
     930                if MATCH_DIRECTORIES == match_type & MATCH_DIRECTORIES:
     931                    for d in os.listdir(path):
     932                        full_dir = os.path.join(path, d)
     933                        if os.path.isdir(full_dir) and (match is None or match_re.search(d)):
     934                            choices.append((full_dir, d))
     935                if MATCH_FILES == match_type & MATCH_FILES:
     936                    for f in os.listdir(path):
     937                        full_file = os.path.join(path, f)
     938                        if os.path.isfile(full_file) and (match is None or match_re.search(f)):
     939                            choices.append((full_file, f))
    928940            except OSError:
    929941                pass
    930942        SelectField.__init__(self, field_name, choices, 1, is_required, validator_list)
Back to Top