Ticket #5894: diff.8.diff
File diff.8.diff, 5.3 KB (added by , 17 years ago) |
---|
-
django/db/models/fields/__init__.py
814 814 self.path, self.match, self.recursive = path, match, recursive 815 815 kwargs['max_length'] = kwargs.get('max_length', 100) 816 816 Field.__init__(self, verbose_name, name, **kwargs) 817 818 def formfield(self, **kwargs): 819 defaults = { 820 'path': self.path, 821 'match': self.match, 822 'recursive': self.recursive, 823 'form_class': forms.FilePathField, 824 } 825 defaults.update(kwargs) 826 return super(FilePathField, self).formfield(**defaults) 817 827 818 828 def get_manipulator_field_objs(self): 819 829 return [curry(oldforms.FilePathField, path=self.path, match=self.match, recursive=self.recursive)] -
django/newforms/fields.py
4 4 5 5 import copy 6 6 import datetime 7 import os 7 8 import re 8 9 import time 9 10 # Python 2.3 fallbacks … … 31 32 'RegexField', 'EmailField', 'FileField', 'ImageField', 'URLField', 32 33 'BooleanField', 'NullBooleanField', 'ChoiceField', 'MultipleChoiceField', 33 34 'ComboField', 'MultiValueField', 'FloatField', 'DecimalField', 34 'SplitDateTimeField', 'IPAddressField', 35 'SplitDateTimeField', 'IPAddressField', 'FilePathField', 35 36 ) 36 37 37 38 # These values, if given to to_python(), will trigger the self.required check. … … 750 751 751 752 def __init__(self, *args, **kwargs): 752 753 super(IPAddressField, self).__init__(ipv4_re, *args, **kwargs) 754 755 class FilePathField(ChoiceField): 756 def __init__(self, path, match=None, recursive=False, required=True, widget=Select, label=None, initial=None, help_text=None, *args, **kwargs): 757 self.path, self.match, self.recursive = path, match, recursive 758 super(FilePathField, self).__init__(choices=(), required=required, widget=widget, label=label, initial=initial, help_text=help_text, *args, **kwargs) 759 self.choices = [] 760 if self.match is not None: 761 self.match_re = re.compile(self.match) 762 if recursive: 763 for root, dirs, files in os.walk(self.path): 764 for f in files: 765 if self.match is None or self.match_re.search(f): 766 f = os.path.join(root, f) 767 self.choices.append(f, f.replace(self.path, "", 1)) 768 else: 769 try: 770 for f in os.listdir(self.path): 771 full_file = os.path.join(self.path, f) 772 if os.path.isfile(full_file) and (self.match is None or self.match_re.search(f)): 773 self.choices.append((full_file, f)) 774 except OSError: 775 pass 776 self.widget.choices = self.choices -
tests/regressiontests/forms/fields.py
1106 1106 >>> f.clean(None) 1107 1107 u'' 1108 1108 1109 # FilePathField ############################################################### 1110 1111 >>> import re 1112 >>> from django import newforms as forms 1113 >>> loc = forms.__file__ 1114 >>> m = re.search(r'(.*?)/__init__\..*$', loc) 1115 >>> loc = m.group(1) + '/' 1116 >>> loc 1117 '.../django/newforms/' 1118 >>> f = forms.FilePathField(path=loc) 1119 >>> f.choices 1120 [('.../django/newforms/util.pyc', 'util.pyc'), ('.../django/newforms/forms.py', 'forms.py'), ('.../django/newforms/models.py', 'models.py'), ('.../django/newforms/widgets.py', 'widgets.py'), ('.../django/newforms/__init__.py', '__init__.py'), ('.../django/newforms/fields.pyc', 'fields.pyc'), ('.../django/newforms/widgets.pyc', 'widgets.pyc'), ('.../django/newforms/models.pyc', 'models.pyc'), ('.../django/newforms/util.py', 'util.py'), ('.../django/newforms/fields.py', 'fields.py'), ('.../django/newforms/fields.py~', 'fields.py~'), ('.../django/newforms/forms.pyc', 'forms.pyc'), ('.../django/newforms/__init__.pyc', '__init__.pyc')] 1121 >>> f.clean('fields.py') 1122 Traceback (most recent call last): 1123 ... 1124 django.newforms.util.ValidationError: [u'Select a valid choice. That choice is not one of the available choices.'] 1125 >>> f.clean(loc+'fields.py') 1126 u'.../django/newforms/fields.py' 1127 1109 1128 # SplitDateTimeField ########################################################## 1110 1129 1111 1130 >>> f = SplitDateTimeField() -
docs/newforms.txt
1345 1345 1346 1346 .. _`bind the file data to the form`: `Binding uploaded files to a form`_ 1347 1347 1348 ``FilePathField`` 1349 ~~~~~~~~~~~~~~ 1350 1351 **New in Django development version** 1352 1353 * Default widget: ``Select`` 1354 * Empty value: ``None`` 1355 * Normalizes to: A unicode object 1356 * Validates that the selected choice exists in the list of choices. 1357 * Error message keys: ``required``, ``invalid_choice`` 1358 1359 The choices are generated from the filesystem. They can be forced to match a specific regulat expression(this only matches the file, not the entire path). It can also be recursive. 1360 1348 1361 ``ImageField`` 1349 1362 ~~~~~~~~~~~~~~ 1350 1363