Opened 19 years ago

Closed 19 years ago

Last modified 18 years ago

#589 closed enhancement (fixed)

A patch that adds FilePathField

Reported by: jay@… Owned by: Adrian Holovaty
Component: contrib.admin Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I coded a patch that implements the FilePathField that was discussed at http://www.socialistsoftware.com/?p=12 and http://groups.google.com/group/django-developers/browse_thread/thread/e0d0bd7f303179e8/2fb7e3ff4409175a

The interface works like this:

fieldName = meta.FilePathField(path='/path/to/files', match='re_string', recursive=True)

path is the location of the files you want to choose
match is a string that is put into re.compile(match) so that only filenames that match that re are included in the list
recursive is a boolean which when set to True allows all the files in subdirectories of path to be included in the list

path is the only required parameter.

I am not sure of the correct way to submit patches so you can download it at http://socialistsoftware.com/django/FilePathField.diff
and I have pasted it inline below.

Index: trunk/django/core/meta/fields.py
===================================================================
--- trunk/django/core/meta/fields.py    (revision 770)
+++ trunk/django/core/meta/fields.py    (working copy)
@@ -427,6 +427,43 @@
         f = os.path.join(self.get_directory_name(), get_valid_filename(os.path.basename(filename)))
         return os.path.normpath(f)

+class FilePathField(Field):
+    def __init__(self, verbose_name=None, name=None, path='', match='', recursive=False, **kwargs):
+        self.path = path
+        self.match = match
+        self.recursive = recursive
+        Field.__init__(self, verbose_name, name, **kwargs)
+
+    def get_manipulator_fields(self, opts, manipulator, change, name_prefix='', rel=False):
+       if self.match != '':
+           import re
+           p = re.compile(self.match)
+        files = os.listdir(self.path)
+        theFileList = []
+        if self.recursive:
+            for root, dirs, files in os.walk(self.path):
+                for f in files:
+                   if self.match != '':
+                       m = p.match(f)
+                       if m:
+                           theFileList.append(tuple([ os.path.join(self.path, f), f]))
+                   else:
+                       theFileList.append(tuple([ os.path.join(self.path, f), f]))
+        else:
+            for f in files:
+                if os.path.isfile(os.path.join(self.path,f)):
+                   if self.match != '':
+                       m = p.match(f)
+                       if m:
+                               theFileList.append(tuple([ os.path.join(self.path,f), f]))
+                   else:
+                       theFileList.append(tuple([ os.path.join(self.path,f), f]))
+        self.choices = tuple(theFileList)
+        return Field.get_manipulator_fields(self, opts, manipulator, change, name_prefix, rel)
+
+    def get_manipulator_field_objs(self):
+        return [formfields.TextField]
+
 class FloatField(Field):
     empty_strings_allowed = False
     def __init__(self, verbose_name=None, name=None, max_digits=None, decimal_places=None, **kwargs):

Attachments (1)

FilePathField.diff (1.9 KB ) - added by jay@… 19 years ago.
FilePathField patch

Download all attachments as: .zip

Change History (3)

by jay@…, 19 years ago

Attachment: FilePathField.diff added

FilePathField patch

comment:1 by Adrian Holovaty, 19 years ago

Status: newassigned

comment:2 by Adrian Holovaty, 19 years ago

Resolution: fixed
Status: assignedclosed

(In [846]) Fixed #589 -- Added FilePathField. It's available as an ORM field and as a standalone field in django.core.formfields. Thanks, jay@…

Note: See TracTickets for help on using tickets.
Back to Top