Split errors into errors and _errors_many

Reduce indentation and linearize code to improve readability.
This commit is contained in:
Ian Foote 2014-03-27 15:38:21 +00:00
parent f64599c361
commit c0c8833185

View File

@ -507,7 +507,9 @@ class BaseSerializer(WritableField):
'Use the `many=True` flag when instantiating the serializer.',
DeprecationWarning, stacklevel=3)
if not many:
if many:
return self._errors_many(data)
ret = self.from_native(data, files)
if not self._errors:
@ -515,6 +517,13 @@ class BaseSerializer(WritableField):
return self._errors
def _errors_many(self, data):
"""Run deserialization of bulk data."""
if not hasattr(data, '__iter__') or isinstance(data, (dict, six.text_type)):
self._errors = {'non_field_errors': ['Expected a list of items.']}
return self._errors
ret = RelationsList()
errors = []
update = self.object is not None
@ -528,7 +537,6 @@ class BaseSerializer(WritableField):
identities = [self.get_identity(self.to_native(obj)) for obj in objects]
identity_to_objects = dict(zip(identities, objects))
if hasattr(data, '__iter__') and not isinstance(data, (dict, six.text_type)):
for item in data:
if update:
# Determine which object we're updating
@ -547,8 +555,6 @@ class BaseSerializer(WritableField):
ret._deleted = identity_to_objects.values()
self._errors = any(errors) and errors or []
else:
self._errors = {'non_field_errors': ['Expected a list of items.']}
if not self._errors:
self.object = ret