Ticket #5894: diff.3.diff
File diff.3.diff, 3.2 KB (added by , 17 years ago) |
---|
-
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. … … 755 756 756 757 def __init__(self, *args, **kwargs): 757 758 super(IPAddressField, self).__init__(ipv4_re, *args, **kwargs) 759 760 class 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
812 812 class FilePathField(Field): 813 813 def __init__(self, verbose_name=None, name=None, path='', match=None, recursive=False, **kwargs): 814 814 self.path, self.match, self.recursive = path, match, recursive 815 kwargs['max_length'] = kwargs.get('max_length', 100)816 815 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 818 827 def get_manipulator_field_objs(self): 819 828 return [curry(oldforms.FilePathField, path=self.path, match=self.match, recursive=self.recursive)] 820 829