From 6606d7572c357ea0bca9281e56c7ae8a37d63dbe Mon Sep 17 00:00:00 2001 From: jonaswinkler <17569239+jonaswinkler@users.noreply.github.com> Date: Tue, 16 Mar 2021 20:47:45 +0100 Subject: [PATCH] API refactoring --- src/documents/views.py | 53 ++++-------------------------------------- 1 file changed, 5 insertions(+), 48 deletions(-) diff --git a/src/documents/views.py b/src/documents/views.py index a3f495d50..7b282dfb7 100755 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -18,6 +18,7 @@ from django_q.tasks import async_task from rest_framework import parsers from rest_framework.decorators import action from rest_framework.filters import OrderingFilter, SearchFilter +from rest_framework.generics import GenericAPIView from rest_framework.mixins import ( DestroyModelMixin, ListModelMixin, @@ -366,23 +367,12 @@ class SavedViewViewSet(ModelViewSet): serializer.save(user=self.request.user) -class BulkEditView(APIView): +class BulkEditView(GenericAPIView): permission_classes = (IsAuthenticated,) serializer_class = BulkEditSerializer parser_classes = (parsers.JSONParser,) - def get_serializer_context(self): - return { - 'request': self.request, - 'format': self.format_kwarg, - 'view': self - } - - def get_serializer(self, *args, **kwargs): - kwargs['context'] = self.get_serializer_context() - return self.serializer_class(*args, **kwargs) - def post(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) @@ -399,23 +389,12 @@ class BulkEditView(APIView): return HttpResponseBadRequest(str(e)) -class PostDocumentView(APIView): +class PostDocumentView(GenericAPIView): permission_classes = (IsAuthenticated,) serializer_class = PostDocumentSerializer parser_classes = (parsers.MultiPartParser,) - def get_serializer_context(self): - return { - 'request': self.request, - 'format': self.format_kwarg, - 'view': self - } - - def get_serializer(self, *args, **kwargs): - kwargs['context'] = self.get_serializer_context() - return self.serializer_class(*args, **kwargs) - def post(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) @@ -453,23 +432,12 @@ class PostDocumentView(APIView): return Response("OK") -class SelectionDataView(APIView): +class SelectionDataView(GenericAPIView): permission_classes = (IsAuthenticated,) serializer_class = DocumentListSerializer parser_classes = (parsers.MultiPartParser, parsers.JSONParser) - def get_serializer_context(self): - return { - 'request': self.request, - 'format': self.format_kwarg, - 'view': self - } - - def get_serializer(self, *args, **kwargs): - kwargs['context'] = self.get_serializer_context() - return self.serializer_class(*args, **kwargs) - def post(self, request, format=None): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) @@ -620,23 +588,12 @@ class StatisticsView(APIView): }) -class BulkDownloadView(APIView): +class BulkDownloadView(GenericAPIView): permission_classes = (IsAuthenticated,) serializer_class = BulkDownloadSerializer parser_classes = (parsers.JSONParser,) - def get_serializer_context(self): - return { - 'request': self.request, - 'format': self.format_kwarg, - 'view': self - } - - def get_serializer(self, *args, **kwargs): - kwargs['context'] = self.get_serializer_context() - return self.serializer_class(*args, **kwargs) - def post(self, request, format=None): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True)