OpenSlides/openslides/utils/rest_api.py

29 lines
861 B
Python
Raw Normal View History

from django.core.urlresolvers import reverse
from rest_framework import permissions, routers, serializers, viewsets # noqa
2014-10-11 14:15:42 +02:00
router = routers.DefaultRouter()
class RESTModelMixin:
"""
Mixin for django models which are used in our rest api.
"""
def get_root_rest_element(self):
"""
Returns the root rest instance.
Uses self as default.
"""
return self
def get_root_rest_url(self):
"""
Returns the detail url of the root model of this object.
"""
# Gets the default url-name in the same way as django rest framework
# does in relations.HyperlinkedModelSerializer
root_instance = self.get_root_rest_element()
rest_url = '%s-detail' % type(root_instance)._meta.object_name.lower()
return reverse(rest_url, args=[str(root_instance.pk)])