Opened 18 years ago

Closed 17 years ago

#2923 closed defect (fixed)

[patch] FileField does not allow modifications on Windows

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

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@… 17 years ago.
Strip forward slash from "field_data" in "isWithinMediaRoot".

Download all attachments as: .zip

Change History (8)

comment:1 by Chris Beaven, 18 years ago

Summary: FileField does not allow modifications on Windows[patch] FileField does not allow modifications on Windows

Just marking that this contains a patch.

comment:2 by Adrian Holovaty, 18 years ago

priority: highestnormal
Severity: blockernormal

comment:3 by anonymous, 18 years ago

Resolution: fixed
Status: newclosed

comment:4 by Chris Beaven, 18 years ago

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

This was fixed in [4036].

comment:5 by (none), 18 years ago

milestone: Version 1.0

Milestone Version 1.0 deleted

comment:6 by justin.driscoll@…, 17 years ago

Patch needs improvement: set
Resolution: fixed
Status: closedreopened

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.

by justin.driscoll@…, 17 years ago

Attachment: 2923.patch added

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

comment:7 by Malcolm Tredinnick, 17 years ago

Resolution: fixed
Status: reopenedclosed

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.

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