Processed review comments.

No type checking in .restore_fields()
Added missing BytesIO import.
This commit is contained in:
Marko Tibold 2012-11-14 21:13:23 +01:00
parent 8cdbc0a33a
commit c35b9eb065
2 changed files with 17 additions and 10 deletions

View File

@ -3,6 +3,8 @@ import datetime
import inspect import inspect
import warnings import warnings
from io import BytesIO
from django.core import validators from django.core import validators
from django.core.exceptions import ObjectDoesNotExist, ValidationError from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.core.urlresolvers import resolve, get_script_prefix from django.core.urlresolvers import resolve, get_script_prefix
@ -31,6 +33,7 @@ class Field(object):
creation_counter = 0 creation_counter = 0
empty = '' empty = ''
type_name = None type_name = None
_use_files = None
def __init__(self, source=None): def __init__(self, source=None):
self.parent = None self.parent = None
@ -51,7 +54,7 @@ class Field(object):
self.root = parent.root or parent self.root = parent.root or parent
self.context = self.root.context self.context = self.root.context
def field_from_native(self, data, field_name, into): def field_from_native(self, data, files, field_name, into):
""" """
Given a dictionary and a field name, updates the dictionary `into`, Given a dictionary and a field name, updates the dictionary `into`,
with the field and it's deserialized value. with the field and it's deserialized value.
@ -166,7 +169,7 @@ class WritableField(Field):
if errors: if errors:
raise ValidationError(errors) raise ValidationError(errors)
def field_from_native(self, data, field_name, into): def field_from_native(self, data, files, field_name, into):
""" """
Given a dictionary and a field name, updates the dictionary `into`, Given a dictionary and a field name, updates the dictionary `into`,
with the field and it's deserialized value. with the field and it's deserialized value.
@ -175,7 +178,10 @@ class WritableField(Field):
return return
try: try:
native = data[field_name] if self._use_files:
native = files[field_name]
else:
native = data[field_name]
except KeyError: except KeyError:
if self.default is not None: if self.default is not None:
native = self.default native = self.default
@ -323,7 +329,7 @@ class RelatedField(WritableField):
value = getattr(obj, self.source or field_name) value = getattr(obj, self.source or field_name)
return self.to_native(value) return self.to_native(value)
def field_from_native(self, data, field_name, into): def field_from_native(self, data, files, field_name, into):
if self.read_only: if self.read_only:
return return
@ -341,7 +347,7 @@ class ManyRelatedMixin(object):
value = getattr(obj, self.source or field_name) value = getattr(obj, self.source or field_name)
return [self.to_native(item) for item in value.all()] return [self.to_native(item) for item in value.all()]
def field_from_native(self, data, field_name, into): def field_from_native(self, data, files, field_name, into):
if self.read_only: if self.read_only:
return return
@ -907,8 +913,10 @@ class FloatField(WritableField):
class FileField(WritableField): class FileField(WritableField):
_use_files = True
type_name = 'FileField' type_name = 'FileField'
widget = widgets.FileInput widget = widgets.FileInput
default_error_messages = { default_error_messages = {
'invalid': _("No file was submitted. Check the encoding type on the form."), 'invalid': _("No file was submitted. Check the encoding type on the form."),
'missing': _("No file was submitted."), 'missing': _("No file was submitted."),
@ -951,6 +959,8 @@ class FileField(WritableField):
class ImageField(FileField): class ImageField(FileField):
_use_files = True
default_error_messages = { default_error_messages = {
'invalid_image': _("Upload a valid image. The file you uploaded was either not an image or a corrupted image."), 'invalid_image': _("Upload a valid image. The file you uploaded was either not an image or a corrupted image."),
} }
@ -990,7 +1000,7 @@ class ImageField(FileField):
# _imaging C module isn't available, so an ImportError will be # _imaging C module isn't available, so an ImportError will be
# raised. Catch and re-raise. # raised. Catch and re-raise.
raise raise
except Exception: # Python Imaging Library doesn't recognize it as an image except Exception: # Python Imaging Library doesn't recognize it as an image
raise ValidationError(self.error_messages['invalid_image']) raise ValidationError(self.error_messages['invalid_image'])
if hasattr(f, 'seek') and callable(f.seek): if hasattr(f, 'seek') and callable(f.seek):
f.seek(0) f.seek(0)

View File

@ -198,10 +198,7 @@ class BaseSerializer(Field):
reverted_data = {} reverted_data = {}
for field_name, field in fields.items(): for field_name, field in fields.items():
try: try:
if isinstance(field, (FileField, ImageField)): field.field_from_native(data, files, field_name, reverted_data)
field.field_from_native(files, field_name, reverted_data)
else:
field.field_from_native(data, field_name, reverted_data)
except ValidationError as err: except ValidationError as err:
self._errors[field_name] = list(err.messages) self._errors[field_name] = list(err.messages)