Add API endpoint to remove a tag from a document
This commit is contained in:
@@ -3,7 +3,8 @@ from __future__ import absolute_import
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.shortcuts import get_object_or_404
|
||||
|
||||
from rest_framework import generics
|
||||
from rest_framework import generics, status
|
||||
from rest_framework.response import Response
|
||||
from taggit.models import Tag
|
||||
|
||||
from acls.models import AccessEntry
|
||||
@@ -13,7 +14,7 @@ from permissions.models import Permission
|
||||
from rest_api.filters import MayanObjectPermissionsFilter
|
||||
from rest_api.permissions import MayanPermission
|
||||
|
||||
from .permissions import PERMISSION_TAG_VIEW
|
||||
from .permissions import PERMISSION_TAG_REMOVE, PERMISSION_TAG_VIEW
|
||||
from .serializers import TagSerializer
|
||||
|
||||
|
||||
@@ -82,3 +83,31 @@ class APIDocumentTagListView(generics.ListAPIView):
|
||||
|
||||
queryset = document.tags.all()
|
||||
return queryset
|
||||
|
||||
|
||||
class APIDocumentTagRemoveView(generics.DestroyAPIView):
|
||||
"""
|
||||
Remove a tag from a document.
|
||||
"""
|
||||
|
||||
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_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()
|
||||
|
||||
document.tags.remove(tag)
|
||||
return Response(status=status.HTTP_204_NO_CONTENT)
|
||||
|
||||
def get_queryset(self):
|
||||
document = self.get_document()
|
||||
return document.tags.all()
|
||||
|
||||
@@ -2,8 +2,8 @@ from __future__ import absolute_import
|
||||
|
||||
from django.conf.urls import patterns, url
|
||||
|
||||
from .api_views import (APIDocumentTagListView, APITagDocumentListView,
|
||||
APITagListView, APITagView)
|
||||
from .api_views import (APIDocumentTagRemoveView, APIDocumentTagListView,
|
||||
APITagDocumentListView, APITagListView, APITagView)
|
||||
from .views import TagTaggedItemListView
|
||||
|
||||
urlpatterns = patterns('tags.views',
|
||||
@@ -30,4 +30,5 @@ api_urls = patterns('',
|
||||
url(r'^tags/(?P<pk>[0-9]+)/$', APITagView.as_view(), name='tag-detail'),
|
||||
url(r'^tags/$', APITagListView.as_view(), name='tag-list'),
|
||||
url(r'^document/(?P<pk>[0-9]+)/tags/$', APIDocumentTagListView.as_view(), name='document-tag-list'),
|
||||
url(r'^document/(?P<document_pk>[0-9]+)/tags/(?P<pk>[0-9]+)/remove/$', APIDocumentTagRemoveView.as_view(), name='document-tag-remove'),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user