Django

Code

Ticket #5894: diff.3.diff

File diff.3.diff, 3.2 kB (added by Alex, 7 months ago)

Both diffs in one!

  • 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. 
     
    755756 
    756757    def __init__(self, *args, **kwargs): 
    757758        super(IPAddressField, self).__init__(ipv4_re, *args, **kwargs) 
     759         
     760class FilePathField(ChoiceField): 
     761    """A ChoiceField whose choices are files or folders from the filesystem""" 
     762     
     763    def __init__(self, path, match=None, recursive=False, required=True, widget=None, label=None, initial=None, help_text=None, *args, **kwargs): 
     764        self.path, self.match, self.recursive = path, match, recursive 
     765        super(FilePathField, self).__init__(choices=(), required=required, widget=widget, label=label, initial=initial, help_text=help_text, *args, **kwargs) 
     766        self.choices = [] 
     767        if self.match is not None: 
     768            self.match_re = re.compile(self.match) 
     769        if recursive: 
     770            for root, dirs, files in os.walk(self.path): 
     771                    for f in files: 
     772                        if self.match is None or self.match_re.search(f): 
     773                            f = os.path.join(root, f) 
     774                            self.choices.append((f, f.replace(self.path, "", 1))) 
     775        else: 
     776            try: 
     777                for f in os.listdir(self.path): 
     778                    full_file = os.path.join(self.path, f) 
     779                    if os.path.isfile(full_file) and (self.match is None or self.match_re.search(f)): 
     780                        self.choices.append((full_file, f)) 
     781            except OSError: 
     782                pass 
     783        self.widget.choices = self.choices 
  • django/db/models/fields/__init__.py

    old new  
    812812class FilePathField(Field): 
    813813    def __init__(self, verbose_name=None, name=None, path='', match=None, recursive=False, **kwargs): 
    814814        self.path, self.match, self.recursive = path, match, recursive 
    815         kwargs['max_length'] = kwargs.get('max_length', 100) 
    816815        Field.__init__(self, verbose_name, name, **kwargs) 
    817  
     816 
     817    def formfield(self, **kwargs): 
     818        defaults = { 
     819            'path': self.path, 
     820            'match': self.match, 
     821            'recursive': self.recursive, 
     822            'form_class': forms.FilePathField, 
     823        } 
     824        defaults.update(kwargs) 
     825        return super(FilePathField, self).formfield(**defaults) 
     826 
    818827    def get_manipulator_field_objs(self): 
    819828        return [curry(oldforms.FilePathField, path=self.path, match=self.match, recursive=self.recursive)] 
    820829