Ticket #3297: fixed-quickie-file-uploads.diff
File fixed-quickie-file-uploads.diff, 5.7 KB (added by , 18 years ago) |
---|
-
django/db/models/fields/__init__.py
660 660 f = os.path.join(self.get_directory_name(), get_valid_filename(os.path.basename(filename))) 661 661 return os.path.normpath(f) 662 662 663 def formfield(self, **kwargs): 664 defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'widget':forms.FileInput} 665 defaults.update(kwargs) 666 return forms.FileField(**defaults) 667 668 663 669 class FilePathField(Field): 664 670 def __init__(self, verbose_name=None, name=None, path='', match=None, recursive=False, **kwargs): 665 671 self.path, self.match, self.recursive = path, match, recursive -
django/newforms/models.py
13 0 14 1 This method is created for any form_for_model Form. 15 2 """ 3 from django.db import models 4 16 5 if self.errors: 17 6 raise ValueError("The %s could not be created because the data didn't validate." % self._model._meta.object_name) 18 obj = self._model(**self.clean_data) 7 # The default initializer does not understand FileField data, we need to intercept. 8 init_data = self.clean_data.copy() 9 extra_data = {} 10 11 # Strip out data the model initializer can't handle, saving it in extra_data 12 opts = self._model._meta 13 for f in opts.fields: 14 if isinstance(f, models.FileField): 15 extra_data[f.name] = init_data.pop(f.name, [None, '']) 16 17 obj = self._model(**init_data) 18 19 19 if commit: 20 20 obj.save() 21 22 # Now process that data we stripped out. 23 # (We do this after the object is saved because we want to ensure the object 24 # gets saved before we save the file. Otherwise it could become orphaned.) 25 for f in opts.fields: 26 if isinstance(f, models.FileField): 27 file_data, path_val_data = extra_data[f.name] 28 # only save if there is a file 29 if file_data is not None: 30 func = getattr(obj, 'save_%s_file' % f.name) 31 func(file_data['filename'], file_data['content']) 32 21 33 return obj 22 34 23 35 def save_instance(form, instance, commit=True): … … 36 48 for f in opts.fields + opts.many_to_many: 37 49 if isinstance(f, models.AutoField): 38 50 continue 39 setattr(instance, f.attname, clean_data[f.name]) 51 # Skip FileFields, we don't want to process the upload yet, and that's 52 # the only way to change the value anyways. 53 if isinstance(f, models.FileField) and commit: 54 continue 55 setattr(instance, f.name, clean_data[f.name]) 40 56 if commit: 41 57 instance.save() 58 59 # Now save any uploaded files. 60 for f in opts.fields: 61 if isinstance(f, models.FileField): 62 file_data = clean_data[f.name][0] 63 # if there was an upload, save the file 64 if file_data is not None: 65 func = getattr(instance, 'save_%s_file' % f.name) 66 func(file_data['filename'], file_data['content']) 67 # otherwise we just want to leave the value intact... 68 # (but if we did want to set it, it is in clean_data[f.name][1] 69 42 70 return instance 43 71 44 72 def make_instance_save(instance): -
django/newforms/fields.py
10 10 import time 11 11 12 12 __all__ = ( 13 'Field', 'CharField', ' IntegerField',13 'Field', 'CharField', 'FileField', 'IntegerField', 14 14 'DEFAULT_DATE_INPUT_FORMATS', 'DateField', 15 15 'DEFAULT_TIME_INPUT_FORMATS', 'TimeField', 16 16 'DEFAULT_DATETIME_INPUT_FORMATS', 'DateTimeField', … … 105 105 if self.max_length is not None and isinstance(widget, (TextInput, PasswordInput)): 106 106 return {'maxlength': str(self.max_length)} 107 107 108 class FileField(Field): 109 def __init__(self, required=True, widget=None, label=None, initial=None): 110 super(FileField, self).__init__(required, widget, label, initial) 111 112 def clean(self, value): 113 super(FileField, self).clean(value) 114 return value 115 108 116 class IntegerField(Field): 109 117 def __init__(self, max_value=None, min_value=None, required=True, widget=None, label=None, initial=None): 110 118 self.max_value, self.min_value = max_value, min_value -
django/newforms/widgets.py
108 108 class FileInput(Input): 109 109 input_type = 'file' 110 110 111 def render(self, name, value, attrs=None, choices=()): 112 if value is None: value = '' 113 file_attrs = self.build_attrs(attrs, type='file', name=name+'_file') 114 final_attrs = self.build_attrs(attrs, type='hidden', name=name) 115 if value != '': final_attrs['value'] = smart_unicode(value) # Only add the 'value' attribute if a value is non-empty. 116 117 if value != '': 118 currently = u'Currently: %s<br />Change: ' % smart_unicode(value) 119 else: 120 currently = u'' 121 122 return u'%s<input %s /> <input%s />' % (currently, flatatt(file_attrs), flatatt(final_attrs)) 123 124 def value_from_datadict(self, data, name): 125 return [data.get(name+'_file', None), data.get(name, None)] 126 127 111 128 class Textarea(Widget): 112 129 def render(self, name, value, attrs=None): 113 130 if value is None: value = ''