[Django] #20355: How about pre/post_change_file signals to allow apps to delete replaced files?

3 views
Skip to first unread message

Django

unread,
May 6, 2013, 5:27:14 AM5/6/13
to django-...@googlegroups.com
#20355: How about pre/post_change_file signals to allow apps to delete replaced
files?
--------------------------------------+--------------------
Reporter: pdc@… | Owner: nobody
Type: New feature | Status: new
Component: File uploads/storage | Version: 1.5
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+--------------------
Some people are surprised to discover that saving a new file to a
FileField does not delete the old file, and deleting a model instance does
not delete its dependent files. This is for a good reason of course—I may
know that user avatars are disposable, say, but Django does not. But if
the app does know whether old files can e disposed of, there does not seem
to be an easy way to achieve this systematically because in pre_save and
post_save signal handlers we do not have acces to both the old and new
path names.

Would it be easy to have a pre_change_file and post_change_file signal
pair, fired during the point in saving an instance when the files are
really written? Its args would be something like the following:
{{{
sender, instance, field_name, old_path, new_path, **kwargs
}}}
where old_path is None if the file is being saved for the first time, and
new_path is None if the file is being deleted (in other words, replaced
with no file).

As well as enabling app-specific decisions to be me made about deleting
old files, this would also be a hook to hang the recalculation of
ImageField width and height attributes that would avoid recalculating them
needlessly.

--
Ticket URL: <https://code.djangoproject.com/ticket/20355>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 20, 2013, 12:08:08 PM5/20/13
to django-...@googlegroups.com
#20355: How about pre/post_change_file signals to allow apps to delete replaced
files?
-------------------------------------+-------------------------------------

Reporter: pdc@… | Owner: nobody
Type: New feature | Status: new
Component: File | Version: 1.5
uploads/storage | Resolution:
Severity: Normal | Triage Stage:
Keywords: | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by mbrochh@…):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

Can't you use pre_save and get the just to-be-saved model from the DB?
Then you can compare old path against new path.

--
Ticket URL: <https://code.djangoproject.com/ticket/20355#comment:1>

Django

unread,
Jun 16, 2013, 4:27:41 AM6/16/13
to django-...@googlegroups.com
#20355: How about pre/post_change_file signals to allow apps to delete replaced
files?
-------------------------------------+-------------------------------------

Reporter: pdc@… | Owner: nobody
Type: New feature | Status: new
Component: File | Version: 1.5
uploads/storage | Resolution:
Severity: Normal | Triage Stage:
Keywords: | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by akaariai):

Note that deleting the file on the moment the change happens isn't safe.
What if a rollback happens? Even at rollback time this isn't safe - there
is still the possibility there are other transactions open seeing the old
file, but at least that is a temporary error state.

So, I think queuing the file deletes on transaction.commit is needed,
otherwise this is a very dangerous thing to do. So, I am marking this as
wontfix, we can revisit this once transaction.post_commit hook is
available...

--
Ticket URL: <https://code.djangoproject.com/ticket/20355#comment:2>

Django

unread,
Jun 16, 2013, 4:27:56 AM6/16/13
to django-...@googlegroups.com
#20355: How about pre/post_change_file signals to allow apps to delete replaced
files?
-------------------------------------+-------------------------------------
Reporter: pdc@… | Owner: nobody
Type: New feature | Status: closed
Component: File | Version: 1.5
uploads/storage | Resolution: wontfix

Severity: Normal | Triage Stage:
Keywords: | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by akaariai):

* status: new => closed
* resolution: => wontfix


--
Ticket URL: <https://code.djangoproject.com/ticket/20355#comment:3>

Reply all
Reply to author
Forward
0 new messages