Ticket #1642: svn.diff
File svn.diff, 4.2 KB (added by , 18 years ago) |
---|
-
django/db/models/fields/__init__.py
20 20 BLANK_CHOICE_DASH = [("", "---------")] 21 21 BLANK_CHOICE_NONE = [("", "None")] 22 22 23 # filters for chosing files and/or directories with FilePathField, use 24 # * MATCH_FILES to select only files, 25 # * MATCH_DIRECTORIES to select only directories and 26 # * MATCH_FILES | MATCH_DIRECTORIES to select both 27 MATCH_FILES = 1 28 MATCH_DIRECTORIES = 2 29 23 30 # prepares a value for use in a LIKE query 24 31 prep_for_like_query = lambda x: str(x).replace("\\", "\\\\").replace("%", "\%").replace("_", "\_") 25 32 … … 622 629 return os.path.normpath(f) 623 630 624 631 class FilePathField(Field): 625 def __init__(self, verbose_name=None, name=None, path='', match=None, recursive=False, **kwargs):626 self.path, self.match, self. recursive = path, match, recursive632 def __init__(self, verbose_name=None, name=None, path='', match=None, match_type=MATCH_FILES, recursive=False, **kwargs): 633 self.path, self.match, self.match_type, self.recursive = path, match, match_type, recursive 627 634 Field.__init__(self, verbose_name, name, **kwargs) 628 635 629 636 def get_manipulator_field_objs(self): 630 return [curry(forms.FilePathField, path=self.path, match=self.match, recursive=self.recursive)]637 return [curry(forms.FilePathField, path=self.path, match=self.match, match_type=self.match_type, recursive=self.recursive)] 631 638 632 639 class FloatField(Field): 633 640 empty_strings_allowed = False -
django/forms/__init__.py
907 907 908 908 class FilePathField(SelectField): 909 909 "A SelectField whose choices are the files in a given directory." 910 def __init__(self, field_name, path, match=None, recursive=False, is_required=False, validator_list=None):910 def __init__(self, field_name, path, match=None, match_type=None, recursive=False, is_required=False, validator_list=None): 911 911 import os 912 912 from django.db.models import BLANK_CHOICE_DASH 913 from django.db.models.fields import MATCH_DIRECTORIES, MATCH_FILES 913 914 if match is not None: 914 915 import re 915 916 match_re = re.compile(match) 916 917 choices = not is_required and BLANK_CHOICE_DASH[:] or [] 917 918 if recursive: 918 919 for root, dirs, files in os.walk(path): 919 for f in files: 920 if match is None or match_re.search(f): 921 choices.append((os.path.join(root, f), f)) 920 if MATCH_DIRECTORIES == match_type & MATCH_DIRECTORIES: 921 for d in dirs: 922 if match is None or match_re.search(d): 923 choices.append((os.path.join(root, d), d)) 924 if MATCH_FILES == match_type & MATCH_FILES: 925 for f in files: 926 if match is None or match_re.search(f): 927 choices.append((os.path.join(root, f), f)) 922 928 else: 923 929 try: 924 for f in os.listdir(path): 925 full_file = os.path.join(path, f) 926 if os.path.isfile(full_file) and (match is None or match_re.search(f)): 927 choices.append((full_file, f)) 930 if MATCH_DIRECTORIES == match_type & MATCH_DIRECTORIES: 931 for d in os.listdir(path): 932 full_dir = os.path.join(path, d) 933 if os.path.isdir(full_dir) and (match is None or match_re.search(d)): 934 choices.append((full_dir, d)) 935 if MATCH_FILES == match_type & MATCH_FILES: 936 for f in os.listdir(path): 937 full_file = os.path.join(path, f) 938 if os.path.isfile(full_file) and (match is None or match_re.search(f)): 939 choices.append((full_file, f)) 928 940 except OSError: 929 941 pass 930 942 SelectField.__init__(self, field_name, choices, 1, is_required, validator_list)