Opened 8 years ago

Closed 7 years ago

#24105 closed Cleanup/optimization (fixed)

When upload_to is callable, storage.get_valid_name() does not get called

Reported by: John Bowen Owned by: Abhaya Agarwal
Component: File uploads/storage Version: dev
Severity: Normal Keywords: upload_to, get_valid_name
Cc: Abhaya Agarwal Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Module: db.models.fields.files.py

Present in recent commits of versions 1.8 and 1.7
Commit: c1493879d98328040b36ad93cd104c0b3b546772
Commit: bbcbacf0ad4fc9aa89d13c35f4b084185b284a49

Summary summed it up. But maybe the Storage class should be calling get_valid_name like the docs suggest. -see (_save)
https://docs.djangoproject.com/en/1.7/howto/custom-file-storage/

"_save(name, content)¶
Called by Storage.save(). The name will already have gone through get_valid_name() and get_available_name(), and the content will be a File object itself."

Attachments (1)

files.patch (1.1 KB) - added by John Bowen 8 years ago.

Download all attachments as: .zip

Change History (8)

Changed 8 years ago by John Bowen

Attachment: files.patch added

comment:1 Changed 8 years ago by Tim Graham

Component: Database layer (models, ORM)File uploads/storage
Needs documentation: set
Needs tests: set
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

This might be a reasonable change, but we need to think if it might break backwards compatibility in any way. It will also need tests and docs.

comment:2 Changed 8 years ago by Berker Peksag

Patch needs improvement: set

The patch raises a SyntaxError at the following line:

+                       directory_name, filename = os.path.split(self.upload_to(instance, filename)

The else clause is not needed:

if callable(self.upload_to):
    # do stuff
return os.path.join(self.get_directory_name(), self.get_filename(filename))

Indentation should be 4 spaces.

comment:3 Changed 7 years ago by Abhaya Agarwal

Version: 1.7master

With this change, the documentation of get_valid_name should also be updated to clarify that the passed name might come from upload_to also.
https://docs.djangoproject.com/en/1.7/howto/custom-file-storage/

get_valid_name(name)¶
Returns a filename suitable for use with the underlying storage system. The name argument passed to this method is the original filename sent to the server, after having any path information removed.

comment:4 Changed 7 years ago by Abhaya Agarwal

Owner: changed from nobody to Abhaya Agarwal
Status: newassigned

comment:5 Changed 7 years ago by Abhaya Agarwal

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

I have created a pull request at https://github.com/django/django/pull/4603 with documentation & test cases based on the original patch.

comment:6 Changed 7 years ago by Abhaya Agarwal

Cc: Abhaya Agarwal added

comment:7 Changed 7 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In 9de9c240:

Fixed #24105 -- Called Storage.get_valid_name() when upload_to is callable

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