Code

Ticket #7614: second_patch.diff

File second_patch.diff, 6.8 KB (added by axiak, 6 years ago)

Second patch...

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