Fix tag widget and add tests
Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
@@ -117,7 +117,9 @@ class APIDocumentTagListView(generics.ListCreateAPIView):
|
||||
user=self.request.user
|
||||
)
|
||||
|
||||
return document.attached_tags().all()
|
||||
return document.get_tags(
|
||||
permission=permission_tag_view, user=self.request.user
|
||||
).all()
|
||||
|
||||
def get_serializer(self, *args, **kwargs):
|
||||
if not self.request:
|
||||
@@ -162,7 +164,9 @@ class APIDocumentTagView(generics.RetrieveDestroyAPIView):
|
||||
serializer_class = DocumentTagSerializer
|
||||
|
||||
def get_document(self):
|
||||
document = get_object_or_404(klass=Document, pk=self.kwargs['document_pk'])
|
||||
document = get_object_or_404(
|
||||
klass=Document, pk=self.kwargs['document_pk']
|
||||
)
|
||||
|
||||
AccessControlList.objects.check_access(
|
||||
obj=document, permissions=(permission_document_view,),
|
||||
@@ -171,7 +175,7 @@ class APIDocumentTagView(generics.RetrieveDestroyAPIView):
|
||||
return document
|
||||
|
||||
def get_queryset(self):
|
||||
return self.get_document().attached_tags().all()
|
||||
return self.get_document().tags.all()
|
||||
|
||||
def get_serializer(self, *args, **kwargs):
|
||||
if not self.request:
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
from __future__ import absolute_import, unicode_literals
|
||||
|
||||
from django.apps import apps
|
||||
from django.template.loader import render_to_string
|
||||
|
||||
from .permissions import permission_tag_view
|
||||
@@ -10,17 +9,12 @@ def widget_document_tags(document, user):
|
||||
"""
|
||||
A tag widget that displays the tags for the given document
|
||||
"""
|
||||
AccessControlList = apps.get_model(
|
||||
app_label='acls', model_name='AccessControlList'
|
||||
)
|
||||
|
||||
tags = AccessControlList.objects.restrict_queryset(
|
||||
permission=permission_tag_view, queryset=document.attached_tags().all(),
|
||||
user=user
|
||||
)
|
||||
|
||||
return render_to_string(
|
||||
template_name='tags/document_tags_widget.html', context={'tags': tags}
|
||||
template_name='tags/document_tags_widget.html', context={
|
||||
'tags': document.get_tags(
|
||||
permission=permission_tag_view, user=user
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -162,10 +162,12 @@ class TagDocumentViewTestCase(TagTestMixin, TagViewTestMixin, GenericDocumentVie
|
||||
|
||||
self.test_tag.documents.add(self.test_document)
|
||||
|
||||
self.grant_access(obj=self.test_tag, permission=permission_tag_view)
|
||||
self.grant_access(
|
||||
obj=self.test_document, permission=permission_tag_view
|
||||
)
|
||||
self.grant_access(
|
||||
obj=self.test_tag, permission=permission_tag_view
|
||||
)
|
||||
|
||||
response = self._request_test_document_tag_list_view()
|
||||
self.assertContains(
|
||||
|
||||
80
mayan/apps/tags/tests/test_widgets.py
Normal file
80
mayan/apps/tags/tests/test_widgets.py
Normal file
@@ -0,0 +1,80 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.utils.encoding import force_text
|
||||
|
||||
from mayan.apps.documents.tests import GenericDocumentViewTestCase
|
||||
from mayan.apps.documents.tests.mixins import DocumentViewTestMixin
|
||||
from mayan.apps.documents.permissions import permission_document_view
|
||||
|
||||
from ..permissions import permission_tag_view
|
||||
|
||||
from .mixins import TagTestMixin
|
||||
|
||||
|
||||
class DocumentTagHTMLWidgetTestCase(DocumentViewTestMixin, TagTestMixin, GenericDocumentViewTestCase):
|
||||
def test_document_tags_widget_no_permissions(self):
|
||||
self._create_test_tag()
|
||||
|
||||
self.test_tag.documents.add(self.test_document)
|
||||
|
||||
response = self._request_test_document_list_view()
|
||||
self.assertNotContains(
|
||||
response=response, text=self.test_document.label, status_code=200
|
||||
)
|
||||
self.assertNotContains(
|
||||
response=response, text=self.test_tag.label, status_code=200
|
||||
)
|
||||
|
||||
def test_document_tags_widget_with_document_access(self):
|
||||
self._create_test_tag()
|
||||
|
||||
self.test_tag.documents.add(self.test_document)
|
||||
|
||||
self.grant_access(
|
||||
obj=self.test_document, permission=permission_document_view
|
||||
)
|
||||
|
||||
response = self._request_test_document_list_view()
|
||||
self.assertContains(
|
||||
response=response, text=self.test_document.label, status_code=200
|
||||
)
|
||||
self.assertNotContains(
|
||||
response=response, text=force_text(self.test_tag), status_code=200
|
||||
)
|
||||
|
||||
def test_document_tags_widget_with_tag_access(self):
|
||||
self._create_test_tag()
|
||||
|
||||
self.test_tag.documents.add(self.test_document)
|
||||
|
||||
self.grant_access(
|
||||
obj=self.test_tag, permission=permission_tag_view
|
||||
)
|
||||
|
||||
response = self._request_test_document_list_view()
|
||||
self.assertNotContains(
|
||||
response=response, text=self.test_document.label, status_code=200
|
||||
)
|
||||
self.assertNotContains(
|
||||
response=response, text=self.test_tag.label, status_code=200
|
||||
)
|
||||
|
||||
def test_document_tags_widget_with_full_access(self):
|
||||
self._create_test_tag()
|
||||
|
||||
self.test_tag.documents.add(self.test_document)
|
||||
|
||||
self.grant_access(
|
||||
obj=self.test_document, permission=permission_document_view
|
||||
)
|
||||
self.grant_access(
|
||||
obj=self.test_tag, permission=permission_tag_view
|
||||
)
|
||||
|
||||
response = self._request_test_document_list_view()
|
||||
self.assertContains(
|
||||
response=response, text=self.test_document.label, status_code=200
|
||||
)
|
||||
self.assertContains(
|
||||
response=response, text=self.test_tag.label, status_code=200
|
||||
)
|
||||
Reference in New Issue
Block a user