From d47091df61d1a43893c549c693cc4be5ef4fb453 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Thu, 2 Jul 2015 01:26:35 -0400 Subject: [PATCH] Convert resolved smart link document list view to CBV. --- mayan/apps/linking/urls.py | 6 ++--- mayan/apps/linking/views.py | 54 ++++++++++++++++++++++--------------- 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/mayan/apps/linking/urls.py b/mayan/apps/linking/urls.py index 3c006ceeef..fba66139c3 100644 --- a/mayan/apps/linking/urls.py +++ b/mayan/apps/linking/urls.py @@ -3,14 +3,14 @@ from __future__ import unicode_literals from django.conf.urls import patterns, url from .views import ( - DocumentSmartLinkListView, SetupSmartLinkDocumentTypesView, - SmartLinkListView + DocumentSmartLinkListView, ResolvedSmartLinkView, + SetupSmartLinkDocumentTypesView, SmartLinkListView ) urlpatterns = patterns( 'linking.views', url(r'^document/(?P\d+)/list/$', DocumentSmartLinkListView.as_view(), name='smart_link_instances_for_document'), - url(r'^document/(?P\d+)/(?P\d+)/$', 'smart_link_instance_view', name='smart_link_instance_view'), + url(r'^document/(?P\d+)/(?P\d+)/$', ResolvedSmartLinkView.as_view(), name='smart_link_instance_view'), url(r'^setup/list/$', SmartLinkListView.as_view(), name='smart_link_list'), url(r'^setup/create/$', 'smart_link_create', name='smart_link_create'), diff --git a/mayan/apps/linking/views.py b/mayan/apps/linking/views.py index 02d98ea6a7..ba35753d57 100644 --- a/mayan/apps/linking/views.py +++ b/mayan/apps/linking/views.py @@ -17,7 +17,7 @@ from common.views import AssignRemoveView, SingleObjectListView from common.widgets import two_state_template from documents.models import Document, DocumentType from documents.permissions import permission_document_view -from documents.views import document_list +from documents.views import DocumentListView from permissions import Permission from .forms import SmartLinkConditionForm, SmartLinkForm @@ -61,31 +61,43 @@ class SetupSmartLinkDocumentTypesView(AssignRemoveView): return data -def smart_link_instance_view(request, document_id, smart_link_pk): - document = get_object_or_404(Document, pk=document_id) - smart_link = get_object_or_404(SmartLink, pk=smart_link_pk) +class ResolvedSmartLinkView(DocumentListView): + def dispatch(self, request, *args, **kwargs): + self.document = get_object_or_404(Document, pk=self.kwargs['document_pk']) + self.smart_link = get_object_or_404(SmartLink, pk=self.kwargs['smart_link_pk']) - try: - Permission.check_permissions(request.user, [permission_smart_link_view]) - except PermissionDenied: - AccessControlList.objects.check_access(permission_smart_link_view, request.user, smart_link) + try: + Permission.check_permissions(request.user, [permission_document_view]) + except PermissionDenied: + AccessControlList.objects.check_access(permission_document_view, request.user, self.document) - try: - object_list = smart_link.get_linked_document_for(document) - except Exception as exception: - object_list = Document.objects.none() + try: + Permission.check_permissions(request.user, [permission_smart_link_view]) + except PermissionDenied: + AccessControlList.objects.check_access(permission_smart_link_view, request.user, self.smart_link) - if request.user.is_staff or request.user.is_superuser: - messages.error(request, _('Smart link query error: %s' % exception)) + return super(ResolvedSmartLinkView, self).dispatch(request, *args, **kwargs) - return document_list( - request, - title=_('Documents in smart link: %s') % smart_link.get_dynamic_title(document), - object_list=object_list, - extra_context={ - 'object': document + def get_document_queryset(self): + try: + queryset = self.smart_link.get_linked_document_for(self.document) + except Exception as exception: + queryset = Document.objects.none() + + if self.request.user.is_staff or self.request.user.is_superuser: + messages.error(request, _('Smart link query error: %s' % exception)) + + return queryset + + def get_extra_context(self): + return { + 'hide_links': True, + 'object': self.document, + 'title': _('Documents in smart link "%(smart_link)s" as relation to "%(document)s"') % { + 'document': self.document, + 'smart_link': self.smart_link.get_dynamic_title(self.document), + } } - ) class SmartLinkListView(SingleObjectListView):