On 5/24/06, Ivan Sagalaev <[EMAIL PROTECTED]> wrote:
> I would prefer self.upload_to to be restored to its original value after
> call. Otherwise a call to save_*_file with a custom parameter would
> change object's "global" behavior which is very counter-intuitive. In
> fact I think custom upload_to should be passed further down to
> _save_FIELD_file and replaced there locally.
Ok, that makes a lot of sense, especiallt the part about not changing
the global behaviour.
Try #2:
Index: django/db/models/base.py
===================================================================
--- django/db/models/base.py (revision 2970)
+++ django/db/models/base.py (working copy)
@@ -300,13 +300,14 @@
def _get_FIELD_size(self, field):
return os.path.getsize(self._get_FIELD_filename(field))
- def _save_FIELD_file(self, field, filename, raw_contents):
- directory = field.get_directory_name()
+ def _save_FIELD_file(self, field, filename, raw_contents, custom_upload_to=
""):
+
+ directory = field.get_directory_name(custom_upload_to)
try: # Create the date-based directory if it doesn't exist.
os.makedirs(os.path.join(settings.MEDIA_ROOT, directory))
except OSError: # Directory probably already exists.
pass
- filename = field.get_filename(filename)
+ filename = field.get_filename(filename, custom_upload_to)
# If the filename already exists, keep adding an underscore to the name
of
# the file until the filename doesn't exist.
Index: django/db/models/fields/__init__.py
===================================================================
--- django/db/models/fields/__init__.py (revision 2977)
+++ django/db/models/fields/__init__.py (working copy)
@@ -568,7 +568,9 @@
setattr(cls, 'get_%s_filename' % self.name, curry(cls._get_FIELD_filena
me, field=self))
setattr(cls, 'get_%s_url' % self.name, curry(cls._get_FIELD_url, field=
self))
setattr(cls, 'get_%s_size' % self.name, curry(cls._get_FIELD_size, fiel
d=self))
- setattr(cls, 'save_%s_file' % self.name, lambda instance, filename, raw
_contents: instance._save_FIELD_file(self, filename, raw_contents))
+ def save_func(instance, filename, raw_contents, upload_to=""):
+ instance._save_FIELD_file(self, filename, raw_contents, upload_to)
+ setattr(cls, 'save_%s_file' % self.name, save_func)
dispatcher.connect(self.delete_file, signal=signals.post_delete, sender
=cls)
def delete_file(self, instance):
@@ -595,14 +597,19 @@
else:
func(new_data[upload_field_name]["filename"], new_data[upload_f
ield_name]["content"])
- def get_directory_name(self):
- return os.path.normpath(datetime.datetime.now().strftime(self.upload_to
))
+ def get_directory_name(self, custom_upload_to=""):
+ upload_to = self.upload_to
+ if custom_upload_to:
+ upload_to = custom_upload_to
+ return os.path.normpath(datetime.datetime.now().strftime(upload_to))
- def get_filename(self, filename):
+ def get_filename(self, filename, custom_upload_to):
from django.utils.text import get_valid_filename
- f = os.path.join(self.get_directory_name(), get_valid_filename(os.path.
basename(filename)))
+ f = os.path.join(self.get_directory_name(custom_upload_to), get_valid_f
ilename(os.path.basename(filename)))
return os.path.normpath(f)
+
+
class FilePathField(Field):
def __init__(self, verbose_name=None, name=None, path='', match=None, recur
sive=False, **kwargs):
self.path, self.match, self.recursive = path, match, recursive
Jay P.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---