Ticket #5893: filepath-folders.diff

File filepath-folders.diff, 7.7 KB (added by Alex Gaynor, 15 years ago)
  • django/db/models/fields/__init__.py

    diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
    index 05238cf..192881b 100644
    a b import datetime  
    33import os
    44import re
    55import time
    6 try:
    7     import decimal
    8 except ImportError:
    9     from django.utils import _decimal as decimal    # for Python 2.3
    106
    117from django.db import connection
    128from django.db.models import signals
    class EmailField(CharField):  
    671667class FilePathField(Field):
    672668    """File path"""
    673669   
    674     def __init__(self, verbose_name=None, name=None, path='', match=None, recursive=False, **kwargs):
     670    def __init__(self, verbose_name=None, name=None, path='', match=None,
     671        recursive=False, allow_files=True, allow_folders=False, **kwargs):
    675672        self.path, self.match, self.recursive = path, match, recursive
     673        self.allow_files, self.allow_folders =  allow_files, allow_folders
     674        assert self.allow_files or self.allow_folders
    676675        kwargs['max_length'] = kwargs.get('max_length', 100)
    677676        Field.__init__(self, verbose_name, name, **kwargs)
    678677
    class FilePathField(Field):  
    682681            'match': self.match,
    683682            'recursive': self.recursive,
    684683            'form_class': forms.FilePathField,
     684            'allow_files': self.allow_files,
     685            'allow_folders': self.allow_folders,
    685686        }
    686687        defaults.update(kwargs)
    687688        return super(FilePathField, self).formfield(**defaults)
  • django/forms/fields.py

    diff --git a/django/forms/fields.py b/django/forms/fields.py
    index 0aef355..bf7384c 100644
    a b class MultiValueField(Field):  
    822822        raise NotImplementedError('Subclasses must implement this method.')
    823823
    824824class FilePathField(ChoiceField):
    825     def __init__(self, path, match=None, recursive=False, required=True,
    826                  widget=None, label=None, initial=None, help_text=None,
    827                 *args, **kwargs):
     825    def __init__(self, path, match=None, recursive=False, allow_files=True,
     826        allow_folders=False, required=True, widget=Select, label=None,
     827        initial=None, help_text=None, *args, **kwargs):
    828828        self.path, self.match, self.recursive = path, match, recursive
     829        self.allow_files, self.allow_folders = allow_files, allow_folders
     830        assert self.allow_files or self.allow_folders
    829831        super(FilePathField, self).__init__(choices=(), required=required,
    830832            widget=widget, label=label, initial=initial, help_text=help_text,
    831833            *args, **kwargs)
    class FilePathField(ChoiceField):  
    840842
    841843        if recursive:
    842844            for root, dirs, files in os.walk(self.path):
    843                 for f in files:
    844                     if self.match is None or self.match_re.search(f):
    845                         f = os.path.join(root, f)
    846                         self.choices.append((f, f.replace(path, "", 1)))
     845                if self.allow_files:
     846                    for f in files:
     847                        if self.match is None or self.match_re.search(f):
     848                            f = os.path.join(root, f)
     849                            self.choices.append((f, f.replace(path, "", 1)))
     850                if self.allow_folders:
     851                    for f in dirs:
     852                        if self.match is None or self.match_re.search(f):
     853                            f = os.path.join(root, f)
     854                            self.choices.append((f, f.replace(path, "", 1)))
    847855        else:
    848856            try:
    849857                for f in os.listdir(self.path):
    850858                    full_file = os.path.join(self.path, f)
    851                     if os.path.isfile(full_file) and (self.match is None or self.match_re.search(f)):
     859                    if (((self.allow_files and os.path.isfile(full_file)) or
     860                        (self.allow_folders and os.path.isdir(full_file))) and
     861                        (self.match is None or self.match_re.search(f))):
    852862                        self.choices.append((full_file, f))
    853863            except OSError:
    854864                pass
  • docs/ref/forms/fields.txt

    diff --git a/docs/ref/forms/fields.txt b/docs/ref/forms/fields.txt
    index 4bb6a7c..4b22732 100644
    a b extra arguments; only ``path`` is required:  
    523523    A regular expression pattern; only files with names matching this expression
    524524    will be allowed as choices.
    525525
     526.. attribute:: FilePathField.allow_files
     527
     528    Optional.  Either ``True`` or ``False``.  Default is ``True``.  Specifies
     529    whether files in the specified location should be included.  Either this or
     530    :attr:`~FilePathField.allow_folders` must be ``True``.
     531
     532.. attribute:: FilePathField.allow_folders
     533
     534    Optional.  Either ``True`` or ``False``.  Default is ``False``.  Specifies
     535    whether folders in the specified location should be included.  Either this
     536    or :attr:`~FilePathField.allow_files` must be ``True``.
     537
    526538``FloatField``
    527539~~~~~~~~~~~~~~
    528540
  • docs/ref/models/fields.txt

    diff --git a/docs/ref/models/fields.txt b/docs/ref/models/fields.txt
    index 0cb5be4..e40e817 100644
    a b directory on the filesystem. Has three special arguments, of which the first is  
    571571    Optional. Either ``True`` or ``False``. Default is ``False``. Specifies
    572572    whether all subdirectories of :attr:`~FilePathField.path` should be included
    573573
     574.. attribute:: FilePathField.allow_files
     575
     576    Optional.  Either ``True`` or ``False``.  Default is ``True``.  Specifies
     577    whether files in the specified location should be included.  Either this or
     578    :attr:`~FilePathField.allow_folders` must be ``True``.
     579
     580.. attribute:: FilePathField.allow_folders
     581
     582    Optional.  Either ``True`` or ``False``.  Default is ``False``.  Specifies
     583    whether folders in the specified location should be included.  Either this
     584    or :attr:`~FilePathField.allow_files` must be ``True``.
     585
     586
    574587Of course, these arguments can be used together.
    575588
    576589The one potential gotcha is that :attr:`~FilePathField.match` applies to the
  • tests/regressiontests/forms/fields.py

    diff --git a/tests/regressiontests/forms/fields.py b/tests/regressiontests/forms/fields.py
    index 9d407d9..578ed24 100644
    a b u'.../django/forms/fields.py'  
    14421442>>> fix_os_paths(f.choices)
    14431443[('.../django/forms/__init__.py', '__init__.py'), ('.../django/forms/extras/__init__.py', 'extras/__init__.py'), ('.../django/forms/extras/widgets.py', 'extras/widgets.py'), ('.../django/forms/fields.py', 'fields.py'), ('.../django/forms/forms.py', 'forms.py'), ('.../django/forms/models.py', 'models.py'), ('.../django/forms/util.py', 'util.py'), ('.../django/forms/widgets.py', 'widgets.py')]
    14441444
     1445>>> f = forms.FilePathField(path=path, allow_folders=True, allow_files=False)
     1446>>> fix_os_paths(f.choices)
     1447[('.../django/forms/extras', 'extras')]
     1448>>> f = forms.FilePathField(path=path, allow_folders=True, allow_files=True)
     1449>>> f.choices.sort()
     1450>>> fix_os_paths(f.choices)
     1451[('.../django/forms/__init__.py', '__init__.py'), ('.../django/forms/__init__.pyc', '__init__.pyc'), ('.../django/forms/extras', 'extras'), ('.../django/forms/fields.py', 'fields.py'), ('.../django/forms/fields.pyc', 'fields.pyc'), ('.../django/forms/forms.py', 'forms.py'), ('.../django/forms/forms.pyc', 'forms.pyc'), ('.../django/forms/formsets.pyc', 'formsets.pyc'), ('.../django/forms/models.py', 'models.py'), ('.../django/forms/models.pyc', 'models.pyc'), ('.../django/forms/util.py', 'util.py'), ('.../django/forms/util.pyc', 'util.pyc'), ('.../django/forms/widgets.py', 'widgets.py'), ('.../django/forms/widgets.pyc', 'widgets.pyc')]
     1452
    14451453# SplitDateTimeField ##########################################################
    14461454
    14471455>>> f = SplitDateTimeField()
Back to Top