Finish tags app API refactor. Add tags app API tests.
This commit is contained in:
@@ -4,6 +4,7 @@ from django.core.exceptions import PermissionDenied
|
||||
from django.shortcuts import get_object_or_404
|
||||
|
||||
from rest_framework import generics, status, views
|
||||
from rest_framework.exceptions import ValidationError
|
||||
from rest_framework.response import Response
|
||||
|
||||
from acls.models import AccessControlList
|
||||
@@ -134,6 +135,17 @@ class APIDocumentTagListView(generics.ListCreateAPIView):
|
||||
|
||||
return document.tags.all()
|
||||
|
||||
def get_serializer_context(self):
|
||||
"""
|
||||
Extra context provided to the serializer class.
|
||||
"""
|
||||
return {
|
||||
'format': self.format_kwarg,
|
||||
'request': self.request,
|
||||
'document': self.get_document(),
|
||||
'view': self
|
||||
}
|
||||
|
||||
def get_serializer_class(self):
|
||||
if self.request.method == 'GET':
|
||||
return DocumentTagSerializer
|
||||
@@ -152,39 +164,62 @@ class APIDocumentTagListView(generics.ListCreateAPIView):
|
||||
|
||||
|
||||
class APIDocumentTagView(generics.RetrieveDestroyAPIView):
|
||||
serialize_class = DocumentTagSerializer
|
||||
filter_backends = (MayanObjectPermissionsFilter,)
|
||||
mayan_object_permissions = {
|
||||
'GET': (permission_tag_view,),
|
||||
'DELETE': (permission_tag_remove,)
|
||||
}
|
||||
serializer_class = DocumentTagSerializer
|
||||
|
||||
def get_folder(self):
|
||||
return get_object_or_404(Document, pk=self.kwargs['document_pk'])
|
||||
def delete(self, request, *args, **kwargs):
|
||||
"""
|
||||
Remove a tag from the selected document.
|
||||
"""
|
||||
|
||||
return super(APIDocumentTagView, self).delete(request, *args, **kwargs)
|
||||
|
||||
def get(self, *args, **kwargs):
|
||||
"""
|
||||
Returns the details of the selected document tag.
|
||||
"""
|
||||
|
||||
return super(APIDocumentTagView, self).get(*args, **kwargs)
|
||||
|
||||
def get_document(self):
|
||||
document = get_object_or_404(Document, pk=self.kwargs['document_pk'])
|
||||
|
||||
try:
|
||||
Permission.check_permissions(
|
||||
self.request.user, (permission_document_view,)
|
||||
)
|
||||
except PermissionDenied:
|
||||
documents = AccessControlList.objects.check_access(
|
||||
permission_document_view, self.request.user, document
|
||||
)
|
||||
return document
|
||||
|
||||
def get_queryset(self):
|
||||
return self.get_document().tags.all()
|
||||
|
||||
def get_serializer_context(self):
|
||||
"""
|
||||
Extra context provided to the serializer class.
|
||||
"""
|
||||
return {
|
||||
'document': self.get_document(),
|
||||
'format': self.format_kwarg,
|
||||
'request': self.request,
|
||||
'document': self.get_document(),
|
||||
'view': self
|
||||
}
|
||||
|
||||
'''
|
||||
def delete(self, request, *args, **kwargs):
|
||||
"""
|
||||
Remove a tag from a document.
|
||||
"""
|
||||
|
||||
document = get_object_or_404(Document, pk=self.kwargs['document_pk'])
|
||||
|
||||
def perform_destroy(self, instance):
|
||||
try:
|
||||
Permission.check_permissions(request.user, (permission_tag_remove,))
|
||||
except PermissionDenied:
|
||||
AccessControlList.objects.check_access(
|
||||
permission_tag_remove, request.user, document
|
||||
)
|
||||
instance.documents.remove(self.get_document())
|
||||
except Exception as exception:
|
||||
raise ValidationError(exception)
|
||||
|
||||
tag = get_object_or_404(Tag, pk=self.kwargs['pk'])
|
||||
tag.documents.remove(document)
|
||||
return Response(status=status.HTTP_204_NO_CONTENT)
|
||||
def retrieve(self, request, *args, **kwargs):
|
||||
instance = self.get_object()
|
||||
|
||||
'''
|
||||
serializer = self.get_serializer(instance)
|
||||
return Response(serializer.data)
|
||||
|
||||
@@ -56,7 +56,7 @@ class DocumentTagSerializer(TagSerializer):
|
||||
def get_remove(self, instance):
|
||||
return reverse(
|
||||
'rest_api:document-tag', args=(
|
||||
self.context['document'].pk, instance.pk
|
||||
self.context['document'].pk, instance.pk,
|
||||
), request=self.context['request'], format=self.context['format']
|
||||
)
|
||||
|
||||
|
||||
@@ -99,7 +99,6 @@ class TagAPITestCase(APITestCase):
|
||||
|
||||
self.assertEqual(tag.documents.count(), 1)
|
||||
|
||||
"""
|
||||
def test_tag_remove_document(self):
|
||||
tag = Tag.objects.create(color=TEST_TAG_COLOR, label=TEST_TAG_LABEL)
|
||||
|
||||
@@ -119,8 +118,7 @@ class TagAPITestCase(APITestCase):
|
||||
tag.documents.add(document)
|
||||
|
||||
self.client.delete(
|
||||
reverse('rest_api:tag-document', args=(tag.pk, document.pk)),
|
||||
reverse('rest_api:document-tag', args=(document.pk, tag.pk)),
|
||||
)
|
||||
|
||||
self.assertEqual(tag.documents.count(), 0)
|
||||
"""
|
||||
|
||||
@@ -54,9 +54,18 @@ urlpatterns = patterns(
|
||||
|
||||
api_urls = patterns(
|
||||
'',
|
||||
url(r'^tags/(?P<pk>[0-9]+)/documents/$', APITagDocumentListView.as_view(), name='tag-document-list'),
|
||||
url(
|
||||
r'^tags/(?P<pk>[0-9]+)/documents/$', APITagDocumentListView.as_view(),
|
||||
name='tag-document-list'
|
||||
),
|
||||
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]+)/$', APIDocumentTagView.as_view(), name='document-tag'),
|
||||
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]+)/$',
|
||||
APIDocumentTagView.as_view(), name='document-tag'
|
||||
),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user