Update common app API to viewsets

Update the API entries for content types and templates to use
viewsets and the new api_router_entries URL registraion
method.

Signed-off-by: Roberto Rosario <Roberto.Rosario@mayan-edms.com>
This commit is contained in:
Roberto Rosario
2019-01-19 01:04:59 -04:00
parent 16d8fb9fea
commit 7c4ae1aef0
2 changed files with 31 additions and 36 deletions

View File

@@ -2,42 +2,43 @@ from __future__ import unicode_literals
from django.contrib.contenttypes.models import ContentType
from rest_framework import generics
from rest_framework import viewsets
from rest_framework.permissions import IsAuthenticated
from .classes import Template
from .serializers import ContentTypeSerializer, TemplateSerializer
class APIContentTypeList(generics.ListAPIView):
class APIContentTypeViewSet(viewsets.ReadOnlyModelViewSet):
"""
Returns a list of all the available content types.
list:
Return a list of all the available content types.
retrieve:
Return the given content type details.
"""
serializer_class = ContentTypeSerializer
lookup_field = 'pk'
lookup_url_kwarg = 'content_type_id'
queryset = ContentType.objects.order_by('app_label', 'model')
serializer_class = ContentTypeSerializer
class APITemplateListView(generics.ListAPIView):
class APITemplateViewSet(viewsets.ReadOnlyModelViewSet):
"""
Returns a list of partial templates.
get: Returns a list of partial templates.
list:
Return a list of partial templates.
retrieve:
Return the given partial template details.
"""
serializer_class = TemplateSerializer
lookup_url_kwarg = 'name'
permission_classes = (IsAuthenticated,)
def get_queryset(self):
return Template.all(rendered=True, request=self.request)
class APITemplateView(generics.RetrieveAPIView):
"""
Returns the selected partial template details.
get: Retrieve the details of the partial template.
"""
serializer_class = TemplateSerializer
permission_classes = (IsAuthenticated,)
def get_object(self):
return Template.get(name=self.kwargs['name']).render(
request=self.request
)
def get_queryset(self):
return Template.all(rendered=True, request=self.request)

View File

@@ -3,9 +3,7 @@ from __future__ import unicode_literals
from django.conf.urls import url
from django.views.i18n import javascript_catalog, set_language
from .api_views import (
APIContentTypeList, APITemplateListView, APITemplateView
)
from .api_views import APIContentTypeViewSet, APITemplateViewSet
from .views import (
AboutView, CheckVersionView, CurrentUserLocaleProfileDetailsView,
CurrentUserLocaleProfileEditView, FaviconRedirectView, HomeView,
@@ -67,17 +65,13 @@ urlpatterns += [
),
]
api_urls = [
url(
regex=r'^content_types/$', name='content-type-list',
view=APIContentTypeList.as_view()
),
url(
regex=r'^templates/$', name='template-list',
view=APITemplateListView.as_view()
),
url(
regex=r'^templates/(?P<name>[-\w]+)/$', name='template-detail',
view=APITemplateView.as_view()
),
]
api_router_entries = (
{
'prefix': r'content_types', 'viewset': APIContentTypeViewSet,
'basename': 'content_type'
},
{
'prefix': r'templates', 'viewset': APITemplateViewSet,
'basename': 'template'
},
)