Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Dec. 8, 2010 18:24:55

dmitry b.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

FileField and location and storage


Hi,

I've written a custom Storage that uses WebDAV as its underlying
engine. When I save a file '/foo/bar/file.ext', I want it to be saved
under /foo/bar in the webdav filesystem. However, it seems that
FileField overrides the directory structure:

def generate_filename(self, instance, filename):
return os.path.join(self.get_directory_name(),
self.get_filename(filename))

where get_directory_name() is defined as

def get_directory_name(self):
return
os.path.normpath(force_unicode(datetime.datetime.now().strftime(smart_str(self.upload_to))))

I'm not really sure why FileField finds it appropriate to impose its
own directory structure on the underlying storage engine. But short
of creating a custom subclass of FileField and overriding
generate_filename and/or get_directory_name(), is there a different
way of preserving the original file location path?


Thanks
Dmitry

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Dec. 9, 2010 04:43:57

Sam L.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

FileField and location and storage


I have a custom file storage working with a specific subdir structure.

Did you pass in a method specifying the path in the upload_to kwarg
for FileField?

Here's my model code, where EnhancedFileSystemStorage is my custom
file storage class, and get_random_dir_name is just a helper method
that does exactly as named.

file_storage = EnhancedFileSystemStorage(location=settings.DYNMEDIA_ROOT,
base_url=settings.DYNMEDIA_URL)

def file_upload_to(instance, filename):
return os.path.join(get_random_dir_name(instance.file_storage.location,
filename), filename)

file = models.FileField(upload_to=file_upload_to, storage=file_storage)

On 9 December 2010 05:24, dmitry b <dmitry.ma...@gmail.com> wrote:
> Hi,
>
> I've written a custom Storage that uses WebDAV as its underlying
> engine.  When I save a file '/foo/bar/file.ext', I want it to be saved
> under /foo/bar in the webdav filesystem.  However, it seems that
> FileField overrides the directory structure:
>
>    def generate_filename(self, instance, filename):
>        return os.path.join(self.get_directory_name(),
> self.get_filename(filename))
>
> where get_directory_name() is defined as
>
>    def get_directory_name(self):
>        return
> os.path.normpath(force_unicode(datetime.datetime.now().strftime(smart_str(self.upload_to))))
>
> I'm not really sure why FileField finds it appropriate to impose its
> own directory structure on the underlying storage engine.  But short
> of creating a custom subclass of FileField and overriding
> generate_filename and/or get_directory_name(), is there a different
> way of preserving the original file location path?
>
>
> Thanks
> Dmitry
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-us...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
>http://groups.google.com/group/django-users?hl=en.
>
>

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Dec. 9, 2010 04:46:57

Sam L.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

FileField and location and storage


Some more background -

If you look at lines 228 to 230 of django/db/models/fields/files.py,
you'll see that the generate_filename method is overwritten by your
upload_to method if you specify one.

228 self.upload_to = upload_to
229 if callable(upload_to):
230 self.generate_filename = upload_to

On 9 December 2010 15:43, Sam Lai <samuel....@gmail.com> wrote:
> I have a custom file storage working with a specific subdir structure.
>
> Did you pass in a method specifying the path in the upload_to kwarg
> for FileField?
>
> Here's my model code, where EnhancedFileSystemStorage is my custom
> file storage class, and get_random_dir_name is just a helper method
> that does exactly as named.
>
>    file_storage = EnhancedFileSystemStorage(location=settings.DYNMEDIA_ROOT,
>                                             base_url=settings.DYNMEDIA_URL)
>
>    def file_upload_to(instance, filename):
>        return os.path.join(get_random_dir_name(instance.file_storage.location,
> filename), filename)
>
>    file = models.FileField(upload_to=file_upload_to, storage=file_storage)
>
> On 9 December 2010 05:24, dmitry b <dmitry.ma...@gmail.com> wrote:
>> Hi,
>>
>> I've written a custom Storage that uses WebDAV as its underlying
>> engine.  When I save a file '/foo/bar/file.ext', I want it to be saved
>> under /foo/bar in the webdav filesystem.  However, it seems that
>> FileField overrides the directory structure:
>>
>>    def generate_filename(self, instance, filename):
>>        return os.path.join(self.get_directory_name(),
>> self.get_filename(filename))
>>
>> where get_directory_name() is defined as
>>
>>    def get_directory_name(self):
>>        return
>> os.path.normpath(force_unicode(datetime.datetime.now().strftime(smart_str(self.upload_to))))
>>
>> I'm not really sure why FileField finds it appropriate to impose its
>> own directory structure on the underlying storage engine.  But short
>> of creating a custom subclass of FileField and overriding
>> generate_filename and/or get_directory_name(), is there a different
>> way of preserving the original file location path?
>>
>>
>> Thanks
>> Dmitry
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To post to this group, send email to django-us...@googlegroups.com.
>> To unsubscribe from this group, send email to
>> django-users+unsubscr...@googlegroups.com.
>> For more options, visit this group at
>>http://groups.google.com/group/django-users?hl=en.
>>
>>
>

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 11th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support