Django

Code

Ticket #5619 (reopened)

Opened 1 year ago

Last modified 5 months ago

FileField and ImageField return the wrong path/url before calling save_FOO_file()

Reported by: wreese@gmail.com Assigned to: leahculver
Milestone: Component: Core framework
Version: SVN Keywords:
Cc: Triage Stage: Someday/Maybe
Has patch: 1 Needs documentation: 0
Needs tests: 1 Patch needs improvement: 0

Description

Models with FileField? or ImageField? attributes do not function properly. The get_FOO_filename() and get_FOO_url() methods fail to insert the upload_to path if they are accessed before a call to save_FOO_image(). This happens because _get_FIELD_filename() and _get_FIELD_url() do not call field.get_filename(). I've attached a patch to resolve the issue.

Attachments

get_filename_fix.diff (0.9 kB) - added by wreese@gmail.com on 09/27/07 00:09:19.
patch-5619-with-tests.diff (2.7 kB) - added by leahculver on 07/18/08 16:44:52.
added tests for previous patch

Change History

09/27/07 00:09:19 changed by wreese@gmail.com

  • attachment get_filename_fix.diff added.

09/27/07 16:57:00 changed by SmileyChris

  • needs_tests set to 1.

12/11/07 12:02:08 changed by Gulopine

  • keywords set to fs-rf.

12/17/07 20:57:33 changed by Gulopine

  • keywords changed from fs-rf to fs-rf-fixed.

02/28/08 11:14:28 changed by jacob

  • stage changed from Unreviewed to Accepted.

06/16/08 13:30:11 changed by Gulopine

  • milestone set to 1.0 beta.

07/18/08 13:56:26 changed by leahculver

  • owner changed from nobody to leahculver.
  • status changed from new to assigned.

07/18/08 15:05:00 changed by Gulopine

Sorry for not explaining this properly already, but the fs-rf-fixed keyword on this ticket indicates that this fix has already been rolled into #5361.

07/18/08 16:44:52 changed by leahculver

  • attachment patch-5619-with-tests.diff added.

added tests for previous patch

07/18/08 16:46:00 changed by leahculver

  • stage changed from Accepted to Ready for checkin.

Malcolm reviewed.

07/19/08 13:35:12 changed by mtredinnick

  • status changed from assigned to closed.
  • resolution set to fixed.

(In [7986]) Fixed #5619 -- Return the same path in get_FOO_filename() before and after a model is saved (i.e. make sure the upload prefix is prepended in both cases).

Patch from wreese@gmail.com. Tests from Leah Culver.

07/19/08 16:15:25 changed by carljm

  • status changed from closed to reopened.
  • resolution deleted.

This patch is totally broken, and breaks get_FIELD_url for all files or images uploaded prior to the current day, because field.get_directory_name uses datetime.now() to interpolate the date values. [7986] needs to be reverted as soon as possible.

07/19/08 16:20:24 changed by Karen Tracey <kmtracey@gmail.com>

You do know you can get your own copy reverted back to pre-7986 by doing something like:

svn -r7985 up

right? So you can get your code back to working immediately without the change actually being reverted instantaneously.

07/19/08 16:29:39 changed by carljm

Of course, already done - but only after too much time spent looking for the problem. Not trying to be a pain, just trying to save others the same hassle.

07/19/08 16:34:46 changed by mtredinnick

  • status changed from reopened to closed.
  • resolution set to fixed.

Since you've already opened another ticket (#7843) for the new bug, how about now also reopening this one (which does fix another problem)? It only requires one ticket. I'm sorry it's had an unintended side-effect and I'll look at it very shortly, but for now, just use an earlier revision for a little bit.

07/19/08 16:37:32 changed by carljm

My bad - guess I assumed that that if the fix here wasn't workable, this ticket would still need attention as well. Sorry for the hassle.

07/19/08 16:43:59 changed by mtredinnick

you could have reopened this. There's just no need for two tickets (the fix here needs changing, not reverting, since it fixes a real problem, as can be seen from the patch). Anyway, we're on the same page now. It'll be fixed soon.

07/19/08 17:26:33 changed by mtredinnick

(In [7998]) Reverted [7986].

It turns out that we need to treat filename creation/display (in particular, the upload_to path) differently depending upon whether the value is out of the database or provided by other code and there's no reliable way to determine that at the moment (although some later proposed changes might alter that). So calling get_FIELD_filename on an unsaved model with a changed file attribute will not necessarily return the same result as after the save().

Refs #5619. Fixed #7843.

07/19/08 17:26:46 changed by mtredinnick

(In [7999]) Added a cavaet to the use of get_FOO_filename() and get_FOO_url(). This constraint has always existed, but it's very hard to fix in the current code, so better to work around it for now.

Refs #5619

07/19/08 17:29:54 changed by mtredinnick

  • status changed from closed to reopened.
  • resolution deleted.
  • stage changed from Ready for checkin to Someday/Maybe.

As noted in the above commits, there are technical reasons why it's not really possible to fix this in a straightforward fashion. Tracking when an attribute is updated by code (as opposed to a database read) is something we might add in the future, in which case, this problem could be addressed at that point. For now, the simplest and practical solution is "don't do that". The model is going to be saved in any case, so save it first, then read off the filename.

Reopening and moving to Someday/Maybe.

07/19/08 17:43:32 changed by Gulopine

  • keywords deleted.
  • milestone deleted.

Yeah, I'm actually not sure now why I had put fs-rf-fixed on this one. When I add Leah's tests to the #5361 patch, it fails quite reliably, so it's clearly still an issue. I'll keep it in my head and see what happens if/when we get that potential future differentiation between db-load and otherwise. Removing all fs-rf tags, as well as the 1.0 beta milestone, since it clearly won't get taken care of in the immediate future.

The one thing I'd note on this is that the docs probably shouldn't say "*if* you are using upload_to," since it's actually a required argument on all FileFields. That requirement isn't going away after #5361 either, so it's still important to make it clear.


Add/Change #5619 (FileField and ImageField return the wrong path/url before calling save_FOO_file())




Change Properties
Action