Ticket #3297: 4490.2.patch
File 4490.2.patch, 5.0 KB (added by , 18 years ago) |
---|
-
django/db/models/fields/__init__.py
652 652 else: 653 653 func(new_data[upload_field_name]["filename"], new_data[upload_field_name]["content"]) 654 654 655 def formfield_save_file(self, field_name, new_data, new_object): 656 func = getattr(new_object, 'save_%s_file' % self.name) 657 func(new_data[field_name][0]["filename"], new_data[field_name][0]["content"]) 658 655 659 def get_directory_name(self): 656 660 return os.path.normpath(datetime.datetime.now().strftime(self.upload_to)) 657 661 … … 660 664 f = os.path.join(self.get_directory_name(), get_valid_filename(os.path.basename(filename))) 661 665 return os.path.normpath(f) 662 666 667 def formfield(self, **kwargs): 668 defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'widget':forms.FileInput} 669 defaults.update(kwargs) 670 return forms.FileField(**defaults) 671 663 672 class FilePathField(Field): 664 673 def __init__(self, verbose_name=None, name=None, path='', match=None, recursive=False, **kwargs): 665 674 self.path, self.match, self.recursive = path, match, recursive -
django/newforms/models.py
13 13 14 14 This method is created for any form_for_model Form. 15 15 """ 16 from django.db import models 16 17 if self.errors: 17 18 raise ValueError("The %s could not be created because the data didn't validate." % self._model._meta.object_name) 18 19 return save_instance(self, self._model(), commit) … … 33 34 for f in opts.fields: 34 35 if isinstance(f, models.AutoField): 35 36 continue 37 if isinstance(f,models.FileField): 38 continue 36 39 setattr(instance, f.attname, clean_data[f.name]) 37 40 if commit: 38 41 instance.save() 39 42 for f in opts.many_to_many: 40 43 setattr(instance, f.attname, clean_data[f.name]) 44 45 for f in opts.fields: 46 if isinstance(f, models.FileField): 47 f.formfield_save_file(f.name, clean_data, instance) 41 48 # GOTCHA: If many-to-many data is given and commit=False, the many-to-many 42 49 # data will be lost. This happens because a many-to-many options cannot be 43 50 # set on an object until after it's saved. Maybe we should raise an -
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', … … 107 107 if self.max_length is not None and isinstance(widget, (TextInput, PasswordInput)): 108 108 return {'maxlength': str(self.max_length)} 109 109 110 class FileField(Field): 111 def __init__(self,required=True,widget=None,label=None,initial=None): 112 super(FileField,self).__init__(required,widget,label,initial) 113 114 def clean(self, value): 115 super(FileField,self).clean(value) 116 if self.required: 117 if value[0]: 118 if not len(value[0]['content'])>0: 119 raise ValidationError(gettext(u'This field is required')) 120 else: 121 if not value[1]: 122 raise ValidationError(gettext(u'This field is required')) 123 return value 124 110 125 class IntegerField(Field): 111 126 def __init__(self, max_value=None, min_value=None, *args, **kwargs): 112 127 self.max_value, self.min_value = max_value, min_value -
django/newforms/widgets.py
107 107 108 108 class FileInput(Input): 109 109 input_type = 'file' 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)] 110 126 111 127 class Textarea(Widget): 112 128 def render(self, name, value, attrs=None):