Ticket #3297: 3297.patch
File 3297.patch, 4.4 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 663 668 class FilePathField(Field): 664 669 def __init__(self, verbose_name=None, name=None, path='', match=None, recursive=False, **kwargs): 665 670 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 file_data = clean_data[f.name][0] 48 if file_data is not None: 49 func = getattr(instance,'save_%s_file' % f.name) 50 func(file_data['filename'], file_data['content']) 41 51 # GOTCHA: If many-to-many data is given and commit=False, the many-to-many 42 52 # data will be lost. This happens because a many-to-many options cannot be 43 53 # 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 return value 117 110 118 class IntegerField(Field): 111 119 def __init__(self, max_value=None, min_value=None, *args, **kwargs): 112 120 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):