From 7c4ae1aef0024edcef652370331e6a465c7abee4 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Sat, 19 Jan 2019 01:04:59 -0400 Subject: [PATCH] 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 --- mayan/apps/common/api_views.py | 39 +++++++++++++++++----------------- mayan/apps/common/urls.py | 28 ++++++++++-------------- 2 files changed, 31 insertions(+), 36 deletions(-) diff --git a/mayan/apps/common/api_views.py b/mayan/apps/common/api_views.py index 9c632282a2..ab762843a8 100644 --- a/mayan/apps/common/api_views.py +++ b/mayan/apps/common/api_views.py @@ -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) diff --git a/mayan/apps/common/urls.py b/mayan/apps/common/urls.py index a857ab1bce..9d42b6f802 100644 --- a/mayan/apps/common/urls.py +++ b/mayan/apps/common/urls.py @@ -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[-\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' + }, +)