2014-12-15 14:55:17 +03:00
|
|
|
from __future__ import unicode_literals
|
2014-12-02 18:15:21 +03:00
|
|
|
import collections
|
2014-12-15 14:55:17 +03:00
|
|
|
from rest_framework.compat import OrderedDict, unicode_to_repr
|
2014-11-07 13:13:46 +03:00
|
|
|
|
|
|
|
|
|
|
|
class ReturnDict(OrderedDict):
|
|
|
|
"""
|
|
|
|
Return object from `serialier.data` for the `Serializer` class.
|
|
|
|
Includes a backlink to the serializer instance for renderers
|
|
|
|
to use if they need richer field information.
|
|
|
|
"""
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.serializer = kwargs.pop('serializer')
|
|
|
|
super(ReturnDict, self).__init__(*args, **kwargs)
|
|
|
|
|
2014-11-07 14:16:26 +03:00
|
|
|
def copy(self):
|
|
|
|
return ReturnDict(self, serializer=self.serializer)
|
|
|
|
|
2015-01-19 17:21:09 +03:00
|
|
|
def __repr__(self):
|
|
|
|
return dict.__repr__(self)
|
|
|
|
|
2015-01-21 17:18:13 +03:00
|
|
|
def __reduce__(self):
|
|
|
|
# Pickling these objects will drop the .serializer backlink,
|
|
|
|
# but preserve the raw data.
|
|
|
|
return (dict, (dict(self),))
|
|
|
|
|
2014-11-07 13:13:46 +03:00
|
|
|
|
|
|
|
class ReturnList(list):
|
|
|
|
"""
|
|
|
|
Return object from `serialier.data` for the `SerializerList` class.
|
|
|
|
Includes a backlink to the serializer instance for renderers
|
|
|
|
to use if they need richer field information.
|
|
|
|
"""
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.serializer = kwargs.pop('serializer')
|
|
|
|
super(ReturnList, self).__init__(*args, **kwargs)
|
|
|
|
|
2015-01-19 17:21:09 +03:00
|
|
|
def __repr__(self):
|
|
|
|
return list.__repr__(self)
|
|
|
|
|
2015-01-21 17:18:13 +03:00
|
|
|
def __reduce__(self):
|
|
|
|
# Pickling these objects will drop the .serializer backlink,
|
|
|
|
# but preserve the raw data.
|
|
|
|
return (list, (list(self),))
|
|
|
|
|
2014-11-07 13:13:46 +03:00
|
|
|
|
|
|
|
class BoundField(object):
|
|
|
|
"""
|
|
|
|
A field object that also includes `.value` and `.error` properties.
|
|
|
|
Returned when iterating over a serializer instance,
|
|
|
|
providing an API similar to Django forms and form fields.
|
|
|
|
"""
|
|
|
|
def __init__(self, field, value, errors, prefix=''):
|
|
|
|
self._field = field
|
|
|
|
self.value = value
|
|
|
|
self.errors = errors
|
|
|
|
self.name = prefix + self.field_name
|
|
|
|
|
|
|
|
def __getattr__(self, attr_name):
|
|
|
|
return getattr(self._field, attr_name)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _proxy_class(self):
|
|
|
|
return self._field.__class__
|
|
|
|
|
|
|
|
def __repr__(self):
|
2014-12-15 14:55:17 +03:00
|
|
|
return unicode_to_repr('<%s value=%s errors=%s>' % (
|
2014-11-07 13:13:46 +03:00
|
|
|
self.__class__.__name__, self.value, self.errors
|
2014-12-15 14:55:17 +03:00
|
|
|
))
|
2014-11-07 13:13:46 +03:00
|
|
|
|
|
|
|
|
|
|
|
class NestedBoundField(BoundField):
|
|
|
|
"""
|
|
|
|
This `BoundField` additionally implements __iter__ and __getitem__
|
|
|
|
in order to support nested bound fields. This class is the type of
|
|
|
|
`BoundField` that is used for serializer fields.
|
|
|
|
"""
|
|
|
|
def __iter__(self):
|
|
|
|
for field in self.fields.values():
|
|
|
|
yield self[field.field_name]
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
field = self.fields[key]
|
|
|
|
value = self.value.get(key) if self.value else None
|
|
|
|
error = self.errors.get(key) if self.errors else None
|
|
|
|
if hasattr(field, 'fields'):
|
|
|
|
return NestedBoundField(field, value, error, prefix=self.name + '.')
|
|
|
|
return BoundField(field, value, error, prefix=self.name + '.')
|
|
|
|
|
|
|
|
|
2014-12-02 18:15:21 +03:00
|
|
|
class BindingDict(collections.MutableMapping):
|
2014-11-07 13:13:46 +03:00
|
|
|
"""
|
|
|
|
This dict-like object is used to store fields on a serializer.
|
|
|
|
|
|
|
|
This ensures that whenever fields are added to the serializer we call
|
|
|
|
`field.bind()` so that the `field_name` and `parent` attributes
|
|
|
|
can be set correctly.
|
|
|
|
"""
|
|
|
|
def __init__(self, serializer):
|
|
|
|
self.serializer = serializer
|
|
|
|
self.fields = OrderedDict()
|
|
|
|
|
|
|
|
def __setitem__(self, key, field):
|
|
|
|
self.fields[key] = field
|
|
|
|
field.bind(field_name=key, parent=self.serializer)
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
return self.fields[key]
|
|
|
|
|
|
|
|
def __delitem__(self, key):
|
|
|
|
del self.fields[key]
|
|
|
|
|
2014-12-02 18:15:21 +03:00
|
|
|
def __iter__(self):
|
|
|
|
return iter(self.fields)
|
2014-11-07 13:13:46 +03:00
|
|
|
|
2014-12-02 18:15:21 +03:00
|
|
|
def __len__(self):
|
|
|
|
return len(self.fields)
|
2015-01-21 16:03:37 +03:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return dict.__repr__(self.fields)
|