Replace index, workflow, document and statistics views that use 'object_list' to SingleObjectListView class.

This commit is contained in:
Roberto Rosario
2015-08-15 01:07:52 -04:00
parent c6b20f13cc
commit c84ac0a2fc
5 changed files with 121 additions and 111 deletions
+7 -7
View File
@@ -8,9 +8,9 @@ from .api_views import (
APIIndexTemplateView, APIIndexView APIIndexTemplateView, APIIndexView
) )
from .views import ( from .views import (
IndexInstanceNodeView, IndexListView, SetupIndexDocumentTypesView, DocumentIndexNodeListView, IndexInstanceNodeView, IndexListView,
SetupIndexCreateView, SetupIndexDeleteView, SetupIndexEditView, SetupIndexDocumentTypesView, SetupIndexCreateView, SetupIndexDeleteView,
SetupIndexListView SetupIndexEditView, SetupIndexListView, SetupIndexTreeTemplateListView
) )
urlpatterns = patterns( urlpatterns = patterns(
@@ -32,8 +32,8 @@ urlpatterns = patterns(
name='index_setup_delete' name='index_setup_delete'
), ),
url( url(
r'^setup/index/(?P<index_pk>\d+)/view/$', 'index_setup_view', r'^setup/index/(?P<pk>\d+)/template/$',
name='index_setup_view' SetupIndexTreeTemplateListView.as_view(), name='index_setup_view'
), ),
url( url(
r'^setup/index/(?P<pk>\d+)/document_types/$', r'^setup/index/(?P<pk>\d+)/document_types/$',
@@ -63,8 +63,8 @@ urlpatterns = patterns(
name='rebuild_index_instances' name='rebuild_index_instances'
), ),
url( url(
r'^list/for/document/(?P<document_id>\d+)/$', 'document_index_list', r'^list/for/document/(?P<pk>\d+)/$',
name='document_index_list' DocumentIndexNodeListView.as_view(), name='document_index_list'
), ),
) )
+68 -67
View File
@@ -88,49 +88,39 @@ class SetupIndexDeleteView(SingleObjectDeleteView):
} }
def index_setup_view(request, index_pk): class SetupIndexTreeTemplateListView(SingleObjectListView):
index = get_object_or_404(Index, pk=index_pk) view_permission = permission_document_indexing_setup
try: def get_index(self):
Permission.check_permissions( return get_object_or_404(Index, pk=self.kwargs['pk'])
request.user, (permission_document_indexing_setup,)
)
except PermissionDenied:
AccessControlList.objects.check_access(
permission_document_indexing_setup, request.user, index
)
object_list = index.template_root.get_descendants(include_self=True) def get_queryset(self):
return self.get_index().template_root.get_descendants(include_self=True)
context = { def get_extra_context(self):
'object_list': object_list, return {
'index': index, 'extra_columns': (
'navigation_object_list': ('index',), {
'title': _('Tree template nodes for index: %s') % index, 'name': _('Level'), 'attribute': encapsulate(
'hide_object': True, lambda node: node_level(node)
'extra_columns': [ )
{ },
'name': _('Level'), 'attribute': encapsulate( {
lambda x: node_level(x) 'name': _('Enabled'), 'attribute': encapsulate(
) lambda node: two_state_template(node.enabled)
}, )
{ },
'name': _('Enabled'), 'attribute': encapsulate( {
lambda x: two_state_template(x.enabled) 'name': _('Has document links?'), 'attribute': encapsulate(
) lambda node: two_state_template(node.link_documents)
}, )
{ },
'name': _('Has document links?'), 'attribute': encapsulate( ),
lambda x: two_state_template(x.link_documents) 'hide_object': True,
) 'index': self.get_index(),
}, 'navigation_object_list': ('index',),
], 'title': _('Tree template nodes for index: %s') % self.get_index(),
} }
return render_to_response(
'appearance/generic_list.html', context,
context_instance=RequestContext(request)
)
class SetupIndexDocumentTypesView(AssignRemoveView): class SetupIndexDocumentTypesView(AssignRemoveView):
@@ -434,38 +424,49 @@ def rebuild_index_instances(request):
return HttpResponseRedirect(next) return HttpResponseRedirect(next)
def document_index_list(request, document_id): class DocumentIndexNodeListView(SingleObjectListView):
""" """
Show a list of indexes where the current document can be found Show a list of indexes where the current document can be found
""" """
document = get_object_or_404(Document, pk=document_id) object_permission = permission_document_indexing_view
object_list = [] object_permission_related = 'index'
queryset = document.node_instances.all() def dispatch(self, request, *args, **kwargs):
try: try:
# TODO: should be AND not OR Permission.check_permissions(
Permission.check_permissions( request.user, (permission_document_view,)
request.user, (
permission_document_view, permission_document_indexing_view
) )
) except PermissionDenied:
except PermissionDenied: AccessControlList.objects.check_access(
queryset = AccessControlList.objects.filter_by_access( permission_document_view, request.user, self.get_document()
permission_document_indexing_view, request.user, queryset,
related='index'
)
for index_instance in queryset:
object_list.append(
get_breadcrumbs(
index_instance, single_link=True, include_count=True
) )
)
return render_to_response('appearance/generic_list.html', { return super(
'object_list': object_list, DocumentIndexNodeListView, self
'object': document, ).dispatch(request, *args, **kwargs)
'hide_link': True,
'title': _('Indexes containing document: %s') % document, def get_document(self):
}, context_instance=RequestContext(request)) return get_object_or_404(Document, pk=self.kwargs['pk'])
def get_extra_context(self):
return {
'extra_columns': (
{
'name': _('Node'),
'attribute': encapsulate(
lambda node: get_breadcrumbs(
index_instance_node=node, single_link=True, include_count=True
)
)
},
),
'hide_object': True,
'object': self.get_document(),
'title': _(
'Indexes nodes containing document: %s'
) % self.get_document(),
}
def get_queryset(self):
return self.get_document().node_instances.all()
+7 -10
View File
@@ -115,25 +115,22 @@ class WorkflowInstanceDetailView(SingleObjectListView):
WorkflowInstanceDetailView, self WorkflowInstanceDetailView, self
).dispatch(request, *args, **kwargs) ).dispatch(request, *args, **kwargs)
def get_workflow_instance(self): def get_extra_context(self):
return get_object_or_404(WorkflowInstance, pk=self.kwargs['pk']) return {
def get_queryset(self):
return self.get_workflow_instance().log_entries.order_by('-datetime')
def get_context_data(self, **kwargs):
context = {
'hide_object': True, 'hide_object': True,
'navigation_object_list': ('object', 'workflow_instance'), 'navigation_object_list': ('object', 'workflow_instance'),
'object': self.get_workflow_instance().document, 'object': self.get_workflow_instance().document,
'object_list': self.get_queryset(),
'title': _('Detail of workflow: %(workflow)s') % { 'title': _('Detail of workflow: %(workflow)s') % {
'workflow': self.get_workflow_instance() 'workflow': self.get_workflow_instance()
}, },
'workflow_instance': self.get_workflow_instance(), 'workflow_instance': self.get_workflow_instance(),
} }
return context def get_queryset(self):
return self.get_workflow_instance().log_entries.order_by('-datetime')
def get_workflow_instance(self):
return get_object_or_404(WorkflowInstance, pk=self.kwargs['pk'])
class WorkflowInstanceTransitionView(FormView): class WorkflowInstanceTransitionView(FormView):
+3 -1
View File
@@ -1009,10 +1009,12 @@ class DocumentTypeFilenameListView(SingleObjectListView):
), ),
'hide_link': True, 'hide_link': True,
'navigation_object_list': ('document_type',), 'navigation_object_list': ('document_type',),
'object_list': self.get_document_type().filenames.all(),
'title': _('Filenames for document type: %s') % self.get_document_type(), 'title': _('Filenames for document type: %s') % self.get_document_type(),
} }
def get_queryset(self):
return self.get_document_type().filenames.all()
class DocumentTypeFilenameEditView(SingleObjectEditView): class DocumentTypeFilenameEditView(SingleObjectEditView):
fields = ('enabled', 'filename',) fields = ('enabled', 'filename',)
+36 -26
View File
@@ -1,52 +1,62 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from django.http import Http404
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from common.views import SimpleView from common.views import SingleObjectListView
from .classes import Statistic, StatisticNamespace from .classes import Statistic, StatisticNamespace
from .permissions import permission_statistics_view from .permissions import permission_statistics_view
class NamespaceListView(SimpleView): class NamespaceListView(SingleObjectListView):
extra_context = {
'hide_link': True,
'title': _('Statistics namespaces'),
}
template_name = 'appearance/generic_list.html'
view_permission = permission_statistics_view
def get_queryset(self):
return StatisticNamespace.get_all()
class NamespaceDetailView(SingleObjectListView):
template_name = 'appearance/generic_list.html' template_name = 'appearance/generic_list.html'
view_permission = permission_statistics_view view_permission = permission_statistics_view
def get_extra_context(self): def get_extra_context(self):
return { return {
'hide_link': True, 'hide_link': True,
'object_list': StatisticNamespace.get_all(), 'object': self.get_namespace(),
'title': _('Statistics namespaces'), 'title': _('Namespace details for: %s') % self.get_namespace(),
} }
def get_namespace(self):
return StatisticNamespace.get(self.kwargs['namespace_id'])
class NamespaceDetailView(SimpleView): def get_queryset(self):
return self.get_namespace().statistics
class StatisticExecute(SingleObjectListView):
template_name = 'appearance/generic_list.html' template_name = 'appearance/generic_list.html'
view_permission = permission_statistics_view view_permission = permission_statistics_view
def get_extra_context(self): def get_extra_context(self):
namespace = StatisticNamespace.get(self.kwargs['namespace_id'])
return { return {
'hide_link': True, 'hide_link': True,
'object': namespace, 'namespace': self.get_statictic().namespace,
'object_list': namespace.statistics,
'title': _('Namespace details for: %s') % namespace,
}
class StatisticExecute(SimpleView):
template_name = 'appearance/generic_list.html'
view_permission = permission_statistics_view
def get_extra_context(self):
statictic = Statistic.get(self.kwargs['statistic_id'])
return {
'hide_link': True,
'namespace': statictic.namespace,
'navigation_object_list': ('namespace', 'object'), 'navigation_object_list': ('namespace', 'object'),
'object': statictic, 'object': self.get_statictic(),
'object_list': statictic.get_results(), 'title': _('Results for: %s') % self.get_statictic(),
'title': _('Results for: %s') % statictic,
} }
def get_queryset(self):
return self.get_statictic().get_results()
def get_statictic(self):
try:
return Statistic.get(self.kwargs['statistic_id'])
except KeyError:
raise Http404(_('Statistic "%s" not found.') % self.kwargs['statistic_id'])