Django

Code

Ticket #5894: diff.11.diff

File diff.11.diff, 6.3 kB (added by Alex, 7 months ago)

Fixed a bug introduced last patch

  • django/db/models/fields/__init__.py

    old new  
    814814        self.path, self.match, self.recursive = path, match, recursive 
    815815        kwargs['max_length'] = kwargs.get('max_length', 100) 
    816816        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) 
    817827 
    818828    def get_manipulator_field_objs(self): 
    819829        return [curry(oldforms.FilePathField, path=self.path, match=self.match, recursive=self.recursive)] 
  • django/newforms/fields.py

    old new  
    44 
    55import copy 
    66import datetime 
     7import os 
    78import re 
    89import time 
    910# Python 2.3 fallbacks 
     
    3132    'RegexField', 'EmailField', 'FileField', 'ImageField', 'URLField', 
    3233    'BooleanField', 'NullBooleanField', 'ChoiceField', 'MultipleChoiceField', 
    3334    'ComboField', 'MultiValueField', 'FloatField', 'DecimalField', 
    34     'SplitDateTimeField', 'IPAddressField', 
     35    'SplitDateTimeField', 'IPAddressField', 'FilePathField', 
    3536) 
    3637 
    3738# These values, if given to to_python(), will trigger the self.required check. 
     
    714715        """ 
    715716        raise NotImplementedError('Subclasses must implement this method.') 
    716717 
     718class FilePathField(ChoiceField): 
     719    def __init__(self, path, match=None, recursive=False, required=True, 
     720                 widget=Select, label=None, initial=None, help_text=None, 
     721                 *args, **kwargs): 
     722        self.path, self.match, self.recursive = path, match, recursive 
     723        super(FilePathField, self).__init__(choices=(), required=required, 
     724            widget=widget, label=label, initial=initial, help_text=help_text, 
     725            *args, **kwargs) 
     726        self.choices = [] 
     727        if self.match is not None: 
     728            self.match_re = re.compile(self.match) 
     729        if recursive: 
     730            for root, dirs, files in os.walk(self.path): 
     731                for f in files: 
     732                    if self.match is None or self.match_re.search(f): 
     733                        f = os.path.join(root, f) 
     734                        self.choices.append((f, f.replace(path, "", 1))) 
     735        else: 
     736            try: 
     737                for f in os.listdir(self.path): 
     738                    full_file = os.path.join(self.path, f) 
     739                    if os.path.isfile(full_file) and (self.match is None or self.match_re.search(f)): 
     740                        self.choices.append((full_file, f)) 
     741            except OSError: 
     742                pass 
     743        self.widget.choices = self.choices 
     744 
    717745class SplitDateTimeField(MultiValueField): 
    718746    default_error_messages = { 
    719747        'invalid_date': _(u'Enter a valid date.'), 
  • tests/regressiontests/forms/fields.py

    old new  
    11061106>>> f.clean(None) 
    11071107u'' 
    11081108 
     1109# FilePathField ############################################################### 
     1110 
     1111>>> import os 
     1112>>> from django import newforms as forms 
     1113>>> path = forms.__file__ 
     1114>>> path = os.path.dirname(path) + '/' 
     1115>>> path 
     1116'.../django/newforms/' 
     1117>>> f = forms.FilePathField(path=path) 
     1118>>> f.choices.sort() 
     1119>>> f.choices 
     1120[('.../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')] 
     1121>>> f.clean('fields.py') 
     1122Traceback (most recent call last): 
     1123... 
     1124ValidationError: [u'Select a valid choice. That choice is not one of the available choices.'] 
     1125>>> f.clean(path + 'fields.py') 
     1126u'.../django/newforms/fields.py' 
     1127>>> f = forms.FilePathField(path=path, match='^.*?\.py$') 
     1128>>> f.choices.sort() 
     1129>>> f.choices 
     1130[('.../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')] 
     1131>>> f = forms.FilePathField(path=path, recursive=True, match='^.*?\.py$') 
     1132>>> f.choices.sort() 
     1133>>> f.choices 
     1134[('.../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')] 
     1135 
    11091136# SplitDateTimeField ########################################################## 
    11101137 
    11111138>>> f = SplitDateTimeField() 
  • docs/newforms.txt

    old new  
    13451345 
    13461346.. _`bind the file data to the form`: `Binding uploaded files to a form`_ 
    13471347 
     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 
     1359The 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 
    13481361``ImageField`` 
    13491362~~~~~~~~~~~~~~ 
    13501363