Code

Ticket #5386: newforms.content_type.diff

File newforms.content_type.diff, 1.8 KB (added by Ionut Ciocirlan <ionut.ciocirlan@…>, 6 years ago)
Line 
1Index: django/newforms/fields.py
2===================================================================
3--- django/newforms/fields.py   (revision 7397)
4+++ django/newforms/fields.py   (working copy)
5@@ -416,9 +416,10 @@
6 
7 class UploadedFile(StrAndUnicode):
8     "A wrapper for files uploaded in a FileField"
9-    def __init__(self, filename, content):
10+    def __init__(self, filename, content, content_type):
11         self.filename = filename
12         self.content = content
13+        self.content_type = content_type
14 
15     def __unicode__(self):
16         """
17@@ -445,7 +446,7 @@
18         elif not data and initial:
19             return initial
20         try:
21-            f = UploadedFile(data['filename'], data['content'])
22+            f = UploadedFile(data['filename'], data['content'], data['content-type'])
23         except TypeError:
24             raise ValidationError(self.error_messages['invalid'])
25         except KeyError:
26Index: docs/newforms.txt
27===================================================================
28--- docs/newforms.txt   (revision 7397)
29+++ docs/newforms.txt   (working copy)
30@@ -1340,11 +1340,18 @@
31                             client.
32                             
33     ``content``             The array of bytes comprising the file content.
34+                           
35+    ``content_type``        The Content-Type of the file as submitted by the
36+                            browser.
37     ======================  ====================================================
38 
39 The string representation of an ``UploadedFile`` is the same as the filename
40 attribute.
41 
42+You can perform basic validation against the ``content_type`` if you need to
43+allow only uploads of specific files. Note that this is however unreliable,
44+since the data is submitted by the browser.
45+
46 When you use a ``FileField`` on a form, you must also remember to
47 `bind the file data to the form`_.
48