Convert document type quick label creation view to CBV.

This commit is contained in:
Roberto Rosario
2016-03-21 16:24:23 -04:00
parent 18a3773b65
commit 66d2baafb8
2 changed files with 49 additions and 45 deletions

View File

@@ -20,11 +20,11 @@ from .views import (
DocumentPageViewResetView, DocumentPreviewView, DocumentRestoreView, DocumentPageViewResetView, DocumentPreviewView, DocumentRestoreView,
DocumentRestoreManyView, DocumentTrashView, DocumentTrashManyView, DocumentRestoreManyView, DocumentTrashView, DocumentTrashManyView,
DocumentTypeCreateView, DocumentTypeDeleteView, DocumentTypeCreateView, DocumentTypeDeleteView,
DocumentTypeDocumentListView, DocumentTypeFilenameDeleteView, DocumentTypeDocumentListView, DocumentTypeFilenameCreateView,
DocumentTypeFilenameEditView, DocumentTypeFilenameListView, DocumentTypeFilenameDeleteView, DocumentTypeFilenameEditView,
DocumentTypeListView, DocumentTypeEditView, DocumentVersionListView, DocumentTypeFilenameListView, DocumentTypeListView, DocumentTypeEditView,
DocumentVersionRevertView, DocumentView, EmptyTrashCanView, DocumentVersionListView, DocumentVersionRevertView, DocumentView,
RecentDocumentListView EmptyTrashCanView, RecentDocumentListView
) )
urlpatterns = patterns( urlpatterns = patterns(
@@ -234,8 +234,9 @@ urlpatterns = patterns(
name='document_type_filename_delete' name='document_type_filename_delete'
), ),
url( url(
r'^type/(?P<document_type_id>\d+)/filename/create/$', r'^type/(?P<pk>\d+)/filename/create/$',
'document_type_filename_create', name='document_type_filename_create' DocumentTypeFilenameCreateView.as_view(),
name='document_type_filename_create'
), ),
) )

View File

@@ -444,9 +444,23 @@ class DocumentTypeEditView(SingleObjectEditView):
} }
class DocumentTypeFilenameListView(SingleObjectListView): class DocumentTypeFilenameCreateView(SingleObjectCreateView):
model = DocumentType form_class = DocumentTypeFilenameForm_create
view_permission = permission_document_type_view
def dispatch(self, request, *args, **kwargs):
try:
Permission.check_permissions(
request.user, (permission_document_type_edit,)
)
except PermissionDenied:
AccessControlList.objects.check_access(
permission_document_type_edit, request.user,
self.get_document_type()
)
return super(DocumentTypeFilenameCreateView, self).dispatch(
request, *args, **kwargs
)
def get_document_type(self): def get_document_type(self):
return get_object_or_404(DocumentType, pk=self.kwargs['pk']) return get_object_or_404(DocumentType, pk=self.kwargs['pk'])
@@ -454,15 +468,14 @@ class DocumentTypeFilenameListView(SingleObjectListView):
def get_extra_context(self): def get_extra_context(self):
return { return {
'document_type': self.get_document_type(), 'document_type': self.get_document_type(),
'hide_link': True,
'navigation_object_list': ('document_type',), 'navigation_object_list': ('document_type',),
'title': _( 'title': _(
'Quick labels for document type: %s' 'Create quick label for document type: %s'
) % self.get_document_type(), ) % self.get_document_type(),
} }
def get_queryset(self): def get_instance_extra_data(self):
return self.get_document_type().filenames.all() return {'document_type': self.get_document_type()}
class DocumentTypeFilenameEditView(SingleObjectEditView): class DocumentTypeFilenameEditView(SingleObjectEditView):
@@ -518,6 +531,27 @@ class DocumentTypeFilenameDeleteView(SingleObjectDeleteView):
) )
class DocumentTypeFilenameListView(SingleObjectListView):
model = DocumentType
view_permission = permission_document_type_view
def get_document_type(self):
return get_object_or_404(DocumentType, pk=self.kwargs['pk'])
def get_extra_context(self):
return {
'document_type': self.get_document_type(),
'hide_link': True,
'navigation_object_list': ('document_type',),
'title': _(
'Quick labels for document type: %s'
) % self.get_document_type(),
}
def get_queryset(self):
return self.get_document_type().filenames.all()
class DocumentVersionListView(SingleObjectListView): class DocumentVersionListView(SingleObjectListView):
def dispatch(self, request, *args, **kwargs): def dispatch(self, request, *args, **kwargs):
try: try:
@@ -1188,34 +1222,3 @@ def document_print(request, document_id):
'title': _('Print: %s') % document, 'title': _('Print: %s') % document,
'submit_label': _('Submit'), 'submit_label': _('Submit'),
}, context_instance=RequestContext(request)) }, context_instance=RequestContext(request))
def document_type_filename_create(request, document_type_id):
Permission.check_permissions(request.user, (permission_document_type_edit,))
document_type = get_object_or_404(DocumentType, pk=document_type_id)
if request.method == 'POST':
form = DocumentTypeFilenameForm_create(request.POST)
if form.is_valid():
try:
document_type_filename = DocumentTypeFilename(
document_type=document_type,
filename=form.cleaned_data['filename'],
enabled=True
)
document_type_filename.save()
messages.success(request, _('Document type quick label created successfully'))
return HttpResponseRedirect(reverse('documents:document_type_filename_list', args=(document_type_id,)))
except Exception as exception:
messages.error(request, _('Error creating document type quick label; %(error)s') % {
'error': exception})
else:
form = DocumentTypeFilenameForm_create()
return render_to_response('appearance/generic_form.html', {
'document_type': document_type,
'form': form,
'navigation_object_list': ('document_type',),
'title': _('Create quick label for document type: %s') % document_type,
}, context_instance=RequestContext(request))