Code

Ticket #7614: initial_patch.diff

File initial_patch.diff, 6.2 KB (added by axiak, 6 years ago)

Initial patch, with doc and test changes.

Line 
1Index: django/db/models/fields/__init__.py
2===================================================================
3--- django/db/models/fields/__init__.py (revision 7827)
4+++ django/db/models/fields/__init__.py (working copy)
5@@ -766,9 +766,13 @@
6     def get_db_prep_save(self, value):
7         "Returns field's value prepared for saving into a database."
8         # Need to convert UploadedFile objects provided via a form to unicode for database insertion
9-        if value is None:
10+        if hasattr(value, 'file_name'):
11+            raise Exception("Yep %s" % value)
12+            return value.file_name
13+        elif value is None:
14             return None
15-        return unicode(value)
16+        else:
17+            return unicode(value)
18 
19     def get_manipulator_fields(self, opts, manipulator, change, name_prefix='', rel=False, follow=True):
20         field_list = Field.get_manipulator_fields(self, opts, manipulator, change, name_prefix, rel, follow)
21@@ -857,9 +861,9 @@
22         return os.path.normpath(f)
23 
24     def save_form_data(self, instance, data):
25-        from django.newforms.fields import UploadedFile
26+        from django.core.files.uploadedfile import UploadedFile
27         if data and isinstance(data, UploadedFile):
28-            getattr(instance, "save_%s_file" % self.name)(data.filename, data.data, save=False)
29+            getattr(instance, "save_%s_file" % self.name)(data.file_name, data, save=False)
30 
31     def formfield(self, **kwargs):
32         defaults = {'form_class': forms.FileField}
33Index: django/newforms/fields.py
34===================================================================
35--- django/newforms/fields.py   (revision 7827)
36+++ django/newforms/fields.py   (working copy)
37@@ -27,8 +27,8 @@
38 
39 from util import ErrorList, ValidationError
40 from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple, DateTimeInput
41+from django.core.files.uploadedfile import SimpleUploadedFile
42 
43-
44 __all__ = (
45     'Field', 'CharField', 'IntegerField',
46     'DEFAULT_DATE_INPUT_FORMATS', 'DateField',
47@@ -460,23 +460,20 @@
48                 category = DeprecationWarning,
49                 stacklevel = 2
50             )
51+            data = SimpleUploadedFile(data['filename'], data['content'])
52 
53         try:
54             file_name = data.file_name
55             file_size = data.file_size
56         except AttributeError:
57-            try:
58-                file_name = data.get('filename')
59-                file_size = bool(data['content'])
60-            except (AttributeError, KeyError):
61-                raise ValidationError(self.error_messages['invalid'])
62+            raise ValidationError(self.error_messages['invalid'])
63 
64         if not file_name:
65             raise ValidationError(self.error_messages['invalid'])
66         if not file_size:
67             raise ValidationError(self.error_messages['empty'])
68 
69-        return UploadedFile(file_name, data)
70+        return data
71 
72 class ImageField(FileField):
73     default_error_messages = {
74Index: tests/modeltests/model_forms/models.py
75===================================================================
76--- tests/modeltests/model_forms/models.py      (revision 7827)
77+++ tests/modeltests/model_forms/models.py      (working copy)
78@@ -803,7 +803,7 @@
79 >>> f.is_valid()
80 True
81 >>> type(f.cleaned_data['file'])
82-<class 'django.newforms.fields.UploadedFile'>
83+<class 'django.core.files.uploadedfile.SimpleUploadedFile'>
84 >>> instance = f.save()
85 >>> instance.file
86 u'...test1.txt'
87@@ -814,7 +814,7 @@
88 >>> f.is_valid()
89 True
90 >>> type(f.cleaned_data['file'])
91-<class 'django.newforms.fields.UploadedFile'>
92+<class 'django.core.files.uploadedfile.SimpleUploadedFile'>
93 >>> instance = f.save()
94 >>> instance.file
95 u'...test1.txt'
96@@ -906,7 +906,7 @@
97 >>> f.is_valid()
98 True
99 >>> type(f.cleaned_data['image'])
100-<class 'django.newforms.fields.UploadedFile'>
101+<class 'django.core.files.uploadedfile.SimpleUploadedFile'>
102 >>> instance = f.save()
103 >>> instance.image
104 u'...test.png'
105@@ -918,7 +918,7 @@
106 >>> f.is_valid()
107 True
108 >>> type(f.cleaned_data['image'])
109-<class 'django.newforms.fields.UploadedFile'>
110+<class 'django.core.files.uploadedfile.SimpleUploadedFile'>
111 >>> instance = f.save()
112 >>> instance.image
113 u'...test.png'
114Index: tests/regressiontests/forms/fields.py
115===================================================================
116--- tests/regressiontests/forms/fields.py       (revision 7827)
117+++ tests/regressiontests/forms/fields.py       (working copy)
118@@ -800,10 +800,10 @@
119 ValidationError: [u'The submitted file is empty.']
120 
121 >>> type(f.clean(SimpleUploadedFile('name', 'Some File Content')))
122-<class 'django.newforms.fields.UploadedFile'>
123+<class 'django.core.files.uploadedfile.SimpleUploadedFile'>
124 
125 >>> type(f.clean(SimpleUploadedFile('name', 'Some File Content'), 'files/test4.pdf'))
126-<class 'django.newforms.fields.UploadedFile'>
127+<class 'django.core.files.uploadedfile.SimpleUploadedFile'>
128 
129 # URLField ##################################################################
130 
131Index: docs/newforms.txt
132===================================================================
133--- docs/newforms.txt   (revision 7827)
134+++ docs/newforms.txt   (working copy)
135@@ -1331,23 +1331,12 @@
136     * Validates that non-empty file data has been bound to the form.
137     * Error message keys: ``required``, ``invalid``, ``missing``, ``empty``
138 
139-An ``UploadedFile`` object has two attributes:
140+To learn more about the ``UploadedFile`` object, see the `file uploads documentation`_.
141 
142-    ======================  ====================================================
143-    Attribute               Description
144-    ======================  ====================================================
145-    ``filename``            The name of the file, provided by the uploading
146-                            client.
147-                           
148-    ``content``             The array of bytes comprising the file content.
149-    ======================  ====================================================
150-
151-The string representation of an ``UploadedFile`` is the same as the filename
152-attribute.
153-
154 When you use a ``FileField`` on a form, you must also remember to
155 `bind the file data to the form`_.
156 
157+.. _file uploads documentation: ../upload_handling/
158 .. _`bind the file data to the form`: `Binding uploaded files to a form`_
159 
160 ``FilePathField``