Django

Code

Ticket #7614: initial_patch.diff

File initial_patch.diff, 6.2 kB (added by axiak, 5 months ago)

Initial patch, with doc and test changes.

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

    old new  
    766766    def get_db_prep_save(self, value): 
    767767        "Returns field's value prepared for saving into a database." 
    768768        # Need to convert UploadedFile objects provided via a form to unicode for database insertion 
    769         if value is None: 
     769        if hasattr(value, 'file_name'): 
     770            raise Exception("Yep %s" % value) 
     771            return value.file_name 
     772        elif value is None: 
    770773            return None 
    771         return unicode(value) 
     774        else: 
     775            return unicode(value) 
    772776 
    773777    def get_manipulator_fields(self, opts, manipulator, change, name_prefix='', rel=False, follow=True): 
    774778        field_list = Field.get_manipulator_fields(self, opts, manipulator, change, name_prefix, rel, follow) 
     
    857861        return os.path.normpath(f) 
    858862 
    859863    def save_form_data(self, instance, data): 
    860         from django.newforms.fields import UploadedFile 
     864        from django.core.files.uploadedfile import UploadedFile 
    861865        if data and isinstance(data, UploadedFile): 
    862             getattr(instance, "save_%s_file" % self.name)(data.filename, data.data, save=False) 
     866            getattr(instance, "save_%s_file" % self.name)(data.file_name, data, save=False) 
    863867 
    864868    def formfield(self, **kwargs): 
    865869        defaults = {'form_class': forms.FileField} 
  • django/newforms/fields.py

    old new  
    2727 
    2828from util import ErrorList, ValidationError 
    2929from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple, DateTimeInput 
     30from django.core.files.uploadedfile import SimpleUploadedFile 
    3031 
    31  
    3232__all__ = ( 
    3333    'Field', 'CharField', 'IntegerField', 
    3434    'DEFAULT_DATE_INPUT_FORMATS', 'DateField', 
     
    460460                category = DeprecationWarning, 
    461461                stacklevel = 2 
    462462            ) 
     463            data = SimpleUploadedFile(data['filename'], data['content']) 
    463464 
    464465        try: 
    465466            file_name = data.file_name 
    466467            file_size = data.file_size 
    467468        except AttributeError: 
    468             try: 
    469                 file_name = data.get('filename') 
    470                 file_size = bool(data['content']) 
    471             except (AttributeError, KeyError): 
    472                 raise ValidationError(self.error_messages['invalid']) 
     469            raise ValidationError(self.error_messages['invalid']) 
    473470 
    474471        if not file_name: 
    475472            raise ValidationError(self.error_messages['invalid']) 
    476473        if not file_size: 
    477474            raise ValidationError(self.error_messages['empty']) 
    478475 
    479         return UploadedFile(file_name, data) 
     476        return data 
    480477 
    481478class ImageField(FileField): 
    482479    default_error_messages = { 
  • tests/modeltests/model_forms/models.py

    old new  
    803803>>> f.is_valid() 
    804804True 
    805805>>> type(f.cleaned_data['file']) 
    806 <class 'django.newforms.fields.UploadedFile'> 
     806<class 'django.core.files.uploadedfile.SimpleUploadedFile'> 
    807807>>> instance = f.save() 
    808808>>> instance.file 
    809809u'...test1.txt' 
     
    814814>>> f.is_valid() 
    815815True 
    816816>>> type(f.cleaned_data['file']) 
    817 <class 'django.newforms.fields.UploadedFile'> 
     817<class 'django.core.files.uploadedfile.SimpleUploadedFile'> 
    818818>>> instance = f.save() 
    819819>>> instance.file 
    820820u'...test1.txt' 
     
    906906>>> f.is_valid() 
    907907True 
    908908>>> type(f.cleaned_data['image']) 
    909 <class 'django.newforms.fields.UploadedFile'> 
     909<class 'django.core.files.uploadedfile.SimpleUploadedFile'> 
    910910>>> instance = f.save() 
    911911>>> instance.image 
    912912u'...test.png' 
     
    918918>>> f.is_valid() 
    919919True 
    920920>>> type(f.cleaned_data['image']) 
    921 <class 'django.newforms.fields.UploadedFile'> 
     921<class 'django.core.files.uploadedfile.SimpleUploadedFile'> 
    922922>>> instance = f.save() 
    923923>>> instance.image 
    924924u'...test.png' 
  • tests/regressiontests/forms/fields.py

    old new  
    800800ValidationError: [u'The submitted file is empty.'] 
    801801 
    802802>>> type(f.clean(SimpleUploadedFile('name', 'Some File Content'))) 
    803 <class 'django.newforms.fields.UploadedFile'> 
     803<class 'django.core.files.uploadedfile.SimpleUploadedFile'> 
    804804 
    805805>>> type(f.clean(SimpleUploadedFile('name', 'Some File Content'), 'files/test4.pdf')) 
    806 <class 'django.newforms.fields.UploadedFile'> 
     806<class 'django.core.files.uploadedfile.SimpleUploadedFile'> 
    807807 
    808808# URLField ################################################################## 
    809809 
  • docs/newforms.txt

    old new  
    13311331    * Validates that non-empty file data has been bound to the form. 
    13321332    * Error message keys: ``required``, ``invalid``, ``missing``, ``empty`` 
    13331333 
    1334 An ``UploadedFile`` object has two attributes: 
     1334To learn more about the ``UploadedFile`` object, see the `file uploads documentation`_. 
    13351335 
    1336     ======================  ==================================================== 
    1337     Attribute               Description 
    1338     ======================  ==================================================== 
    1339     ``filename``            The name of the file, provided by the uploading 
    1340                             client. 
    1341                              
    1342     ``content``             The array of bytes comprising the file content. 
    1343     ======================  ==================================================== 
    1344  
    1345 The string representation of an ``UploadedFile`` is the same as the filename 
    1346 attribute. 
    1347  
    13481336When you use a ``FileField`` on a form, you must also remember to 
    13491337`bind the file data to the form`_. 
    13501338 
     1339.. _file uploads documentation: ../upload_handling/ 
    13511340.. _`bind the file data to the form`: `Binding uploaded files to a form`_ 
    13521341 
    13531342``FilePathField``