2011-06-14 21:22:13 +04:00
|
|
|
"""
|
|
|
|
Customizable serialization.
|
|
|
|
"""
|
|
|
|
from django.db import models
|
2012-05-22 09:39:50 +04:00
|
|
|
from django.db.models.query import QuerySet, RawQuerySet
|
2011-07-01 15:29:42 +04:00
|
|
|
from django.utils.encoding import smart_unicode, is_protected_type, smart_str
|
2011-06-14 21:22:13 +04:00
|
|
|
|
|
|
|
import inspect
|
|
|
|
import types
|
|
|
|
|
|
|
|
|
|
|
|
# We register serializer classes, so that we can refer to them by their
|
|
|
|
# class names, if there are cyclical serialization heirachys.
|
|
|
|
_serializers = {}
|
|
|
|
|
|
|
|
|
|
|
|
def _field_to_tuple(field):
|
|
|
|
"""
|
2011-12-29 17:31:12 +04:00
|
|
|
Convert an item in the `fields` attribute into a 2-tuple.
|
2011-06-14 21:22:13 +04:00
|
|
|
"""
|
|
|
|
if isinstance(field, (tuple, list)):
|
|
|
|
return (field[0], field[1])
|
|
|
|
return (field, None)
|
|
|
|
|
2012-01-21 22:33:34 +04:00
|
|
|
|
2011-06-14 21:22:13 +04:00
|
|
|
def _fields_to_list(fields):
|
|
|
|
"""
|
2012-02-24 04:30:44 +04:00
|
|
|
Return a list of field tuples.
|
2011-06-14 21:22:13 +04:00
|
|
|
"""
|
2012-02-24 04:30:44 +04:00
|
|
|
return [_field_to_tuple(field) for field in fields or ()]
|
2011-06-14 21:22:13 +04:00
|
|
|
|
|
|
|
|
|
|
|
class _SkipField(Exception):
|
|
|
|
"""
|
|
|
|
Signals that a serialized field should be ignored.
|
|
|
|
We use this mechanism as the default behavior for ensuring
|
|
|
|
that we don't infinitely recurse when dealing with nested data.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2011-06-15 17:09:01 +04:00
|
|
|
class _RegisterSerializer(type):
|
2011-06-14 21:22:13 +04:00
|
|
|
"""
|
2011-06-15 17:09:01 +04:00
|
|
|
Metaclass to register serializers.
|
2011-06-14 21:22:13 +04:00
|
|
|
"""
|
|
|
|
def __new__(cls, name, bases, attrs):
|
|
|
|
# Build the class and register it.
|
2011-12-29 17:31:12 +04:00
|
|
|
ret = super(_RegisterSerializer, cls).__new__(cls, name, bases, attrs)
|
2011-06-14 21:22:13 +04:00
|
|
|
_serializers[name] = ret
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
class Serializer(object):
|
|
|
|
"""
|
|
|
|
Converts python objects into plain old native types suitable for
|
|
|
|
serialization. In particular it handles models and querysets.
|
2011-12-29 17:31:12 +04:00
|
|
|
|
2011-06-15 17:09:01 +04:00
|
|
|
The output format is specified by setting a number of attributes
|
|
|
|
on the class.
|
2011-06-14 21:22:13 +04:00
|
|
|
|
|
|
|
You may also override any of the serialization methods, to provide
|
|
|
|
for more flexible behavior.
|
2011-12-29 17:31:12 +04:00
|
|
|
|
2011-06-14 21:22:13 +04:00
|
|
|
Valid output types include anything that may be directly rendered into
|
|
|
|
json, xml etc...
|
|
|
|
"""
|
2011-06-15 17:09:01 +04:00
|
|
|
__metaclass__ = _RegisterSerializer
|
2011-06-14 21:22:13 +04:00
|
|
|
|
2011-12-29 17:31:12 +04:00
|
|
|
fields = ()
|
2011-06-15 17:09:01 +04:00
|
|
|
"""
|
|
|
|
Specify the fields to be serialized on a model or dict.
|
|
|
|
Overrides `include` and `exclude`.
|
|
|
|
"""
|
2011-06-14 21:22:13 +04:00
|
|
|
|
2011-06-15 17:09:01 +04:00
|
|
|
include = ()
|
|
|
|
"""
|
|
|
|
Fields to add to the default set to be serialized on a model/dict.
|
|
|
|
"""
|
2011-06-14 21:22:13 +04:00
|
|
|
|
2011-06-15 17:09:01 +04:00
|
|
|
exclude = ()
|
|
|
|
"""
|
|
|
|
Fields to remove from the default set to be serialized on a model/dict.
|
|
|
|
"""
|
2011-06-14 21:22:13 +04:00
|
|
|
|
2011-06-15 17:09:01 +04:00
|
|
|
rename = {}
|
|
|
|
"""
|
|
|
|
A dict of key->name to use for the field keys.
|
|
|
|
"""
|
2011-06-14 21:22:13 +04:00
|
|
|
|
2011-06-15 17:09:01 +04:00
|
|
|
related_serializer = None
|
|
|
|
"""
|
|
|
|
The default serializer class to use for any related models.
|
|
|
|
"""
|
2011-06-14 21:22:13 +04:00
|
|
|
|
2011-06-15 17:09:01 +04:00
|
|
|
depth = None
|
|
|
|
"""
|
|
|
|
The maximum depth to serialize to, or `None`.
|
|
|
|
"""
|
2012-03-01 16:51:23 +04:00
|
|
|
|
|
|
|
parent = None
|
|
|
|
"""
|
|
|
|
A reference to the root serializer when descending down into fields.
|
|
|
|
"""
|
2011-06-14 21:22:13 +04:00
|
|
|
|
|
|
|
def __init__(self, depth=None, stack=[], **kwargs):
|
2011-09-29 21:47:20 +04:00
|
|
|
if depth is not None:
|
|
|
|
self.depth = depth
|
2011-06-14 21:22:13 +04:00
|
|
|
self.stack = stack
|
2011-12-29 17:31:12 +04:00
|
|
|
|
2011-06-14 21:22:13 +04:00
|
|
|
def get_fields(self, obj):
|
2011-06-15 17:09:01 +04:00
|
|
|
fields = self.fields
|
2011-06-14 21:22:13 +04:00
|
|
|
|
2011-06-15 17:09:01 +04:00
|
|
|
# If `fields` is not set, we use the default fields and modify
|
|
|
|
# them with `include` and `exclude`
|
2011-06-14 21:22:13 +04:00
|
|
|
if not fields:
|
|
|
|
default = self.get_default_fields(obj)
|
2011-06-15 17:09:01 +04:00
|
|
|
include = self.include or ()
|
|
|
|
exclude = self.exclude or ()
|
2011-06-14 21:22:13 +04:00
|
|
|
fields = set(default + list(include)) - set(exclude)
|
|
|
|
|
|
|
|
return fields
|
|
|
|
|
|
|
|
def get_default_fields(self, obj):
|
|
|
|
"""
|
|
|
|
Return the default list of field names/keys for a model instance/dict.
|
2011-06-15 17:09:01 +04:00
|
|
|
These are used if `fields` is not given.
|
2011-06-14 21:22:13 +04:00
|
|
|
"""
|
|
|
|
if isinstance(obj, models.Model):
|
|
|
|
opts = obj._meta
|
|
|
|
return [field.name for field in opts.fields + opts.many_to_many]
|
|
|
|
else:
|
|
|
|
return obj.keys()
|
|
|
|
|
2012-02-24 04:30:44 +04:00
|
|
|
def get_related_serializer(self, info):
|
2011-06-14 21:22:13 +04:00
|
|
|
# If an element in `fields` is a 2-tuple of (str, tuple)
|
|
|
|
# then the second element of the tuple is the fields to
|
|
|
|
# set on the related serializer
|
2012-08-13 18:58:23 +04:00
|
|
|
|
|
|
|
class OnTheFlySerializer(self.__class__):
|
|
|
|
fields = info
|
|
|
|
parent = getattr(self, 'parent') or self
|
|
|
|
|
2011-06-14 21:22:13 +04:00
|
|
|
if isinstance(info, (list, tuple)):
|
|
|
|
return OnTheFlySerializer
|
|
|
|
|
|
|
|
# If an element in `fields` is a 2-tuple of (str, Serializer)
|
|
|
|
# then the second element of the tuple is the Serializer
|
|
|
|
# class to use for that field.
|
|
|
|
elif isinstance(info, type) and issubclass(info, Serializer):
|
|
|
|
return info
|
|
|
|
|
|
|
|
# If an element in `fields` is a 2-tuple of (str, str)
|
|
|
|
# then the second element of the tuple is the name of the Serializer
|
|
|
|
# class to use for that field.
|
|
|
|
#
|
|
|
|
# Black magic to deal with cyclical Serializer dependancies.
|
|
|
|
# Similar to what Django does for cyclically related models.
|
|
|
|
elif isinstance(info, str) and info in _serializers:
|
|
|
|
return _serializers[info]
|
2011-12-29 17:31:12 +04:00
|
|
|
|
2012-08-13 18:58:23 +04:00
|
|
|
# Otherwise use `related_serializer` or fall back to
|
|
|
|
# `OnTheFlySerializer` preserve custom serialization methods.
|
|
|
|
return getattr(self, 'related_serializer') or OnTheFlySerializer
|
2011-06-14 21:22:13 +04:00
|
|
|
|
|
|
|
def serialize_key(self, key):
|
|
|
|
"""
|
|
|
|
Keys serialize to their string value,
|
2011-06-15 17:09:01 +04:00
|
|
|
unless they exist in the `rename` dict.
|
2011-06-14 21:22:13 +04:00
|
|
|
"""
|
2011-07-04 12:31:46 +04:00
|
|
|
return self.rename.get(smart_str(key), smart_str(key))
|
2011-06-14 21:22:13 +04:00
|
|
|
|
2012-02-24 04:30:44 +04:00
|
|
|
def serialize_val(self, key, obj, related_info):
|
2011-06-14 21:22:13 +04:00
|
|
|
"""
|
|
|
|
Convert a model field or dict value into a serializable representation.
|
|
|
|
"""
|
2012-02-24 04:30:44 +04:00
|
|
|
related_serializer = self.get_related_serializer(related_info)
|
2011-12-29 17:31:12 +04:00
|
|
|
|
2011-06-14 21:22:13 +04:00
|
|
|
if self.depth is None:
|
|
|
|
depth = None
|
|
|
|
elif self.depth <= 0:
|
|
|
|
return self.serialize_max_depth(obj)
|
|
|
|
else:
|
|
|
|
depth = self.depth - 1
|
|
|
|
|
2012-09-19 19:05:16 +04:00
|
|
|
if obj in self.stack:
|
2011-06-14 21:22:13 +04:00
|
|
|
return self.serialize_recursion(obj)
|
|
|
|
else:
|
|
|
|
stack = self.stack[:]
|
|
|
|
stack.append(obj)
|
|
|
|
|
2012-06-27 03:27:57 +04:00
|
|
|
return related_serializer(depth=depth, stack=stack).serialize(
|
2012-06-28 16:16:30 +04:00
|
|
|
obj, request=getattr(self, 'request', None))
|
2011-06-14 21:22:13 +04:00
|
|
|
|
|
|
|
def serialize_max_depth(self, obj):
|
|
|
|
"""
|
|
|
|
Determine how objects should be serialized once `depth` is exceeded.
|
|
|
|
The default behavior is to ignore the field.
|
|
|
|
"""
|
|
|
|
raise _SkipField
|
|
|
|
|
|
|
|
def serialize_recursion(self, obj):
|
|
|
|
"""
|
|
|
|
Determine how objects should be serialized if recursion occurs.
|
|
|
|
The default behavior is to ignore the field.
|
|
|
|
"""
|
|
|
|
raise _SkipField
|
|
|
|
|
|
|
|
def serialize_model(self, instance):
|
|
|
|
"""
|
2011-06-15 17:09:01 +04:00
|
|
|
Given a model instance or dict, serialize it to a dict..
|
2011-06-14 21:22:13 +04:00
|
|
|
"""
|
|
|
|
data = {}
|
|
|
|
|
|
|
|
fields = self.get_fields(instance)
|
|
|
|
|
2011-12-29 17:31:12 +04:00
|
|
|
# serialize each required field
|
2012-02-24 04:30:44 +04:00
|
|
|
for fname, related_info in _fields_to_list(fields):
|
2011-06-14 21:22:13 +04:00
|
|
|
try:
|
2012-01-10 22:38:01 +04:00
|
|
|
# we first check for a method 'fname' on self,
|
|
|
|
# 'fname's signature must be 'def fname(self, instance)'
|
|
|
|
meth = getattr(self, fname, None)
|
|
|
|
if (inspect.ismethod(meth) and
|
|
|
|
len(inspect.getargspec(meth)[0]) == 2):
|
2012-01-09 18:56:41 +04:00
|
|
|
obj = meth(instance)
|
2011-12-13 14:14:23 +04:00
|
|
|
elif hasattr(instance, '__contains__') and fname in instance:
|
2012-01-10 22:38:01 +04:00
|
|
|
# then check for a key 'fname' on the instance
|
2011-12-13 14:14:23 +04:00
|
|
|
obj = instance[fname]
|
|
|
|
elif hasattr(instance, smart_str(fname)):
|
|
|
|
# finally check for an attribute 'fname' on the instance
|
|
|
|
obj = getattr(instance, fname)
|
|
|
|
else:
|
|
|
|
continue
|
|
|
|
|
2011-06-14 21:22:13 +04:00
|
|
|
key = self.serialize_key(fname)
|
2012-02-24 04:30:44 +04:00
|
|
|
val = self.serialize_val(fname, obj, related_info)
|
2011-06-14 21:22:13 +04:00
|
|
|
data[key] = val
|
|
|
|
except _SkipField:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
def serialize_iter(self, obj):
|
|
|
|
"""
|
|
|
|
Convert iterables into a serializable representation.
|
|
|
|
"""
|
|
|
|
return [self.serialize(item) for item in obj]
|
|
|
|
|
|
|
|
def serialize_func(self, obj):
|
|
|
|
"""
|
|
|
|
Convert no-arg methods and functions into a serializable representation.
|
|
|
|
"""
|
|
|
|
return self.serialize(obj())
|
|
|
|
|
|
|
|
def serialize_manager(self, obj):
|
|
|
|
"""
|
|
|
|
Convert a model manager into a serializable representation.
|
|
|
|
"""
|
|
|
|
return self.serialize_iter(obj.all())
|
|
|
|
|
|
|
|
def serialize_fallback(self, obj):
|
|
|
|
"""
|
|
|
|
Convert any unhandled object into a serializable representation.
|
|
|
|
"""
|
|
|
|
return smart_unicode(obj, strings_only=True)
|
2011-12-29 17:31:12 +04:00
|
|
|
|
2012-06-27 03:27:57 +04:00
|
|
|
def serialize(self, obj, request=None):
|
2011-06-14 21:22:13 +04:00
|
|
|
"""
|
|
|
|
Convert any object into a serializable representation.
|
|
|
|
"""
|
2011-12-29 17:31:12 +04:00
|
|
|
|
2012-06-27 03:27:57 +04:00
|
|
|
# Request from related serializer.
|
|
|
|
if request is not None:
|
|
|
|
self.request = request
|
|
|
|
|
2011-06-14 21:22:13 +04:00
|
|
|
if isinstance(obj, (dict, models.Model)):
|
|
|
|
# Model instances & dictionaries
|
|
|
|
return self.serialize_model(obj)
|
2012-05-22 09:39:50 +04:00
|
|
|
elif isinstance(obj, (tuple, list, set, QuerySet, RawQuerySet, types.GeneratorType)):
|
2011-06-14 21:22:13 +04:00
|
|
|
# basic iterables
|
|
|
|
return self.serialize_iter(obj)
|
|
|
|
elif isinstance(obj, models.Manager):
|
|
|
|
# Manager objects
|
|
|
|
return self.serialize_manager(obj)
|
|
|
|
elif inspect.isfunction(obj) and not inspect.getargspec(obj)[0]:
|
|
|
|
# function with no args
|
|
|
|
return self.serialize_func(obj)
|
|
|
|
elif inspect.ismethod(obj) and len(inspect.getargspec(obj)[0]) <= 1:
|
|
|
|
# bound method
|
|
|
|
return self.serialize_func(obj)
|
|
|
|
|
2011-06-22 02:01:41 +04:00
|
|
|
# Protected types are passed through as is.
|
|
|
|
# (i.e. Primitives like None, numbers, dates, and Decimals.)
|
|
|
|
if is_protected_type(obj):
|
|
|
|
return obj
|
|
|
|
|
|
|
|
# All other values are converted to string.
|
2011-06-14 21:22:13 +04:00
|
|
|
return self.serialize_fallback(obj)
|