diff --git a/mayan/apps/tags/api_views.py b/mayan/apps/tags/api_views.py index 3243970280..542c7c6d2e 100644 --- a/mayan/apps/tags/api_views.py +++ b/mayan/apps/tags/api_views.py @@ -3,7 +3,7 @@ from __future__ import absolute_import from django.core.exceptions import PermissionDenied from django.shortcuts import get_object_or_404 -from rest_framework import generics, status +from rest_framework import generics, status, views from rest_framework.response import Response from acls.models import AccessEntry @@ -86,54 +86,37 @@ class APIDocumentTagListView(generics.ListAPIView): return queryset -class APIDocumentTagRemoveView(generics.DestroyAPIView): +class APIDocumentTagView(views.APIView): """ - Remove a tag from a document. + Add or Remove a tag to a document. """ - serializer_class = TagSerializer + def delete(self, request, *args, **kwargs): + """ + Remove a tag from a document. + """ - def get_document(self): document = get_object_or_404(Document, pk=self.kwargs['document_pk']) try: Permission.objects.check_permissions(self.request.user, [PERMISSION_TAG_REMOVE]) except PermissionDenied: AccessEntry.objects.check_access(PERMISSION_TAG_REMOVE, self.request.user, document) - return document - - def delete(self, request, *args, **kwargs): - tag = self.get_object() - document = self.get_document() - + tag = get_object_or_404(Tag, pk=self.kwargs['pk']) tag.documents.remove(document) return Response(status=status.HTTP_204_NO_CONTENT) - def get_queryset(self): - document = self.get_document() - return document.tags.all() + def post(self, request, *args, **kwargs): + """ + Attach a tag to a document. + """ - -class APIDocumentTagAddView(generics.CreateAPIView): - """ - Attach a tag to a document. - """ - - queryset = Tag.objects.all() - serializer_class = TagSerializer - - def get_document(self): document = get_object_or_404(Document, pk=self.kwargs['document_pk']) try: Permission.objects.check_permissions(self.request.user, [PERMISSION_TAG_ATTACH]) except PermissionDenied: AccessEntry.objects.check_access(PERMISSION_TAG_ATTACH, self.request.user, document) - return document - - def post(self, request, *args, **kwargs): - tag = self.get_object() - document = self.get_document() - + tag = get_object_or_404(Tag, pk=self.kwargs['pk']) tag.documents.add(document) return Response(status=status.HTTP_201_CREATED) diff --git a/mayan/apps/tags/urls.py b/mayan/apps/tags/urls.py index fc2749d358..dc42380f07 100644 --- a/mayan/apps/tags/urls.py +++ b/mayan/apps/tags/urls.py @@ -2,9 +2,8 @@ from __future__ import absolute_import from django.conf.urls import patterns, url -from .api_views import (APIDocumentTagAddView, APIDocumentTagRemoveView, - APIDocumentTagListView, APITagDocumentListView, - APITagListView, APITagView) +from .api_views import (APIDocumentTagView, APIDocumentTagListView, + APITagDocumentListView, APITagListView, APITagView) from .views import TagTaggedItemListView urlpatterns = patterns('tags.views', @@ -31,6 +30,5 @@ api_urls = patterns('', url(r'^tags/(?P[0-9]+)/$', APITagView.as_view(), name='tag-detail'), url(r'^tags/$', APITagListView.as_view(), name='tag-list'), url(r'^document/(?P[0-9]+)/tags/$', APIDocumentTagListView.as_view(), name='document-tag-list'), - url(r'^document/(?P[0-9]+)/tags/(?P[0-9]+)/remove/$', APIDocumentTagRemoveView.as_view(), name='document-tag-remove'), - url(r'^document/(?P[0-9]+)/tags/(?P[0-9]+)/add/$', APIDocumentTagAddView.as_view(), name='document-tag-add'), + url(r'^document/(?P[0-9]+)/tags/(?P[0-9]+)/$', APIDocumentTagView.as_view(), name='document-tag'), )