Ticket #5893: filepath-folders.diff
File filepath-folders.diff, 7.7 KB (added by , 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 3 3 import os 4 4 import re 5 5 import time 6 try:7 import decimal8 except ImportError:9 from django.utils import _decimal as decimal # for Python 2.310 6 11 7 from django.db import connection 12 8 from django.db.models import signals … … class EmailField(CharField): 671 667 class FilePathField(Field): 672 668 """File path""" 673 669 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): 675 672 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 676 675 kwargs['max_length'] = kwargs.get('max_length', 100) 677 676 Field.__init__(self, verbose_name, name, **kwargs) 678 677 … … class FilePathField(Field): 682 681 'match': self.match, 683 682 'recursive': self.recursive, 684 683 'form_class': forms.FilePathField, 684 'allow_files': self.allow_files, 685 'allow_folders': self.allow_folders, 685 686 } 686 687 defaults.update(kwargs) 687 688 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): 822 822 raise NotImplementedError('Subclasses must implement this method.') 823 823 824 824 class FilePathField(ChoiceField): 825 def __init__(self, path, match=None, recursive=False, required=True,826 widget=None, label=None, initial=None, help_text=None,827 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): 828 828 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 829 831 super(FilePathField, self).__init__(choices=(), required=required, 830 832 widget=widget, label=label, initial=initial, help_text=help_text, 831 833 *args, **kwargs) … … class FilePathField(ChoiceField): 840 842 841 843 if recursive: 842 844 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))) 847 855 else: 848 856 try: 849 857 for f in os.listdir(self.path): 850 858 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))): 852 862 self.choices.append((full_file, f)) 853 863 except OSError: 854 864 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: 523 523 A regular expression pattern; only files with names matching this expression 524 524 will be allowed as choices. 525 525 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 526 538 ``FloatField`` 527 539 ~~~~~~~~~~~~~~ 528 540 -
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 571 571 Optional. Either ``True`` or ``False``. Default is ``False``. Specifies 572 572 whether all subdirectories of :attr:`~FilePathField.path` should be included 573 573 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 574 587 Of course, these arguments can be used together. 575 588 576 589 The 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' 1442 1442 >>> fix_os_paths(f.choices) 1443 1443 [('.../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')] 1444 1444 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 1445 1453 # SplitDateTimeField ########################################################## 1446 1454 1447 1455 >>> f = SplitDateTimeField()