Ticket #1994: upload_to.diff

File upload_to.diff, 3.1 KB (added by anonymous, 18 years ago)
Line 
1Index: django/db/models/base.py
2===================================================================
3--- django/db/models/base.py (revision 2970)
4+++ django/db/models/base.py (working copy)
5@@ -300,13 +300,14 @@
6 def _get_FIELD_size(self, field):
7 return os.path.getsize(self._get_FIELD_filename(field))
8
9- def _save_FIELD_file(self, field, filename, raw_contents):
10- directory = field.get_directory_name()
11+ def _save_FIELD_file(self, field, filename, raw_contents, custom_upload_to=
12""):
13+
14+ directory = field.get_directory_name(custom_upload_to)
15 try: # Create the date-based directory if it doesn't exist.
16 os.makedirs(os.path.join(settings.MEDIA_ROOT, directory))
17 except OSError: # Directory probably already exists.
18 pass
19- filename = field.get_filename(filename)
20+ filename = field.get_filename(filename, custom_upload_to)
21
22 # If the filename already exists, keep adding an underscore to the name
23 of
24 # the file until the filename doesn't exist.
25Index: django/db/models/fields/__init__.py
26===================================================================
27--- django/db/models/fields/__init__.py (revision 2977)
28+++ django/db/models/fields/__init__.py (working copy)
29@@ -568,7 +568,9 @@
30 setattr(cls, 'get_%s_filename' % self.name, curry(cls._get_FIELD_filena
31me, field=self))
32 setattr(cls, 'get_%s_url' % self.name, curry(cls._get_FIELD_url, field=
33self))
34 setattr(cls, 'get_%s_size' % self.name, curry(cls._get_FIELD_size, fiel
35d=self))
36- setattr(cls, 'save_%s_file' % self.name, lambda instance, filename, raw
37_contents: instance._save_FIELD_file(self, filename, raw_contents))
38+ def save_func(instance, filename, raw_contents, upload_to=""):
39+ instance._save_FIELD_file(self, filename, raw_contents, upload_to)
40+ setattr(cls, 'save_%s_file' % self.name, save_func)
41 dispatcher.connect(self.delete_file, signal=signals.post_delete, sender
42=cls)
43
44 def delete_file(self, instance):
45@@ -595,14 +597,19 @@
46 else:
47 func(new_data[upload_field_name]["filename"], new_data[upload_f
48ield_name]["content"])
49
50- def get_directory_name(self):
51- return os.path.normpath(datetime.datetime.now().strftime(self.upload_to
52))
53+ def get_directory_name(self, custom_upload_to=""):
54+ upload_to = self.upload_to
55+ if custom_upload_to:
56+ upload_to = custom_upload_to
57+ return os.path.normpath(datetime.datetime.now().strftime(upload_to))
58
59- def get_filename(self, filename):
60+ def get_filename(self, filename, custom_upload_to):
61 from django.utils.text import get_valid_filename
62- f = os.path.join(self.get_directory_name(), get_valid_filename(os.path.
63basename(filename)))
64+ f = os.path.join(self.get_directory_name(custom_upload_to), get_valid_f
65ilename(os.path.basename(filename)))
66 return os.path.normpath(f)
67
68+
69+
70 class FilePathField(Field):
71 def __init__(self, verbose_name=None, name=None, path='', match=None, recur
72sive=False, **kwargs):
73 self.path, self.match, self.recursive = path, match, recursive
Back to Top