Ticket #5893: diff.5.diff

File diff.5.diff, 3.7 KB (added by Alex, 7 years ago)

Cleaned up the if brackets again

  • django/oldforms/__init__.py

     
    946946
    947947class FilePathField(SelectField):
    948948    "A SelectField whose choices are the files in a given directory."
    949     def __init__(self, field_name, path, match=None, recursive=False, is_required=False, validator_list=None, max_length=None):
     949    def __init__(self, field_name, path, match=None, recursive=False, is_required=False, validator_list=None, max_length=None, allow_files=True, allow_folders=False):
    950950        import os
    951951        from django.db.models import BLANK_CHOICE_DASH
    952952        if match is not None:
     
    955955        choices = not is_required and BLANK_CHOICE_DASH[:] or []
    956956        if recursive:
    957957            for root, dirs, files in os.walk(path):
    958                 for f in files:
    959                     if match is None or match_re.search(f):
    960                         f = os.path.join(root, f)
    961                         choices.append((f, f.replace(path, "", 1)))
     958                if allow_folders:
     959                    for f in dirs:
     960                        if match is None or match_re.search(f):
     961                            f = os.path.join(root, f)
     962                            choices.append((f, f.replace(path, "", 1)))
     963                if allow_files:
     964                    for f in files:
     965                        if match is None or match_re.search(f):
     966                            f = os.path.join(root, f)
     967                            choices.append((f, f.replace(path, "", 1)))
     968
    962969        else:
    963970            try:
    964971                for f in os.listdir(path):
    965972                    full_file = os.path.join(path, f)
    966                     if os.path.isfile(full_file) and (match is None or match_re.search(f)):
     973                    if allow_folders and (os.path.isdir(full_file) and (match is None or match_re.search(f))):
    967974                        choices.append((full_file, f))
     975                    if allow_files and (os.path.isfile(full_file) and (match is None or match_re.search(f))):
     976                        choices.append((full_file, f))
    968977            except OSError:
    969978                pass
    970979        SelectField.__init__(self, field_name, choices, 1, is_required, validator_list)
  • django/db/models/fields/__init__.py

     
    810810        return super(FileField, self).formfield(**defaults)
    811811
    812812class FilePathField(Field):
    813     def __init__(self, verbose_name=None, name=None, path='', match=None, recursive=False, **kwargs):
    814         self.path, self.match, self.recursive = path, match, recursive
     813    def __init__(self, verbose_name=None, name=None, path='', match=None, recursive=False, allow_files=True, allow_folders=False, **kwargs):
     814        self.path, self.match, self.recursive, self.allow_files, self.allow_folders = path, match, recursive, allow_files, allow_folders
    815815        kwargs['max_length'] = kwargs.get('max_length', 100)
    816816        Field.__init__(self, verbose_name, name, **kwargs)
     817        assert self.allow_folders or self.allow_files, "allow_folders or allow_files can not be both False"
    817818
    818819    def get_manipulator_field_objs(self):
    819         return [curry(oldforms.FilePathField, path=self.path, match=self.match, recursive=self.recursive)]
     820        return [curry(oldforms.FilePathField, path=self.path, match=self.match, recursive=self.recursive, allow_files=self.allow_files, allow_folders=self.allow_folders)]
    820821
    821822class FloatField(Field):
    822823    empty_strings_allowed = False
Back to Top