Ticket #2598: FilePathFieldEnhancement.diff
File FilePathFieldEnhancement.diff, 2.8 KB (added by , 18 years ago) |
---|
-
db/models/fields/__init__.py
613 613 return os.path.normpath(f) 614 614 615 615 class FilePathField(Field): 616 def __init__(self, verbose_name=None, name=None, path='', match=None, recursive=False, **kwargs): 617 self.path, self.match, self.recursive = path, match, recursive 616 def __init__(self, verbose_name=None, name=None, path='', match=None, recursive=False, web_path=None, **kwargs): 617 if not web_path: 618 web_path = path 619 self.path, self.match, self.recursive, self.web_path= path, match, recursive, web_path 618 620 Field.__init__(self, verbose_name, name, **kwargs) 619 621 620 622 def get_manipulator_field_objs(self): 621 return [curry(forms.FilePathField, path=self.path, match=self.match, recursive=self.recursive )]623 return [curry(forms.FilePathField, path=self.path, match=self.match, recursive=self.recursive, web_path=self.web_path)] 622 624 623 625 class FloatField(Field): 624 626 empty_strings_allowed = False -
forms/__init__.py
901 901 902 902 class FilePathField(SelectField): 903 903 "A SelectField whose choices are the files in a given directory." 904 def __init__(self, field_name, path, match=None, recursive=False, is_required=False, validator_list=None):904 def __init__(self, field_name, path, match=None, recursive=False, web_path=None, is_required=False, validator_list=None): 905 905 import os 906 906 from django.db.models import BLANK_CHOICE_DASH 907 path, web_path = os.path.normpath(path), os.path.normpath(web_path) 907 908 if match is not None: 908 909 import re 909 910 match_re = re.compile(match) 910 911 choices = not is_required and BLANK_CHOICE_DASH[:] or [] 911 912 if recursive: 913 ignore_length = len(path)+1 912 914 for root, dirs, files in os.walk(path): 913 915 for f in files: 914 916 if match is None or match_re.search(f): 915 choices.append((os.path.join( root, f), f))917 choices.append((os.path.join(web_path, root[ignore_length:], f), f)) 916 918 else: 917 919 try: 918 920 for f in os.listdir(path): 919 921 full_file = os.path.join(path, f) 920 922 if os.path.isfile(full_file) and (match is None or match_re.search(f)): 921 choices.append(( full_file, f))923 choices.append((os.path.join(web_path, f), f)) 922 924 except OSError: 923 925 pass 924 926 SelectField.__init__(self, field_name, choices, 1, is_required, validator_list)