Ticket #5893: allow_folders.3.diff
File allow_folders.3.diff, 7.7 KB (added by , 16 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 13a84ec..bbed7f1 100644
a b class FileField(Field): 838 838 return super(FileField, self).formfield(**defaults) 839 839 840 840 class 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 843 844 kwargs['max_length'] = kwargs.get('max_length', 100) 844 845 Field.__init__(self, verbose_name, name, **kwargs) 845 846 … … class FilePathField(Field): 848 849 'path': self.path, 849 850 'match': self.match, 850 851 'recursive': self.recursive, 852 'allow_files': self.allow_files, 853 'allow_folders': self.allow_folders, 851 854 'form_class': forms.FilePathField, 852 855 } 853 856 defaults.update(kwargs) -
django/forms/fields.py
diff --git a/django/forms/fields.py b/django/forms/fields.py index 08e8b84..d781149 100644
a b class MultiValueField(Field): 720 720 raise NotImplementedError('Subclasses must implement this method.') 721 721 722 722 class 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 727 728 super(FilePathField, self).__init__(choices=(), required=required, 728 729 widget=widget, label=label, initial=initial, help_text=help_text, 729 730 *args, **kwargs) … … class FilePathField(ChoiceField): 732 733 self.match_re = re.compile(self.match) 733 734 if recursive: 734 735 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))) 739 746 else: 740 747 try: 741 748 for f in os.listdir(self.path): 742 749 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)): 744 751 self.choices.append((full_file, f)) 745 752 except OSError: 746 753 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 321 321 ``recursive`` Optional. Either ``True`` or ``False``. Default is 322 322 ``False``. Specifies whether all subdirectories of 323 323 ``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``. 324 334 ====================== =================================================== 325 335 326 336 Of course, these arguments can be used together. -
docs/forms.txt
diff --git a/docs/forms.txt b/docs/forms.txt index 533ff75..b6f364b 100644
a b extra arguments: 1380 1380 ``match`` No A regular expression pattern; only files with 1381 1381 names matching this expression will be allowed 1382 1382 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``. 1383 1391 ============== ========== =============================================== 1384 1392 1385 1393 ``ImageField`` -
tests/regressiontests/forms/fields.py
diff --git a/tests/regressiontests/forms/fields.py b/tests/regressiontests/forms/fields.py index f3b6a96..1dc5647 100644
a b u'.../django/forms/fields.py' 1169 1169 >>> f.choices.sort() 1170 1170 >>> fix_os_paths(f.choices) 1171 1171 [('.../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')] 1172 >>> f = forms.FilePathField(path=path, allow_folders=True, allow_files=False) 1173 >>> fix_os_paths(f.choices) 1174 [('.../django/forms/extras', 'extras')] 1175 >>> f = forms.FilePathField(path=path, allow_folders=True, allow_files=True) 1176 >>> f.choices.sort() 1177 >>> fix_os_paths(f.choices) 1178 [('.../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')] 1172 1179 1173 1180 # SplitDateTimeField ########################################################## 1174 1181