Refactor the document change type views to use the MultipleObjectFormActionView CBV.
This commit is contained in:
@@ -15,8 +15,9 @@ from .api_views import (
|
|||||||
from .views import (
|
from .views import (
|
||||||
ClearImageCacheView, DeletedDocumentDeleteView,
|
ClearImageCacheView, DeletedDocumentDeleteView,
|
||||||
DeletedDocumentDeleteManyView, DeletedDocumentListView,
|
DeletedDocumentDeleteManyView, DeletedDocumentListView,
|
||||||
DocumentDownloadFormView, DocumentDownloadView, DocumentEditView,
|
DocumentDocumentTypeEditView, DocumentDownloadFormView,
|
||||||
DocumentListView, DocumentPageListView, DocumentPageRotateLeftView,
|
DocumentDownloadView, DocumentEditView, DocumentListView,
|
||||||
|
DocumentPageListView, DocumentPageRotateLeftView,
|
||||||
DocumentPageRotateRightView, DocumentPageView, DocumentPageViewResetView,
|
DocumentPageRotateRightView, DocumentPageView, DocumentPageViewResetView,
|
||||||
DocumentPageZoomInView, DocumentPageZoomOutView, DocumentPreviewView,
|
DocumentPageZoomInView, DocumentPageZoomOutView, DocumentPreviewView,
|
||||||
DocumentPrint, DocumentRestoreView, DocumentRestoreManyView,
|
DocumentPrint, DocumentRestoreView, DocumentRestoreManyView,
|
||||||
@@ -28,11 +29,10 @@ from .views import (
|
|||||||
DocumentVersionDownloadFormView, DocumentVersionDownloadView,
|
DocumentVersionDownloadFormView, DocumentVersionDownloadView,
|
||||||
DocumentVersionListView, DocumentVersionRevertView, DocumentView,
|
DocumentVersionListView, DocumentVersionRevertView, DocumentView,
|
||||||
EmptyTrashCanView, RecentDocumentListView, document_clear_transformations,
|
EmptyTrashCanView, RecentDocumentListView, document_clear_transformations,
|
||||||
document_document_type_edit, document_multiple_clear_transformations,
|
document_multiple_clear_transformations,
|
||||||
document_multiple_document_type_edit, document_multiple_update_page_count,
|
document_multiple_update_page_count, document_page_navigation_first,
|
||||||
document_page_navigation_first, document_page_navigation_last,
|
document_page_navigation_last, document_page_navigation_next,
|
||||||
document_page_navigation_next, document_page_navigation_previous,
|
document_page_navigation_previous, document_update_page_count
|
||||||
document_update_page_count
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -72,11 +72,11 @@ urlpatterns = [
|
|||||||
name='document_multiple_delete'
|
name='document_multiple_delete'
|
||||||
),
|
),
|
||||||
url(
|
url(
|
||||||
r'^(?P<document_id>\d+)/type/$', document_document_type_edit,
|
r'^(?P<pk>\d+)/type/$', DocumentDocumentTypeEditView.as_view(),
|
||||||
name='document_document_type_edit'
|
name='document_document_type_edit'
|
||||||
),
|
),
|
||||||
url(
|
url(
|
||||||
r'^multiple/type/$', document_multiple_document_type_edit,
|
r'^multiple/type/$', DocumentDocumentTypeEditView.as_view(),
|
||||||
name='document_multiple_document_type_edit'
|
name='document_multiple_document_type_edit'
|
||||||
),
|
),
|
||||||
url(
|
url(
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ from django.contrib import messages
|
|||||||
from django.core.exceptions import PermissionDenied
|
from django.core.exceptions import PermissionDenied
|
||||||
from django.core.urlresolvers import reverse, reverse_lazy
|
from django.core.urlresolvers import reverse, reverse_lazy
|
||||||
from django.http import HttpResponseRedirect
|
from django.http import HttpResponseRedirect
|
||||||
from django.shortcuts import render_to_response, get_object_or_404
|
from django.shortcuts import render, render_to_response, get_object_or_404
|
||||||
from django.template import RequestContext
|
from django.template import RequestContext
|
||||||
from django.utils.http import urlencode
|
from django.utils.http import urlencode
|
||||||
from django.utils.translation import ugettext_lazy as _, ungettext
|
from django.utils.translation import ugettext_lazy as _, ungettext
|
||||||
@@ -15,8 +15,9 @@ from django.utils.translation import ugettext_lazy as _, ungettext
|
|||||||
from acls.models import AccessControlList
|
from acls.models import AccessControlList
|
||||||
from common.compressed_files import CompressedFile
|
from common.compressed_files import CompressedFile
|
||||||
from common.generics import (
|
from common.generics import (
|
||||||
ConfirmView, FormView, SingleObjectDetailView, SingleObjectDownloadView,
|
ConfirmView, FormView, MultipleObjectFormActionView,
|
||||||
SingleObjectEditView, SingleObjectListView
|
SingleObjectDetailView, SingleObjectDownloadView, SingleObjectEditView,
|
||||||
|
SingleObjectListView
|
||||||
)
|
)
|
||||||
from common.mixins import MultipleInstanceActionMixin
|
from common.mixins import MultipleInstanceActionMixin
|
||||||
from converter.models import Transformation
|
from converter.models import Transformation
|
||||||
@@ -110,6 +111,61 @@ class DeletedDocumentDeleteManyView(MultipleInstanceActionMixin, DeletedDocument
|
|||||||
success_message_plural = '%(count)d documents deleted.'
|
success_message_plural = '%(count)d documents deleted.'
|
||||||
|
|
||||||
|
|
||||||
|
class DocumentDocumentTypeEditView(MultipleObjectFormActionView):
|
||||||
|
form_class = DocumentTypeSelectForm
|
||||||
|
model = Document
|
||||||
|
object_permission = permission_document_properties_edit
|
||||||
|
success_message = _(
|
||||||
|
'Document type change request performed on %(count)d document'
|
||||||
|
)
|
||||||
|
success_message_plural = _(
|
||||||
|
'Document type change request performed on %(count)d documents'
|
||||||
|
)
|
||||||
|
|
||||||
|
def get_extra_context(self):
|
||||||
|
queryset = self.get_queryset()
|
||||||
|
|
||||||
|
result = {
|
||||||
|
'submit_label': _('Change'),
|
||||||
|
'title': ungettext(
|
||||||
|
'Change the type of the selected document',
|
||||||
|
'Change the type of the selected documents',
|
||||||
|
queryset.count()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if queryset.count() == 1:
|
||||||
|
result.update(
|
||||||
|
{
|
||||||
|
'object': queryset.first(),
|
||||||
|
'title': _(
|
||||||
|
'Change the type of the document: %s'
|
||||||
|
) % queryset.first()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
def get_form_extra_kwargs(self):
|
||||||
|
queryset = self.get_queryset()
|
||||||
|
result = {
|
||||||
|
'user': self.request.user
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
def object_action(self, form, instance):
|
||||||
|
instance.set_document_type(
|
||||||
|
form.cleaned_data['document_type'], _user=self.request.user
|
||||||
|
)
|
||||||
|
|
||||||
|
messages.success(
|
||||||
|
self.request, _(
|
||||||
|
'Document type for "%s" changed successfully.'
|
||||||
|
) % instance
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class DocumentEditView(SingleObjectEditView):
|
class DocumentEditView(SingleObjectEditView):
|
||||||
form_class = DocumentForm
|
form_class = DocumentForm
|
||||||
model = Document
|
model = Document
|
||||||
@@ -289,81 +345,6 @@ class RecentDocumentListView(DocumentListView):
|
|||||||
return RecentDocument.objects.get_for_user(self.request.user)
|
return RecentDocument.objects.get_for_user(self.request.user)
|
||||||
|
|
||||||
|
|
||||||
def document_document_type_edit(request, document_id=None, document_id_list=None):
|
|
||||||
post_action_redirect = None
|
|
||||||
|
|
||||||
if document_id:
|
|
||||||
queryset = Document.objects.filter(pk=document_id)
|
|
||||||
post_action_redirect = reverse('documents:document_list_recent')
|
|
||||||
elif document_id_list:
|
|
||||||
queryset = Document.objects.filter(pk__in=document_id_list)
|
|
||||||
|
|
||||||
queryset = AccessControlList.objects.filter_by_access(
|
|
||||||
permission_document_properties_edit, request.user, queryset=queryset
|
|
||||||
)
|
|
||||||
|
|
||||||
if not queryset:
|
|
||||||
if document_id:
|
|
||||||
raise PermissionDenied
|
|
||||||
else:
|
|
||||||
messages.error(request, _('Must provide at least one document.'))
|
|
||||||
return HttpResponseRedirect(
|
|
||||||
request.META.get('HTTP_REFERER', reverse(settings.LOGIN_REDIRECT_URL))
|
|
||||||
)
|
|
||||||
|
|
||||||
previous = request.POST.get('previous', request.GET.get('previous', request.META.get('HTTP_REFERER', reverse(settings.LOGIN_REDIRECT_URL))))
|
|
||||||
next = request.POST.get('next', request.GET.get('next', post_action_redirect if post_action_redirect else request.META.get('HTTP_REFERER', reverse(settings.LOGIN_REDIRECT_URL))))
|
|
||||||
|
|
||||||
if request.method == 'POST':
|
|
||||||
form = DocumentTypeSelectForm(request.POST, user=request.user)
|
|
||||||
if form.is_valid():
|
|
||||||
|
|
||||||
for instance in queryset:
|
|
||||||
instance.set_document_type(
|
|
||||||
form.cleaned_data['document_type'], _user=request.user
|
|
||||||
)
|
|
||||||
|
|
||||||
messages.success(
|
|
||||||
request, _(
|
|
||||||
'Document type for "%s" changed successfully.'
|
|
||||||
) % instance
|
|
||||||
)
|
|
||||||
return HttpResponseRedirect(next)
|
|
||||||
else:
|
|
||||||
form = DocumentTypeSelectForm(
|
|
||||||
initial={'document_type': queryset.first().document_type},
|
|
||||||
user=request.user
|
|
||||||
)
|
|
||||||
|
|
||||||
context = {
|
|
||||||
'form': form,
|
|
||||||
'submit_label': _('Submit'),
|
|
||||||
'previous': previous,
|
|
||||||
'next': next,
|
|
||||||
'title': ungettext(
|
|
||||||
'Change the type of the selected document.',
|
|
||||||
'Change the type of the selected documents.',
|
|
||||||
queryset.count()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if queryset.count() == 1:
|
|
||||||
context['object'] = queryset.first()
|
|
||||||
|
|
||||||
return render_to_response(
|
|
||||||
'appearance/generic_form.html', context,
|
|
||||||
context_instance=RequestContext(request)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def document_multiple_document_type_edit(request):
|
|
||||||
return document_document_type_edit(
|
|
||||||
request, document_id_list=request.GET.get(
|
|
||||||
'id_list', request.POST.get('id_list', '')
|
|
||||||
).split(',')
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class DocumentDownloadFormView(FormView):
|
class DocumentDownloadFormView(FormView):
|
||||||
form_class = DocumentDownloadForm
|
form_class = DocumentDownloadForm
|
||||||
model = Document
|
model = Document
|
||||||
|
|||||||
Reference in New Issue
Block a user