Convert document smart link list view to CBV.

This commit is contained in:
Roberto Rosario
2015-07-02 01:13:34 -04:00
parent daa6777592
commit dda604dcbb
2 changed files with 46 additions and 44 deletions

View File

@@ -2,14 +2,17 @@ from __future__ import unicode_literals
from django.conf.urls import patterns, url from django.conf.urls import patterns, url
from .views import SetupSmartLinkDocumentTypesView from .views import (
DocumentSmartLinkListView, SetupSmartLinkDocumentTypesView,
SmartLinkListView
)
urlpatterns = patterns( urlpatterns = patterns(
'linking.views', 'linking.views',
url(r'^document/(?P<document_id>\d+)/$', 'smart_link_instances_for_document', name='smart_link_instances_for_document'), url(r'^document/(?P<pk>\d+)/list/$', DocumentSmartLinkListView.as_view(), name='smart_link_instances_for_document'),
url(r'^document/(?P<document_id>\d+)/smart_link/(?P<smart_link_pk>\d+)/$', 'smart_link_instance_view', name='smart_link_instance_view'), url(r'^document/(?P<document_id>\d+)/(?P<smart_link_pk>\d+)/$', 'smart_link_instance_view', name='smart_link_instance_view'),
url(r'^setup/list/$', 'smart_link_list', name='smart_link_list'), url(r'^setup/list/$', SmartLinkListView.as_view(), name='smart_link_list'),
url(r'^setup/create/$', 'smart_link_create', name='smart_link_create'), url(r'^setup/create/$', 'smart_link_create', name='smart_link_create'),
url(r'^setup/(?P<smart_link_pk>\d+)/delete/$', 'smart_link_delete', name='smart_link_delete'), url(r'^setup/(?P<smart_link_pk>\d+)/delete/$', 'smart_link_delete', name='smart_link_delete'),
url(r'^setup/(?P<smart_link_pk>\d+)/edit/$', 'smart_link_edit', name='smart_link_edit'), url(r'^setup/(?P<smart_link_pk>\d+)/edit/$', 'smart_link_edit', name='smart_link_edit'),

View File

@@ -13,7 +13,7 @@ from django.utils.translation import ugettext_lazy as _
from acls.models import AccessControlList from acls.models import AccessControlList
from common.utils import encapsulate from common.utils import encapsulate
from common.views import AssignRemoveView from common.views import AssignRemoveView, SingleObjectListView
from common.widgets import two_state_template from common.widgets import two_state_template
from documents.models import Document, DocumentType from documents.models import Document, DocumentType
from documents.permissions import permission_document_view from documents.permissions import permission_document_view
@@ -88,54 +88,53 @@ def smart_link_instance_view(request, document_id, smart_link_pk):
) )
def smart_link_instances_for_document(request, document_id): class SmartLinkListView(SingleObjectListView):
document = get_object_or_404(Document, pk=document_id) object_permission = permission_smart_link_view
queryset = ResolvedSmartLink.objects.filter(document_types=document.document_type)
def get_queryset(self):
self.queryset = self.get_smart_link_queryset()
return super(SmartLinkListView, self).get_queryset()
def get_smart_link_queryset(self):
return SmartLink.objects.all()
def get_extra_context(self):
return {
'extra_columns': [
{'name': _('Dynamic title'), 'attribute': 'dynamic_title'},
{'name': _('Enabled'), 'attribute': encapsulate(lambda instance: two_state_template(instance.enabled))},
],
'hide_link': True,
'title': _('Smart links'),
}
class DocumentSmartLinkListView(SmartLinkListView):
def dispatch(self, request, *args, **kwargs):
self.document = get_object_or_404(Document, pk=self.kwargs['pk'])
try: try:
Permission.check_permissions(request.user, (permission_document_view,)) Permission.check_permissions(request.user, (permission_document_view,))
except PermissionDenied: except PermissionDenied:
smart_links = AccessControlList.objects.filter_by_access(permission_document_view, request.user, queryset) AccessControlList.objects.check_permissions(permission_document_view, request.user, self.document)
else:
smart_links = queryset
context = { return super(DocumentSmartLinkListView, self).dispatch(request, *args, **kwargs)
'document': document,
def get_smart_link_queryset(self):
return ResolvedSmartLink.objects.filter(document_types=self.document.document_type, enabled=True)
def get_extra_context(self):
return {
'document': self.document,
'extra_columns': ( 'extra_columns': (
{'name': _('Title'), 'attribute': encapsulate(lambda smart_link: smart_link.get_dynamic_title(document))}, {'name': _('Title'), 'attribute': encapsulate(lambda smart_link: smart_link.get_dynamic_title(self.document))},
), ),
'hide_object': True, 'hide_object': True,
'hide_link': True, 'hide_link': True,
'object': document, 'object': self.document,
'object_list': smart_links, 'title': _('Smart links for document: %s') % self.document,
'title': _('Smart links for document: %s') % document,
} }
return render_to_response(
'appearance/generic_list.html', context,
context_instance=RequestContext(request)
)
def smart_link_list(request):
qs = SmartLink.objects.all()
try:
Permission.check_permissions(request.user, [permission_smart_link_view])
except PermissionDenied:
qs = AccessControlList.objects.filter_by_access(permission_smart_link_view, request.user, qs)
return render_to_response('appearance/generic_list.html', {
'title': _('Smart links'),
'object_list': qs,
'extra_columns': [
{'name': _('Dynamic title'), 'attribute': 'dynamic_title'},
{'name': _('Enabled'), 'attribute': encapsulate(lambda x: two_state_template(x.enabled))},
],
'hide_link': True,
}, context_instance=RequestContext(request))
def smart_link_create(request): def smart_link_create(request):
Permission.check_permissions(request.user, [permission_smart_link_create]) Permission.check_permissions(request.user, [permission_smart_link_create])