diff --git a/docs/releases/2.2.rst b/docs/releases/2.2.rst index 536b8eac2e..40d14637a6 100644 --- a/docs/releases/2.2.rst +++ b/docs/releases/2.2.rst @@ -33,6 +33,7 @@ the user links on production install to debug errors live. - Refactor add document to folder view to allow adding a documents to multiple folders at the same time. - Refactor the remove document from folder view to allow removing documents from multiple folders at the same time. +- Refactor the document mailing views and add support for sending multiple documents via email at the same time. Removals -------- diff --git a/mayan/apps/mailer/apps.py b/mayan/apps/mailer/apps.py index 27c2f23a54..a333eb3d23 100644 --- a/mayan/apps/mailer/apps.py +++ b/mayan/apps/mailer/apps.py @@ -6,13 +6,14 @@ from django.apps import apps from django.utils.translation import ugettext_lazy as _ from acls import ModelPermission -from common import MayanAppConfig, menu_object, menu_tools +from common import MayanAppConfig, menu_object, menu_multi_item, menu_tools from mayan.celery import app from navigation import SourceColumn from .links import ( link_document_mailing_error_log, link_send_document_link, - link_send_document + link_send_document, link_send_multiple_document, + link_send_multiple_document_link ) from .permissions import ( permission_mailing_link, permission_mailing_send_document @@ -59,6 +60,12 @@ class MailerApp(MayanAppConfig): } ) + menu_multi_item.bind_links( + links=( + link_send_multiple_document, link_send_multiple_document_link + ), sources=(Document,) + ) + menu_object.bind_links( links=( link_send_document_link, link_send_document diff --git a/mayan/apps/mailer/forms.py b/mayan/apps/mailer/forms.py index cb1dfcb422..169ff90971 100644 --- a/mayan/apps/mailer/forms.py +++ b/mayan/apps/mailer/forms.py @@ -15,13 +15,20 @@ class DocumentMailForm(forms.Form): as_attachment = kwargs.pop('as_attachment', False) super(DocumentMailForm, self).__init__(*args, **kwargs) if as_attachment: - self.fields['subject'].initial = setting_document_subject_template.value - self.fields['body'].initial = setting_document_body_template.value % { + self.fields[ + 'subject' + ].initial = setting_document_subject_template.value + + self.fields[ + 'body' + ].initial = setting_document_body_template.value % { 'project_title': settings.PROJECT_TITLE, 'project_website': settings.PROJECT_WEBSITE } else: - self.fields['subject'].initial = setting_link_subject_template.value + self.fields[ + 'subject' + ].initial = setting_link_subject_template.value self.fields['body'].initial = setting_link_body_template.value % { 'project_title': settings.PROJECT_TITLE, 'project_website': settings.PROJECT_WEBSITE diff --git a/mayan/apps/mailer/links.py b/mayan/apps/mailer/links.py index 2909f83a10..583bd9e544 100644 --- a/mayan/apps/mailer/links.py +++ b/mayan/apps/mailer/links.py @@ -17,6 +17,12 @@ link_send_document_link = Link( args='resolved_object.pk', permissions=(permission_mailing_link,), text=_('Email link'), view='mailer:send_document_link' ) +link_send_multiple_document = Link( + text=_('Email document'), view='mailer:send_multiple_document' +) +link_send_multiple_document_link = Link( + text=_('Email link'), view='mailer:send_multiple_document_link' +) link_document_mailing_error_log = Link( icon='fa fa-envelope', permissions=(permission_view_error_log,), text=_('Document mailing error log'), view='mailer:error_log', diff --git a/mayan/apps/mailer/urls.py b/mayan/apps/mailer/urls.py index cbd1d2146e..462e63e880 100644 --- a/mayan/apps/mailer/urls.py +++ b/mayan/apps/mailer/urls.py @@ -2,16 +2,24 @@ from __future__ import unicode_literals from django.conf.urls import url -from .views import LogEntryListView, send_document_link +from .views import LogEntryListView, MailDocumentLinkView, MailDocumentView urlpatterns = [ url( - r'^(?P\d+)/send/link/$', send_document_link, + r'^(?P\d+)/send/link/$', MailDocumentLinkView.as_view(), name='send_document_link' ), url( - r'^(?P\d+)/send/document/$', send_document_link, - {'as_attachment': True}, name='send_document' + r'^multiple/send/link/$', MailDocumentLinkView.as_view(), + name='send_multiple_document_link' + ), + url( + r'^(?P\d+)/send/document/$', MailDocumentView.as_view(), + name='send_document' + ), + url( + r'^multiple/send/document/$', MailDocumentView.as_view(), + name='send_multiple_document' ), url( r'^log/$', LogEntryListView.as_view(), name='error_log' diff --git a/mayan/apps/mailer/views.py b/mayan/apps/mailer/views.py index e26002d730..3de1fdcf18 100644 --- a/mayan/apps/mailer/views.py +++ b/mayan/apps/mailer/views.py @@ -1,17 +1,11 @@ from __future__ import absolute_import, unicode_literals -from django.conf import settings -from django.contrib import messages from django.contrib.sites.models import Site -from django.core.urlresolvers import reverse -from django.http import HttpResponseRedirect -from django.shortcuts import render_to_response -from django.template import Context, RequestContext, Template +from django.template import Context, Template from django.utils.html import strip_tags -from django.utils.translation import ugettext_lazy as _ +from django.utils.translation import ungettext, ugettext_lazy as _ -from acls.models import AccessControlList -from common.generics import SingleObjectListView +from common.generics import MultipleObjectFormActionView, SingleObjectListView from documents.models import Document from .forms import DocumentMailForm @@ -32,96 +26,81 @@ class LogEntryListView(SingleObjectListView): view_permission = permission_view_error_log -def send_document_link(request, document_id=None, document_id_list=None, as_attachment=False): - if document_id: - documents = Document.objects.filter(pk=document_id) - elif document_id_list: - documents = Document.objects.filter(pk__in=document_id_list) +class MailDocumentView(MultipleObjectFormActionView): + as_attachment = True + form_class = DocumentMailForm + model = Document + object_permission = permission_mailing_send_document - if as_attachment: - permission = permission_mailing_send_document - else: - permission = permission_mailing_link - - documents = AccessControlList.objects.filter_by_access( - permission, request.user, queryset=documents + success_message = _('%(count)d document queued for email delivery') + success_message_plural = _( + '%(count)d documents queued for email delivery' ) + title = 'Email document' + title_plural = 'Email documents' + title_document = 'Email document: %s' - 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 = { + 'submit_icon': 'fa fa-envelope', + 'submit_label': _('Send'), + 'title': ungettext( + self.title, + self.title_plural, + queryset.count() ) + } + + if queryset.count() == 1: + result.update( + { + 'object': queryset.first(), + 'title': _(self.title_document) % queryset.first() + } + ) + + return result + + def get_form_extra_kwargs(self): + return { + 'as_attachment': self.as_attachment + } + + def object_action(self, form, instance): + context = Context({ + 'link': 'http://%s%s' % ( + Site.objects.get_current().domain, + instance.get_absolute_url() + ), + 'document': instance + }) + body_template = Template(form.cleaned_data['body']) + body_html_content = body_template.render(context) + body_text_content = strip_tags(body_html_content) + + subject_template = Template(form.cleaned_data['subject']) + subject_text = strip_tags(subject_template.render(context)) + + task_send_document.apply_async( + args=( + subject_text, body_text_content, self.request.user.email, + form.cleaned_data['email'] + ), kwargs={ + 'document_id': instance.pk, + 'as_attachment': self.as_attachment + } ) - post_action_redirect = reverse('documents:document_list_recent') - next = request.POST.get( - 'next', request.GET.get( - 'next', request.META.get('HTTP_REFERER', post_action_redirect) - ) - ) - - for document in documents: - document.add_as_recent_document_for_user(request.user) - - if request.method == 'POST': - form = DocumentMailForm(request.POST, as_attachment=as_attachment) - if form.is_valid(): - - for document in documents: - context = Context({ - 'link': 'http://%s%s' % ( - Site.objects.get_current().domain, - document.get_absolute_url() - ), - 'document': document - }) - body_template = Template(form.cleaned_data['body']) - body_html_content = body_template.render(context) - body_text_content = strip_tags(body_html_content) - - subject_template = Template(form.cleaned_data['subject']) - subject_text = strip_tags(subject_template.render(context)) - - task_send_document.apply_async( - args=( - subject_text, body_text_content, request.user.email, - form.cleaned_data['email'] - ), kwargs={ - 'document_id': document.pk, - 'as_attachment': as_attachment - } - ) - - # TODO: Pluralize - messages.success( - request, _('Successfully queued for delivery via email.') - ) - return HttpResponseRedirect(next) - else: - form = DocumentMailForm(as_attachment=as_attachment) - - context = { - 'form': form, - 'next': next, - 'submit_label': _('Send'), - 'submit_icon': 'fa fa-envelope' - } - if documents.count() == 1: - context['object'] = documents.first() - if as_attachment: - context['title'] = _('Email document: %s') % ', '.join([unicode(d) for d in documents]) - else: - context['title'] = _('Email link for document: %s') % ', '.join([unicode(d) for d in documents]) - elif documents.count() > 1: - if as_attachment: - context['title'] = _('Email documents: %s') % ', '.join([unicode(d) for d in documents]) - else: - context['title'] = _('Email links for documents: %s') % ', '.join([unicode(d) for d in documents]) - - return render_to_response( - 'appearance/generic_form.html', context, - context_instance=RequestContext(request) +class MailDocumentLinkView(MailDocumentView): + as_attachment = False + object_permission = permission_mailing_link + success_message = _('%(count)d document link queued for email delivery') + success_message_plural = _( + '%(count)d document links queued for email delivery' ) + title = 'Email document link' + title_plural = 'Email document links' + title_document = 'Email link for document: %s'