Code

Opened 9 years ago

Closed 9 years ago

Last modified 8 years ago

#589 closed enhancement (fixed)

A patch that adds FilePathField

Reported by: jay@… Owned by: adrian
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: UI/UX:

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@… 9 years ago.
FilePathField patch

Download all attachments as: .zip

Change History (3)

Changed 9 years ago by jay@…

FilePathField patch

comment:1 Changed 9 years ago by adrian

  • Status changed from new to assigned

comment:2 Changed 9 years ago by adrian

  • Resolution set to fixed
  • Status changed from assigned to closed

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.