Ticket #3297: diff.txt

File diff.txt, 4.4 KB (added by anonymous, 12 years ago)

diff file with what you have to do to patch v4457

Line 
1Index: django/db/models/fields/__init__.py
2===================================================================
3--- django/db/models/fields/__init__.py (revision 4457)
4+++ django/db/models/fields/__init__.py (working copy)
5@@ -660,6 +660,11 @@
6         f = os.path.join(self.get_directory_name(), get_valid_filename(os.path.basename(filename)))
7         return os.path.normpath(f)
8 
9+    def formfield(self, **kwargs):
10+        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'widget':forms.FileInput}
11+        defaults.update(kwargs)
12+        return forms.FileField(**defaults)
13+
14 class FilePathField(Field):
15     def __init__(self, verbose_name=None, name=None, path='', match=None, recursive=False, **kwargs):
16         self.path, self.match, self.recursive = path, match, recursive
17Index: django/newforms/models.py
18===================================================================
19--- django/newforms/models.py   (revision 4457)
20+++ django/newforms/models.py   (working copy)
21@@ -13,6 +13,7 @@
22 
23     This method is created for any form_for_model Form.
24     """
25+    from django.db import models
26     if self.errors:
27         raise ValueError("The %s could not be created because the data didn't validate." % self._model._meta.object_name)
28     return save_instance(self, self._model(), commit)
29@@ -33,11 +34,20 @@
30     for f in opts.fields:
31         if isinstance(f, models.AutoField):
32             continue
33+        if isinstance(f,models.FileField):
34+            continue
35         setattr(instance, f.attname, clean_data[f.name])
36     if commit:
37         instance.save()
38         for f in opts.many_to_many:
39             setattr(instance, f.attname, clean_data[f.name])
40+       
41+        for f in opts.fields:
42+            if isinstance(f, models.FileField):
43+                file_data = clean_data[f.name][0]
44+                if file_data is not None:
45+                    func = getattr(instance,'save_%s_file' % f.name)
46+                    func(file_data['filename'], file_data['content'])
47     # GOTCHA: If many-to-many data is given and commit=False, the many-to-many
48     # data will be lost. This happens because a many-to-many options cannot be
49     # set on an object until after it's saved. Maybe we should raise an
50Index: django/newforms/fields.py
51===================================================================
52--- django/newforms/fields.py   (revision 4457)
53+++ django/newforms/fields.py   (working copy)
54@@ -10,7 +10,7 @@
55 import time
56 
57 __all__ = (
58-    'Field', 'CharField', 'IntegerField',
59+    'Field', 'CharField', 'FileField', 'IntegerField',
60     'DEFAULT_DATE_INPUT_FORMATS', 'DateField',
61     'DEFAULT_TIME_INPUT_FORMATS', 'TimeField',
62     'DEFAULT_DATETIME_INPUT_FORMATS', 'DateTimeField',
63@@ -107,6 +107,14 @@
64         if self.max_length is not None and isinstance(widget, (TextInput, PasswordInput)):
65             return {'maxlength': str(self.max_length)}
66 
67+class FileField(Field):
68+    def __init__(self,required=True,widget=None,label=None,initial=None):
69+        super(FileField,self).__init__(required,widget,label,initial)
70+   
71+    def clean(self, value):
72+        super(FileField,self).clean(value)
73+        return value
74+
75 class IntegerField(Field):
76     def __init__(self, max_value=None, min_value=None, *args, **kwargs):
77         self.max_value, self.min_value = max_value, min_value
78Index: django/newforms/widgets.py
79===================================================================
80--- django/newforms/widgets.py  (revision 4457)
81+++ django/newforms/widgets.py  (working copy)
82@@ -107,6 +107,22 @@
83 
84 class FileInput(Input):
85     input_type = 'file'
86+   
87+    def render(self,name,value,attrs=None,choices=()):
88+        if value is None: value = ''
89+        file_attrs = self.build_attrs(attrs, type='file',name=name+'_file')
90+        final_attrs = self.build_attrs(attrs, type='hidden', name=name)
91+        if value != '': final_attrs['value'] = smart_unicode(value) # only add the value attribute if a value is non-empty
92+       
93+        if value != '':
94+            currently = u'Currently: %s<br/>Change: ' % smart_unicode(value)
95+        else:
96+            currently = u''
97+       
98+        return u'%s<input %s /> <input%s />' % (currently,flatatt(file_attrs),flatatt(final_attrs))
99+   
100+    def value_from_datadict(self,data,name):
101+        return [data.get(name+'_file',None),data.get(name,None)]
102 
103 class Textarea(Widget):
104     def render(self, name, value, attrs=None):
Back to Top