reorganize API

This commit is contained in:
Laura Klünder 2016-10-06 17:44:09 +02:00
parent a19f3c4de0
commit 7ea4a1bd21
10 changed files with 142 additions and 35 deletions

View file

@ -0,0 +1,46 @@
from collections import Iterable
from django.db.models.manager import BaseManager
from rest_framework import serializers
class PkField(serializers.DictField):
"""
give primary key
"""
def to_representation(self, obj):
if hasattr(obj, 'pk'):
return obj.pk
elif isinstance(obj, Iterable):
return tuple(self.to_representation(elem) for elem in obj)
elif isinstance(obj, BaseManager):
return tuple(self.to_representation(elem) for elem in obj.all())
return None
class RecursiveSerializerMixin(serializers.Serializer):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
request = self.context.get('request')
request_sparse = self.context['request_sparse'] = request is not None and request.GET.get('sparse')
sparse = self.context['sparse'] = request_sparse or self.context.get('sparse')
if sparse:
for name in getattr(self.Meta, 'sparse_exclude', ()):
value = self.fields.get(name)
if value is not None and isinstance(value, serializers.Serializer):
self.fields[name] = PkField()
if request_sparse:
for name in tuple(self.fields):
if name == 'url' or name.endswith('_url'):
self.fields.pop(name)
def sparse_context(self):
return {'request': self.context.get('request'), 'sparse': True}
def recursive_value(self, serializer, obj, *args, **kwargs):
if self.context.get('sparse'):
return PkField().to_representation(obj)
return serializer(obj, *args, **kwargs, context=self.sparse_context()).data

View file

@ -29,5 +29,6 @@
{% block description %}
{% if breadcrumblist|length == 1 %}
<p>Welcome to the c3nav RESTful API.</p>
<p><small><em>Add <code>?sparse=1</code> to any request to flatten its result.</em></small></p>
{% else %}{{ description }}{% endif %}
{% endblock %}