Ticket #5894: diff.4.diff

File diff.4.diff, 3.1 KB (added by Alex Gaynor, 16 years ago)

Putting the max_length back into kwargs

  • django/newforms/fields.py

     
    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

     
    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 
     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)
     827
    818828    def get_manipulator_field_objs(self):
    819829        return [curry(oldforms.FilePathField, path=self.path, match=self.match, recursive=self.recursive)]
    820830
Back to Top