Simplify the API to attach or remove tags to a document

This commit is contained in:
Roberto Rosario
2014-10-18 01:27:59 -04:00
parent fe7675f101
commit 9fc4d03b35
2 changed files with 16 additions and 35 deletions
+13 -30
View File
@@ -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)
+3 -5
View File
@@ -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<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'),
url(r'^document/(?P<document_pk>[0-9]+)/tags/(?P<pk>[0-9]+)/add/$', APIDocumentTagAddView.as_view(), name='document-tag-add'),
url(r'^document/(?P<document_pk>[0-9]+)/tags/(?P<pk>[0-9]+)/$', APIDocumentTagView.as_view(), name='document-tag'),
)