Code

Opened 8 years ago

Closed 6 years ago

#2923 closed defect (fixed)

[patch] FileField does not allow modifications on Windows

Reported by: radek Owned by: adrian
Component: contrib.admin Version: master
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: UI/UX:

Description

When you have FileField (or ImageField) and use Django on Windows, second time uploaded filename is always considered invalid.
Ie. you can create one instance, but you can't go back and edit this one and save. You always get:

Enter a valid filename.

The issue is in

\django\trunk\django\db\models\fields\init.py on line 579

Because
os.path.abspath(os.path.join(settings.MEDIA_ROOT, field_data)):

will give:
'D:
www
myproject
media
subfolder
1-icon.png'

while this
os.path.normpath(settings.MEDIA_ROOT):

gives:

'
www
myproject
media'

You can see missing Drive name.

Patch is here:

@@ -576,7 +576,7 @@
         # If the raw path is passed in, validate it's under the MEDIA_ROOT.
         def isWithinMediaRoot(field_data, all_data):
             f = os.path.abspath(os.path.join(settings.MEDIA_ROOT, field_data))
-            if not f.startswith(os.path.normpath(settings.MEDIA_ROOT)):
+            if not f.startswith(os.path.abspath(os.path.normpath(settings.MEDIA_ROOT))):
                 raise validators.ValidationError, _("Enter a valid filename.")
         field_list[1].validator_list.append(isWithinMediaRoot)
         return field_list

Pitty I spent a whole day, before I found it.

Attachments (1)

2923.patch (713 bytes) - added by justin.driscoll@… 6 years ago.
Strip forward slash from "field_data" in "isWithinMediaRoot".

Download all attachments as: .zip

Change History (8)

comment:1 Changed 8 years ago by SmileyChris

  • Summary changed from FileField does not allow modifications on Windows to [patch] FileField does not allow modifications on Windows

Just marking that this contains a patch.

comment:2 Changed 7 years ago by adrian

  • priority changed from highest to normal
  • Severity changed from blocker to normal

comment:3 Changed 7 years ago by anonymous

  • Resolution set to fixed
  • Status changed from new to closed

comment:4 Changed 7 years ago by SmileyChris

Just confirming the fix since it's unusual that tickets are closed by anonymous.

This was fixed in [4036].

comment:5 Changed 7 years ago by anonymous

  • milestone Version 1.0 deleted

Milestone Version 1.0 deleted

comment:6 Changed 6 years ago by justin.driscoll@…

  • Patch needs improvement set
  • Resolution fixed deleted
  • Status changed from closed to reopened

The current patch did not solve what seems to be the same problem for me. A forward slash appended to the beginning "field_data" was causing "os.path.abspath(os.path.join(settings.MEDIA_ROOT, field_data))" to return an incorrect path and raise a validation error.

Changed 6 years ago by justin.driscoll@…

Strip forward slash from "field_data" in "isWithinMediaRoot".

comment:7 Changed 6 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from reopened to closed

Justin, please open a new ticket with your problem and an explanation of how to repeat it. That will help use keep comments on the new change separate from the history for this ticket.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.