Changeset 7323
- Timestamp:
- 03/19/08 17:29:11 (4 months ago)
- Files:
-
- django/trunk/AUTHORS (modified) (1 diff)
- django/trunk/django/db/models/fields/__init__.py (modified) (1 diff)
- django/trunk/django/newforms/fields.py (modified) (3 diffs)
- django/trunk/docs/newforms.txt (modified) (2 diffs)
- django/trunk/tests/regressiontests/forms/fields.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/AUTHORS
r7316 r7323 147 147 gandalf@owca.info 148 148 Marc Garcia <marc.garcia@accopensys.com> 149 Alex Gaynor <alex.gaynor@gmail.com> 149 150 Andy Gayton <andy-django@thecablelounge.com> 150 151 Baishampayan Ghose django/trunk/django/db/models/fields/__init__.py
r7274 r7323 843 843 kwargs['max_length'] = kwargs.get('max_length', 100) 844 844 Field.__init__(self, verbose_name, name, **kwargs) 845 846 def formfield(self, **kwargs): 847 defaults = { 848 'path': self.path, 849 'match': self.match, 850 'recursive': self.recursive, 851 'form_class': forms.FilePathField, 852 } 853 defaults.update(kwargs) 854 return super(FilePathField, self).formfield(**defaults) 845 855 846 856 def get_manipulator_field_objs(self): django/trunk/django/newforms/fields.py
r7025 r7323 5 5 import copy 6 6 import datetime 7 import os 7 8 import re 8 9 import time … … 32 33 'BooleanField', 'NullBooleanField', 'ChoiceField', 'MultipleChoiceField', 33 34 'ComboField', 'MultiValueField', 'FloatField', 'DecimalField', 34 'SplitDateTimeField', 'IPAddressField', 35 'SplitDateTimeField', 'IPAddressField', 'FilePathField', 35 36 ) 36 37 … … 719 720 raise NotImplementedError('Subclasses must implement this method.') 720 721 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 727 super(FilePathField, self).__init__(choices=(), required=required, 728 widget=widget, label=label, initial=initial, help_text=help_text, 729 *args, **kwargs) 730 self.choices = [] 731 if self.match is not None: 732 self.match_re = re.compile(self.match) 733 if recursive: 734 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))) 739 else: 740 try: 741 for f in os.listdir(self.path): 742 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)): 744 self.choices.append((full_file, f)) 745 except OSError: 746 pass 747 self.widget.choices = self.choices 748 721 749 class SplitDateTimeField(MultiValueField): 722 750 default_error_messages = { django/trunk/docs/newforms.txt
r7300 r7323 1334 1334 An ``UploadedFile`` object has two attributes: 1335 1335 1336 ====================== ==================================================== =1337 A rgumentDescription1338 ====================== ==================================================== =1336 ====================== ==================================================== 1337 Attribute Description 1338 ====================== ==================================================== 1339 1339 ``filename`` The name of the file, provided by the uploading 1340 1340 client. 1341 1341 1342 ``content`` The array of bytes comprising the file content. 1342 ====================== ==================================================== =1343 ====================== ==================================================== 1343 1344 1344 1345 The string representation of an ``UploadedFile`` is the same as the filename … … 1349 1350 1350 1351 .. _`bind the file data to the form`: `Binding uploaded files to a form`_ 1352 1353 ``FilePathField`` 1354 ~~~~~~~~~~~~~~~~~ 1355 1356 **New in Django development version** 1357 1358 * Default widget: ``Select`` 1359 * Empty value: ``None`` 1360 * Normalizes to: A unicode object 1361 * Validates that the selected choice exists in the list of choices. 1362 * Error message keys: ``required``, ``invalid_choice`` 1363 1364 The field allows choosing from files inside a certain directory. It takes three 1365 extra arguments: 1366 1367 ============== ========== =============================================== 1368 Argument Required? Description 1369 ============== ========== =============================================== 1370 ``path`` Yes The absolute path to the directory whose 1371 contents you want listed. This directory must 1372 exist. 1373 1374 ``recursive`` No If ``False`` (the default) only the direct 1375 contents of ``path`` will be offered as choices. 1376 If ``True``, the directory will be descended 1377 into recursively and all descendants will be 1378 listed as choices. 1379 1380 ``match`` No A regular expression pattern; only files with 1381 names matching this expression will be allowed 1382 as choices. 1383 ============== ========== =============================================== 1351 1384 1352 1385 ``ImageField`` django/trunk/tests/regressiontests/forms/fields.py
r7294 r7323 1135 1135 u'' 1136 1136 1137 # FilePathField ############################################################### 1138 1139 >>> import os 1140 >>> from django import newforms as forms 1141 >>> path = forms.__file__ 1142 >>> path = os.path.dirname(path) + '/' 1143 >>> path 1144 '.../django/newforms/' 1145 >>> f = forms.FilePathField(path=path) 1146 >>> f.choices.sort() 1147 >>> f.choices 1148 [('.../django/newforms/__init__.py', '__init__.py'), ('.../django/newforms/__init__.pyc', '__init__.pyc'), ('.../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/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')] 1149 >>> f.clean('fields.py') 1150 Traceback (most recent call last): 1151 ... 1152 ValidationError: [u'Select a valid choice. That choice is not one of the available choices.'] 1153 >>> f.clean(path + 'fields.py') 1154 u'.../django/newforms/fields.py' 1155 >>> f = forms.FilePathField(path=path, match='^.*?\.py$') 1156 >>> f.choices.sort() 1157 >>> f.choices 1158 [('.../django/newforms/__init__.py', '__init__.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')] 1159 >>> f = forms.FilePathField(path=path, recursive=True, match='^.*?\.py$') 1160 >>> f.choices.sort() 1161 >>> f.choices 1162 [('.../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')] 1163 1137 1164 # SplitDateTimeField ########################################################## 1138 1165
