Ticket #3297: quickie-file-uploads.diff
File quickie-file-uploads.diff, 6.2 KB (added by , 18 years ago) |
---|
-
django/db/models/fields/__init__.py
660 665 f = os.path.join(self.get_directory_name(), get_valid_filename(os.path.basename(filename))) 661 666 return os.path.normpath(f) 662 667 668 def formfield(self, **kwargs): 669 defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'widget':forms.FileInput} 670 defaults.update(kwargs) 671 return forms.FileField(**defaults) 672 673 663 674 class FilePathField(Field): 664 675 def __init__(self, verbose_name=None, name=None, path='', match=None, recursive=False, **kwargs): 665 676 self.path, self.match, self.recursive = path, match, recursive … … 710 721 empty_strings_allowed = False 711 722 def get_manipulator_field_objs(self): 712 723 return [oldforms.IntegerField] 724 725 def to_python(self, value): 726 if value is None: 727 return value 728 try: 729 return int(value) 730 except (TypeError, ValueError): 731 raise validators.ValidationError, gettext("This value must be an integer.") 713 732 714 733 def formfield(self, **kwargs): 715 734 defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name)} -
django/newforms/models.py
13 18 14 19 This method is created for any form_for_model Form. 15 20 """ 21 from django.db import models 22 16 23 if self.errors: 17 24 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) 25 # The default initializer does not understand FileField data, we need to intercept. 26 init_data = self.clean_data.copy() 27 extra_data = {} 28 29 # Strip out data the model initializer can't handle, saving it in extra_data 30 opts = self._model._meta 31 for f in opts.fields: 32 if isinstance(f, models.FileField): 33 extra_data[f.name] = init_data.pop(f.name, [None, '']) 34 35 obj = self._model(**init_data) 36 19 37 if commit: 20 38 obj.save() 39 40 # Now process that data we stripped out. 41 # (We do this after the object is saved because we want to ensure the object 42 # gets saved before we save the file. Otherwise it could become orphaned.) 43 for f in opts.fields: 44 if isinstance(f, models.FileField): 45 file_data, path_val_data = extra_data[f.name] 46 # only save if there is a file 47 if file_data is not None: 48 func = getattr(obj, 'save_%s_file' % f.name) 49 func(file_data['filename'], file_data['content']) 50 21 51 return obj 22 52 23 53 def save_instance(form, instance, commit=True): … … 36 66 for f in opts.fields + opts.many_to_many: 37 67 if isinstance(f, models.AutoField): 38 68 continue 39 setattr(instance, f.attname, clean_data[f.name]) 69 # Skip FileFields, we don't want to process the upload yet, and that's 70 # the only way to change the value anyways. 71 if isinstance(f, models.FileField) and commit: 72 continue 73 setattr(instance, f.name, clean_data[f.name]) 40 74 if commit: 41 75 instance.save() 76 77 # Now save any uploaded files. 78 for f in opts.fields: 79 if isinstance(f, models.FileField): 80 file_data = clean_data[f.name][0] 81 # if there was an upload, save the file 82 if file_data is not None: 83 func = getattr(instance, 'save_%s_file' % f.name) 84 func(file_data['filename'], file_data['content']) 85 # otherwise we just want to leave the value intact... 86 # (but if we did want to set it, it is in clean_data[f.name][1] 87 42 88 return instance 43 89 44 90 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 = ''