From 66a49ccefc67aaf5b621d1e9b0d0e71dc3c8bae8 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Fri, 23 Dec 2016 15:35:56 -0400 Subject: [PATCH] Refactor the document transformation clear view. --- mayan/apps/documents/urls.py | 20 ++-- mayan/apps/documents/views/document_views.py | 114 +++++++------------ 2 files changed, 53 insertions(+), 81 deletions(-) diff --git a/mayan/apps/documents/urls.py b/mayan/apps/documents/urls.py index 47122ba3a0..38fff707d6 100644 --- a/mayan/apps/documents/urls.py +++ b/mayan/apps/documents/urls.py @@ -21,16 +21,15 @@ from .views import ( DocumentPageRotateRightView, DocumentPageView, DocumentPageViewResetView, DocumentPageZoomInView, DocumentPageZoomOutView, DocumentPreviewView, DocumentPrint, DocumentRestoreView, DocumentRestoreManyView, - DocumentTrashView, DocumentTrashManyView, DocumentTypeCreateView, - DocumentTypeDeleteView, DocumentTypeDocumentListView, - DocumentTypeFilenameCreateView, DocumentTypeFilenameDeleteView, - DocumentTypeFilenameEditView, DocumentTypeFilenameListView, - DocumentTypeListView, DocumentTypeEditView, + DocumentTransformationsClearView, DocumentTrashView, DocumentTrashManyView, + DocumentTypeCreateView, DocumentTypeDeleteView, + DocumentTypeDocumentListView, DocumentTypeFilenameCreateView, + DocumentTypeFilenameDeleteView, DocumentTypeFilenameEditView, + DocumentTypeFilenameListView, DocumentTypeListView, DocumentTypeEditView, DocumentUpdatePageCountView, DocumentVersionDownloadFormView, DocumentVersionDownloadView, DocumentVersionListView, DocumentVersionRevertView, DocumentView, EmptyTrashCanView, - RecentDocumentListView, document_clear_transformations, - document_multiple_clear_transformations, document_page_navigation_first, + RecentDocumentListView, document_page_navigation_first, document_page_navigation_last, document_page_navigation_next, document_page_navigation_previous ) @@ -122,8 +121,9 @@ urlpatterns = [ name='document_multiple_download' ), url( - r'^(?P\d+)/clear_transformations/$', - document_clear_transformations, name='document_clear_transformations' + r'^(?P\d+)/clear_transformations/$', + DocumentTransformationsClearView.as_view(), + name='document_clear_transformations' ), url( @@ -151,7 +151,7 @@ urlpatterns = [ url( r'^multiple/clear_transformations/$', - document_multiple_clear_transformations, + DocumentTransformationsClearView.as_view(), name='document_multiple_clear_transformations' ), url( diff --git a/mayan/apps/documents/views/document_views.py b/mayan/apps/documents/views/document_views.py index 38cedef3d3..9799383c20 100644 --- a/mayan/apps/documents/views/document_views.py +++ b/mayan/apps/documents/views/document_views.py @@ -2,13 +2,11 @@ from __future__ import absolute_import, unicode_literals import logging -from django.conf import settings from django.contrib import messages from django.core.exceptions import PermissionDenied from django.core.urlresolvers import reverse, reverse_lazy from django.http import HttpResponseRedirect -from django.shortcuts import render_to_response, get_object_or_404 -from django.template import RequestContext +from django.shortcuts import get_object_or_404 from django.utils.http import urlencode from django.utils.translation import ugettext_lazy as _, ungettext @@ -550,79 +548,53 @@ class DocumentUpdatePageCountView(MultipleObjectConfirmActionView): ) -def document_clear_transformations(request, document_id=None, document_id_list=None): - if document_id: - documents = Document.objects.filter(pk=document_id) - post_redirect = documents[0].get_absolute_url() - elif document_id_list: - documents = Document.objects.filter(pk__in=document_id_list) - post_redirect = None +class DocumentTransformationsClearView(MultipleObjectConfirmActionView): + model = Document + object_permission = permission_transformation_delete + success_message = _( + 'Transformation clear request processed for %(count)d document' + ) + success_message_plural = _( + 'Transformation clear request processed for %(count)d documents' + ) - if not documents: - messages.error(request, _('Must provide at least one document.')) - return HttpResponseRedirect( - request.META.get( - 'HTTP_REFERER', reverse(settings.LOGIN_REDIRECT_URL) + def get_extra_context(self): + queryset = self.get_queryset() + + result = { + 'title': ungettext( + 'Clear all the page transformations for the selected document?', + 'Clear all the page transformations for the selected document?', + queryset.count() ) - ) + } - documents = AccessControlList.objects.filter_by_access( - permission_transformation_delete, request.user, queryset=documents - ) + if queryset.count() == 1: + result.update( + { + 'object': queryset.first(), + 'title': _( + 'Clear all the page transformations for the ' + 'document: %s?' + ) % queryset.first() + } + ) - previous = request.POST.get('previous', request.GET.get('previous', request.META.get('HTTP_REFERER', post_redirect or reverse('documents:document_list')))) - next = request.POST.get('next', request.GET.get('next', request.META.get('HTTP_REFERER', post_redirect or reverse('documents:document_list')))) + return result - if request.method == 'POST': - for document in documents: - try: - for page in document.pages.all(): - Transformation.objects.get_for_model(page).delete() - except Exception as exception: - messages.error( - request, _( - 'Error deleting the page transformations for ' - 'document: %(document)s; %(error)s.' - ) % { - 'document': document, 'error': exception - } - ) - else: - messages.success( - request, _( - 'All the page transformations for document: %s, ' - 'have been deleted successfully.' - ) % document - ) - - return HttpResponseRedirect(next) - - context = { - 'delete_view': True, - 'next': next, - 'previous': previous, - 'title': ungettext( - 'Clear all the page transformations for the selected document?', - 'Clear all the page transformations for the selected documents?', - documents.count() - ) - } - - if documents.count() == 1: - context['object'] = documents.first() - - return render_to_response( - 'appearance/generic_confirm.html', context, - context_instance=RequestContext(request) - ) - - -def document_multiple_clear_transformations(request): - return document_clear_transformations( - request, document_id_list=request.GET.get( - 'id_list', request.POST.get('id_list', '') - ).split(',') - ) + def object_action(self, form, instance): + try: + for page in instance.pages.all(): + Transformation.objects.get_for_model(page).delete() + except Exception as exception: + messages.error( + self.request, _( + 'Error deleting the page transformations for ' + 'document: %(document)s; %(error)s.' + ) % { + 'document': instance, 'error': exception + } + ) class DocumentPrint(FormView):