Ticket #3297: 4549-with-tests-and-imagefield.patch

File 4549-with-tests-and-imagefield.patch, 9.2 KB (added by Øyvind Saltvik <oyvind.saltvik@…>, 18 years ago)

ImageField with validation added

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

     
    658658            else:
    659659                func(new_data[upload_field_name]["filename"], new_data[upload_field_name]["content"])
    660660
     661    def formfield_save_file(self, field_name, new_data, new_object):
     662        func = getattr(new_object, 'save_%s_file' % self.name)
     663        func(new_data[field_name][0]["filename"], new_data[field_name][0]["content"])
     664
    661665    def get_directory_name(self):
    662666        return os.path.normpath(datetime.datetime.now().strftime(self.upload_to))
    663667
     
    666670        f = os.path.join(self.get_directory_name(), get_valid_filename(os.path.basename(filename)))
    667671        return os.path.normpath(f)
    668672
     673    def formfield(self, **kwargs):
     674        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'widget': forms.FileInput}
     675        defaults.update(kwargs)
     676        return forms.FileField(**defaults)
     677
    669678class FilePathField(Field):
    670679    def __init__(self, verbose_name=None, name=None, path='', match=None, recursive=False, **kwargs):
    671680        self.path, self.match, self.recursive = path, match, recursive
     
    712721                setattr(new_object, self.height_field, getattr(original_object, self.height_field))
    713722            new_object.save()
    714723
     724    def formfield(self, **kwargs):
     725        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'widget': forms.FileInput}
     726        defaults.update(kwargs)
     727        return forms.ImageField(**defaults)
     728
    715729class IntegerField(Field):
    716730    empty_strings_allowed = False
    717731    def get_manipulator_field_objs(self):
  • django/newforms/models.py

     
    1616
    1717    This method is created for any form_for_model Form.
    1818    """
     19    from django.db import models
    1920    if self.errors:
    2021        raise ValueError("The %s could not be created because the data didn't validate." % self._model._meta.object_name)
    2122    return save_instance(self, self._model(), commit)
     
    3637    for f in opts.fields:
    3738        if not f.editable or isinstance(f, models.AutoField):
    3839            continue
     40        if isinstance(f,models.FileField):
     41            continue
    3942        setattr(instance, f.name, clean_data[f.name])
    4043    if commit:
    4144        instance.save()
    4245        for f in opts.many_to_many:
    4346            setattr(instance, f.attname, clean_data[f.name])
     47        for f in opts.fields:
     48            if isinstance(f, models.FileField):
     49                f.formfield_save_file(f.name, clean_data, instance)
    4450    # GOTCHA: If many-to-many data is given and commit=False, the many-to-many
    4551    # data will be lost. This happens because a many-to-many options cannot be
    4652    # set on an object until after it's saved. Maybe we should raise an
  • django/newforms/fields.py

     
    1010import time
    1111
    1212__all__ = (
    13     'Field', 'CharField', 'IntegerField',
     13    'Field', 'CharField', 'FileField', 'ImageField', 'IntegerField',
    1414    'DEFAULT_DATE_INPUT_FORMATS', 'DateField',
    1515    'DEFAULT_TIME_INPUT_FORMATS', 'TimeField',
    1616    'DEFAULT_DATETIME_INPUT_FORMATS', 'DateTimeField',
     
    107107        if self.max_length is not None and isinstance(widget, (TextInput, PasswordInput)):
    108108            return {'maxlength': str(self.max_length)}
    109109
     110class FileField(Field):
     111
     112    prev_error = False
     113 
     114    def clean(self, value):
     115        super(FileField, self).clean(value)
     116
     117        if self.required:
     118            if value[0]:
     119                if not len(value[0]['content'])>0:
     120                    self.prev_error = True
     121                    raise ValidationError(gettext(u'This field is required.'))
     122            else:
     123                if not value[1]:
     124                    self.prev_error = True
     125                    raise ValidationError(gettext(u'This field is required.'))
     126        return value
     127
     128class ImageField(FileField):
     129   
     130    def clean(self, value):
     131        super(ImageField, self).clean(value)
     132        if value[0] and not value[1] and not self.prev_error:
     133            from PIL import Image
     134            from cStringIO import StringIO
     135            try:
     136                Image.open(StringIO(value[0]['content']))
     137            except IOError: # Python Imaging Library doesn't recognize it as an image
     138                raise ValidationError, gettext(u'Upload a valid image. The file you uploaded was either not an image or a corrupted image.')
     139        return value
     140
    110141class IntegerField(Field):
    111142    def __init__(self, max_value=None, min_value=None, *args, **kwargs):
    112143        self.max_value, self.min_value = max_value, min_value
  • django/newforms/widgets.py

     
    115115
    116116class FileInput(Input):
    117117    input_type = 'file'
     118   
     119    def render(self,name,value,attrs=None,choices=()):
     120        if value is None: value = ''
     121        file_attrs = self.build_attrs(attrs, type='file', name=name+'_file')
     122        final_attrs = self.build_attrs(attrs, type='hidden', name=name)
     123        if value != '': final_attrs['value'] = smart_unicode(value) # only add the value attribute if a value is non-empty
     124       
     125        if value != '':
     126            currently = u'Currently: %s<br/>Change: ' % smart_unicode(value)
     127            current = u' <input%s />' % flatatt(final_attrs)
     128        else:
     129            currently = u''
     130            current = u''
     131         
     132        return u'%s<input%s />%s' % (currently,flatatt(file_attrs),current)
     133   
     134    def value_from_datadict(self,data,name):
     135        return [data.get(name+'_file',None),data.get(name,None)]
    118136
    119137class Textarea(Widget):
    120138    def render(self, name, value, attrs=None):
  • tests/modeltests/model_forms/models.py

     
    5454    def __str__(self):
    5555        return self.headline
    5656
     57class File(models.Model):
     58    description = models.CharField(maxlength=50)
     59    file = models.FileField(upload_to='files')
     60
     61    def __str__(self):
     62        return self.description
     63
    5764__test__ = {'API_TESTS': """
    5865>>> from django.newforms import form_for_model, form_for_instance, save_instance, BaseForm, Form, CharField
    5966>>> import datetime
     
    370377Traceback (most recent call last):
    371378...
    372379ValidationError: [u'Select a valid choice. 5 is not one of the available choices.']
     380
     381Test for filefield
     382
     383>>> form_data = {'description': 'FileField test', 'file_file': {'content': 'FileField test', 'filename': 'filetest.txt'} }
     384
     385>>> TestFileForm = form_for_model(File)
     386>>> empty_fileform = TestFileForm(auto_id=False)
     387>>> print empty_fileform.as_ul()
     388<li>Description: <input type="text" name="description" maxlength="50" /></li>
     389<li>File: <input type="file" name="file_file" /></li>
     390
     391Test with data
     392
     393>>> filled_fileform= TestFileForm(form_data, auto_id=False)
     394>>> filled_fileform.is_valid()
     395True
     396
     397>>> instance = filled_fileform.save()
     398>>> print instance.file
     399files/filetest.txt
     400>>> instance.delete()
    373401"""}
  • tests/regressiontests/forms/tests.py

     
    165165# FileInput Widget ############################################################
    166166
    167167>>> w = FileInput()
    168 >>> w.render('email', '')
    169 u'<input type="file" name="email" />'
    170 >>> w.render('email', None)
    171 u'<input type="file" name="email" />'
    172 >>> w.render('email', 'test@example.com')
    173 u'<input type="file" name="email" value="test@example.com" />'
    174 >>> w.render('email', 'some "quoted" & ampersanded value')
    175 u'<input type="file" name="email" value="some &quot;quoted&quot; &amp; ampersanded value" />'
    176 >>> w.render('email', 'test@example.com', attrs={'class': 'fun'})
    177 u'<input type="file" name="email" value="test@example.com" class="fun" />'
     168>>> w.render('file', '')
     169u'<input type="file" name="file_file" />'
     170>>> w.render('file', 'file/filetest.txt')
     171u'Currently: file/filetest.txt<br/>Change: <input type="file" name="file_file" /> <input type="hidden" name="file" value="file/filetest.txt" />'
    178172
    179 You can also pass 'attrs' to the constructor:
    180 >>> w = FileInput(attrs={'class': 'fun'})
    181 >>> w.render('email', '')
    182 u'<input type="file" class="fun" name="email" />'
    183 >>> w.render('email', 'foo@example.com')
    184 u'<input type="file" class="fun" value="foo@example.com" name="email" />'
    185 
    186 >>> w.render('email', 'ŠĐĆŽćžšđ', attrs={'class': 'fun'})
    187 u'<input type="file" class="fun" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" name="email" />'
    188 
    189173# Textarea Widget #############################################################
    190174
    191175>>> w = Textarea()
Back to Top