Ticket #3297: fixed2-quickie-file-uploads.diff

File fixed2-quickie-file-uploads.diff, 5.6 KB (added by Andrew Sutherland <andrew@…>, 12 years ago)

remove additional commit conditional check that shouldn't have existed

  • django/db/models/fields/__init__.py

     
    660660        f = os.path.join(self.get_directory_name(), get_valid_filename(os.path.basename(filename)))
    661661        return os.path.normpath(f)
    662662
     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
    663669class FilePathField(Field):
    664670    def __init__(self, verbose_name=None, name=None, path='', match=None, recursive=False, **kwargs):
    665671        self.path, self.match, self.recursive = path, match, recursive
  • django/newforms/models.py

     
    130
    141    This method is created for any form_for_model Form.
    152    """
     3    from django.db import models
     4
    165    if self.errors:
    176        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
    1919    if commit:
    2020        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
    2133    return obj
    2234
    2335def save_instance(form, instance, commit=True):
     
    3648    for f in opts.fields + opts.many_to_many:
    3749        if isinstance(f, models.AutoField):
    3850            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):
     54            continue
     55        setattr(instance, f.name, clean_data[f.name])
    4056    if commit:
    4157        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
    4270    return instance
    4371
    4472def make_instance_save(instance):
  • django/newforms/fields.py

     
    1010import time
    1111
    1212__all__ = (
    13     'Field', 'CharField', 'IntegerField',
     13    'Field', 'CharField', 'FileField', 'IntegerField',
    1414    'DEFAULT_DATE_INPUT_FORMATS', 'DateField',
    1515    'DEFAULT_TIME_INPUT_FORMATS', 'TimeField',
    1616    'DEFAULT_DATETIME_INPUT_FORMATS', 'DateTimeField',
     
    105105        if self.max_length is not None and isinstance(widget, (TextInput, PasswordInput)):
    106106            return {'maxlength': str(self.max_length)}
    107107
     108class 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
    108116class IntegerField(Field):
    109117    def __init__(self, max_value=None, min_value=None, required=True, widget=None, label=None, initial=None):
    110118        self.max_value, self.min_value = max_value, min_value
  • django/newforms/widgets.py

     
    108108class FileInput(Input):
    109109    input_type = 'file'
    110110
     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
    111128class Textarea(Widget):
    112129    def render(self, name, value, attrs=None):
    113130        if value is None: value = ''
Back to Top