Ticket #5893: diff.diff

File diff.diff, 3.9 KB (added by Alex Gaynor, 17 years ago)
  • 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=True, **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)
    817817
    818818    def get_manipulator_field_objs(self):
    819         return [curry(oldforms.FilePathField, path=self.path, match=self.match, recursive=self.recursive)]
     819        return [curry(oldforms.FilePathField, path=self.path, match=self.match, recursive=self.recursive, allow_files=self.allow_files, allow_folders=self.allow_folders)]
    820820
    821821class FloatField(Field):
    822822    empty_strings_allowed = False
  • 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=True):
    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_files:
     959                    for f in files:
     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_folders:
     964                    for f in dirs:
     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)))
    962968        else:
    963969            try:
    964970                for f in os.listdir(path):
    965971                    full_file = os.path.join(path, f)
    966                     if os.path.isfile(full_file) and (match is None or match_re.search(f)):
    967                         choices.append((full_file, f))
     972                    if allow_files and allow_folders:
     973                        if (os.path.isfile(full_file) or os.path.isdir(full_file)) and (match is None or match_re.search(f)):
     974                            choices.append((full_file, f))
     975                    elif allow_folders:
     976                        if os.path.isdir(full_file) and (match is None or match_re.search(f)):
     977                            choices.append((full_file, f))
     978                    elif allow_files:
     979                        if os.path.isfile(full_file) and (match is None or match_re.search(f)):
     980                            choices.append((full_file, f))
    968981            except OSError:
    969982                pass
    970983        SelectField.__init__(self, field_name, choices, 1, is_required, validator_list)
Back to Top