Django

Code

Ticket #5894: diff.diff

File diff.diff, 2.2 kB (added by Alex, 8 months ago)

Newforms didn't have a filepath field, so I have created one, this should be the first step to fixing the admin, I think

  • 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