Ticket #5893: allow_folders.diff

File allow_folders.diff, 7.8 KB (added by Alex Gaynor, 17 years ago)

Fixed a few typos

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

    diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
    index 13a84ec..bbed7f1 100644
    a b class FileField(Field):  
    838838        return super(FileField, self).formfield(**defaults)
    839839
    840840class FilePathField(Field):
    841     def __init__(self, verbose_name=None, name=None, path='', match=None, recursive=False, **kwargs):
    842         self.path, self.match, self.recursive = path, match, recursive
     841    def __init__(self, verbose_name=None, name=None, path='', match=None, recursive=False, allow_files=True, allow_folders=False, **kwargs):
     842        self.path, self.match, self.recursive, self.allow_files, self.allow_folders = path, match, recursive, allow_files, allow_folders
     843        assert self.allow_files or self.allow_folders
    843844        kwargs['max_length'] = kwargs.get('max_length', 100)
    844845        Field.__init__(self, verbose_name, name, **kwargs)
    845846   
    class FilePathField(Field):  
    848849            'path': self.path,
    849850            'match': self.match,
    850851            'recursive': self.recursive,
     852            'allow_files': self.allow_files,
     853            'allow_folders': self.allow_folders,
    851854            'form_class': forms.FilePathField,
    852855        }
    853856        defaults.update(kwargs)
  • django/newforms/fields.py

    diff --git a/django/newforms/fields.py b/django/newforms/fields.py
    index 08e8b84..d781149 100644
    a b class MultiValueField(Field):  
    720720        raise NotImplementedError('Subclasses must implement this method.')
    721721
    722722class FilePathField(ChoiceField):
    723     def __init__(self, path, match=None, recursive=False, required=True,
    724                  widget=Select, label=None, initial=None, help_text=None,
    725                  *args, **kwargs):
    726         self.path, self.match, self.recursive = path, match, recursive
     723    def __init__(self, path, match=None, recursive=False, allow_files=True,
     724                 allow_folders=False,required=True, widget=Select, label=None,
     725                 initial=None, help_text=None, *args, **kwargs):
     726        self.path, self.match, self.recursive, self.allow_files, self.allow_folders = path, match, recursive, allow_files, allow_folders
     727        assert self.allow_files or self.allow_folders
    727728        super(FilePathField, self).__init__(choices=(), required=required,
    728729            widget=widget, label=label, initial=initial, help_text=help_text,
    729730            *args, **kwargs)
    class FilePathField(ChoiceField):  
    732733            self.match_re = re.compile(self.match)
    733734        if recursive:
    734735            for root, dirs, files in os.walk(self.path):
    735                 for f in files:
    736                     if self.match is None or self.match_re.search(f):
    737                         f = os.path.join(root, f)
    738                         self.choices.append((f, f.replace(path, "", 1)))
     736                if self.allow_files:
     737                    for f in files:
     738                        if self.match is None or self.match_re.search(f):
     739                            f = os.path.join(root, f)
     740                            self.choices.append((f, f.replace(path, "", 1)))
     741                if self.allow_folders:
     742                    for f in dirs:
     743                        if self.match is None or self.match_re.search(f):
     744                            f = os.path.join(root, f)
     745                            self.choices.append((f, f.replace(path, "", 1)))
    739746        else:
    740747            try:
    741748                for f in os.listdir(self.path):
    742749                    full_file = os.path.join(self.path, f)
    743                     if os.path.isfile(full_file) and (self.match is None or self.match_re.search(f)):
     750                    if ((self.allow_files and os.path.isfile(full_file)) or (self.allow_folders and os.path.isdir(full_file))) and (self.match is None or self.match_re.search(f)):
    744751                        self.choices.append((full_file, f))
    745752            except OSError:
    746753                pass
  • docs/model-api.txt

    diff --git a/docs/model-api.txt b/docs/model-api.txt
    index f73c5aa..a6627e2 100644
    a b on the filesystem. Has three special arguments, of which the first is  
    321321    ``recursive``           Optional. Either ``True`` or ``False``. Default is
    322322                            ``False``. Specifies whether all subdirectories of
    323323                            ``path`` should be included.
     324                           
     325    ``allow_files``         Optional.  Specifies whether or not the list of
     326                            choices should contain files, it defaults to
     327                            ``True``. Either this or ``allow_folders`` must be
     328                            ``True``.
     329                           
     330    ``allow_folders``       Optional.  Specifies whether or not the list of
     331                            choices should contain folders, it defaults to
     332                            ``False``.  Either this or ``allow_files`` must be
     333                            ``True``.
    324334    ======================  ===================================================
    325335
    326336Of course, these arguments can be used together.
  • docs/newforms.txt

    diff --git a/docs/newforms.txt b/docs/newforms.txt
    index 533ff75..b6f364b 100644
    a b extra arguments:  
    13801380    ``match``       No          A regular expression pattern; only files with
    13811381                                names matching this expression will be allowed
    13821382                                as choices.
     1383                               
     1384    ``allow_files`` No          Specifies whether or not the list of choices
     1385                                should contain files, it defaults to ``True``.
     1386                                Either this or ``allow_folders`` must be ``True``.
     1387   
     1388    ``allow_folders`` No        Specifies whether or not the list of choices
     1389                                should contain folders, it defaults to ``False``.
     1390                                Either this or ``allow_files`` must be ``True``.
    13831391    ==============  ==========  ===============================================
    13841392
    13851393``ImageField``
  • tests/regressiontests/forms/fields.py

    diff --git a/tests/regressiontests/forms/fields.py b/tests/regressiontests/forms/fields.py
    index 9421d8c..7a43c3c 100644
    a b u'.../django/newforms/fields.py'  
    11591159>>> f.choices.sort()
    11601160>>> f.choices
    11611161[('.../django/newforms/__init__.py', '__init__.py'), ('.../django/newforms/extras/__init__.py', 'extras/__init__.py'), ('.../django/newforms/extras/widgets.py', 'extras/widgets.py'), ('.../django/newforms/fields.py', 'fields.py'), ('.../django/newforms/forms.py', 'forms.py'), ('.../django/newforms/models.py', 'models.py'), ('.../django/newforms/util.py', 'util.py'), ('.../django/newforms/widgets.py', 'widgets.py')]
     1162>>> f = forms.FilePathField(path=path, allow_folders=True, allow_files=False)
     1163>>> f.choices
     1164[('.../django/newforms/extras', 'extras')]
     1165>>> f = forms.FilePathField(path=path, allow_folders=True, allow_files=True)
     1166>>> f.choices.sort()
     1167>>> f.choices
     1168[('.../django/newforms/__init__.py', '__init__.py'), ('.../django/newforms/__init__.pyc', '__init__.pyc'), ('.../django/newforms/extras', 'extras'), ('.../django/newforms/fields.py', 'fields.py'), ('.../django/newforms/fields.pyc', 'fields.pyc'), ('.../django/newforms/forms.py', 'forms.py'), ('.../django/newforms/forms.pyc', 'forms.pyc'), ('.../django/newforms/formsets.pyc', 'formsets.pyc'), ('.../django/newforms/models.py', 'models.py'), ('.../django/newforms/models.pyc', 'models.pyc'), ('.../django/newforms/util.py', 'util.py'), ('.../django/newforms/util.pyc', 'util.pyc'), ('.../django/newforms/widgets.py', 'widgets.py'), ('.../django/newforms/widgets.pyc', 'widgets.pyc')]
    11621169
    11631170# SplitDateTimeField ##########################################################
    11641171
Back to Top