Refactor the document mailing views.
Add support for sending multiple documents via email at the same time.
This commit is contained in:
@@ -33,6 +33,7 @@ the user links
|
|||||||
on production install to debug errors live.
|
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 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 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
|
Removals
|
||||||
--------
|
--------
|
||||||
|
|||||||
@@ -6,13 +6,14 @@ from django.apps import apps
|
|||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
from acls import ModelPermission
|
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 mayan.celery import app
|
||||||
from navigation import SourceColumn
|
from navigation import SourceColumn
|
||||||
|
|
||||||
from .links import (
|
from .links import (
|
||||||
link_document_mailing_error_log, link_send_document_link,
|
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 (
|
from .permissions import (
|
||||||
permission_mailing_link, permission_mailing_send_document
|
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(
|
menu_object.bind_links(
|
||||||
links=(
|
links=(
|
||||||
link_send_document_link, link_send_document
|
link_send_document_link, link_send_document
|
||||||
|
|||||||
@@ -15,13 +15,20 @@ class DocumentMailForm(forms.Form):
|
|||||||
as_attachment = kwargs.pop('as_attachment', False)
|
as_attachment = kwargs.pop('as_attachment', False)
|
||||||
super(DocumentMailForm, self).__init__(*args, **kwargs)
|
super(DocumentMailForm, self).__init__(*args, **kwargs)
|
||||||
if as_attachment:
|
if as_attachment:
|
||||||
self.fields['subject'].initial = setting_document_subject_template.value
|
self.fields[
|
||||||
self.fields['body'].initial = setting_document_body_template.value % {
|
'subject'
|
||||||
|
].initial = setting_document_subject_template.value
|
||||||
|
|
||||||
|
self.fields[
|
||||||
|
'body'
|
||||||
|
].initial = setting_document_body_template.value % {
|
||||||
'project_title': settings.PROJECT_TITLE,
|
'project_title': settings.PROJECT_TITLE,
|
||||||
'project_website': settings.PROJECT_WEBSITE
|
'project_website': settings.PROJECT_WEBSITE
|
||||||
}
|
}
|
||||||
else:
|
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 % {
|
self.fields['body'].initial = setting_link_body_template.value % {
|
||||||
'project_title': settings.PROJECT_TITLE,
|
'project_title': settings.PROJECT_TITLE,
|
||||||
'project_website': settings.PROJECT_WEBSITE
|
'project_website': settings.PROJECT_WEBSITE
|
||||||
|
|||||||
@@ -17,6 +17,12 @@ link_send_document_link = Link(
|
|||||||
args='resolved_object.pk', permissions=(permission_mailing_link,),
|
args='resolved_object.pk', permissions=(permission_mailing_link,),
|
||||||
text=_('Email link'), view='mailer:send_document_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(
|
link_document_mailing_error_log = Link(
|
||||||
icon='fa fa-envelope', permissions=(permission_view_error_log,),
|
icon='fa fa-envelope', permissions=(permission_view_error_log,),
|
||||||
text=_('Document mailing error log'), view='mailer:error_log',
|
text=_('Document mailing error log'), view='mailer:error_log',
|
||||||
|
|||||||
@@ -2,16 +2,24 @@ from __future__ import unicode_literals
|
|||||||
|
|
||||||
from django.conf.urls import url
|
from django.conf.urls import url
|
||||||
|
|
||||||
from .views import LogEntryListView, send_document_link
|
from .views import LogEntryListView, MailDocumentLinkView, MailDocumentView
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(
|
url(
|
||||||
r'^(?P<document_id>\d+)/send/link/$', send_document_link,
|
r'^(?P<pk>\d+)/send/link/$', MailDocumentLinkView.as_view(),
|
||||||
name='send_document_link'
|
name='send_document_link'
|
||||||
),
|
),
|
||||||
url(
|
url(
|
||||||
r'^(?P<document_id>\d+)/send/document/$', send_document_link,
|
r'^multiple/send/link/$', MailDocumentLinkView.as_view(),
|
||||||
{'as_attachment': True}, name='send_document'
|
name='send_multiple_document_link'
|
||||||
|
),
|
||||||
|
url(
|
||||||
|
r'^(?P<pk>\d+)/send/document/$', MailDocumentView.as_view(),
|
||||||
|
name='send_document'
|
||||||
|
),
|
||||||
|
url(
|
||||||
|
r'^multiple/send/document/$', MailDocumentView.as_view(),
|
||||||
|
name='send_multiple_document'
|
||||||
),
|
),
|
||||||
url(
|
url(
|
||||||
r'^log/$', LogEntryListView.as_view(), name='error_log'
|
r'^log/$', LogEntryListView.as_view(), name='error_log'
|
||||||
|
|||||||
@@ -1,17 +1,11 @@
|
|||||||
from __future__ import absolute_import, unicode_literals
|
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.contrib.sites.models import Site
|
||||||
from django.core.urlresolvers import reverse
|
from django.template import Context, Template
|
||||||
from django.http import HttpResponseRedirect
|
|
||||||
from django.shortcuts import render_to_response
|
|
||||||
from django.template import Context, RequestContext, Template
|
|
||||||
from django.utils.html import strip_tags
|
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 MultipleObjectFormActionView, SingleObjectListView
|
||||||
from common.generics import SingleObjectListView
|
|
||||||
from documents.models import Document
|
from documents.models import Document
|
||||||
|
|
||||||
from .forms import DocumentMailForm
|
from .forms import DocumentMailForm
|
||||||
@@ -32,51 +26,55 @@ class LogEntryListView(SingleObjectListView):
|
|||||||
view_permission = permission_view_error_log
|
view_permission = permission_view_error_log
|
||||||
|
|
||||||
|
|
||||||
def send_document_link(request, document_id=None, document_id_list=None, as_attachment=False):
|
class MailDocumentView(MultipleObjectFormActionView):
|
||||||
if document_id:
|
as_attachment = True
|
||||||
documents = Document.objects.filter(pk=document_id)
|
form_class = DocumentMailForm
|
||||||
elif document_id_list:
|
model = Document
|
||||||
documents = Document.objects.filter(pk__in=document_id_list)
|
object_permission = permission_mailing_send_document
|
||||||
|
|
||||||
if as_attachment:
|
success_message = _('%(count)d document queued for email delivery')
|
||||||
permission = permission_mailing_send_document
|
success_message_plural = _(
|
||||||
else:
|
'%(count)d documents queued for email delivery'
|
||||||
permission = permission_mailing_link
|
)
|
||||||
|
title = 'Email document'
|
||||||
|
title_plural = 'Email documents'
|
||||||
|
title_document = 'Email document: %s'
|
||||||
|
|
||||||
documents = AccessControlList.objects.filter_by_access(
|
def get_extra_context(self):
|
||||||
permission, request.user, queryset=documents
|
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()
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
if not documents:
|
return result
|
||||||
messages.error(request, _('Must provide at least one document.'))
|
|
||||||
return HttpResponseRedirect(
|
|
||||||
request.META.get(
|
|
||||||
'HTTP_REFERER', reverse(settings.LOGIN_REDIRECT_URL)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
post_action_redirect = reverse('documents:document_list_recent')
|
def get_form_extra_kwargs(self):
|
||||||
|
return {
|
||||||
|
'as_attachment': self.as_attachment
|
||||||
|
}
|
||||||
|
|
||||||
next = request.POST.get(
|
def object_action(self, form, instance):
|
||||||
'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({
|
context = Context({
|
||||||
'link': 'http://%s%s' % (
|
'link': 'http://%s%s' % (
|
||||||
Site.objects.get_current().domain,
|
Site.objects.get_current().domain,
|
||||||
document.get_absolute_url()
|
instance.get_absolute_url()
|
||||||
),
|
),
|
||||||
'document': document
|
'document': instance
|
||||||
})
|
})
|
||||||
body_template = Template(form.cleaned_data['body'])
|
body_template = Template(form.cleaned_data['body'])
|
||||||
body_html_content = body_template.render(context)
|
body_html_content = body_template.render(context)
|
||||||
@@ -87,41 +85,22 @@ def send_document_link(request, document_id=None, document_id_list=None, as_atta
|
|||||||
|
|
||||||
task_send_document.apply_async(
|
task_send_document.apply_async(
|
||||||
args=(
|
args=(
|
||||||
subject_text, body_text_content, request.user.email,
|
subject_text, body_text_content, self.request.user.email,
|
||||||
form.cleaned_data['email']
|
form.cleaned_data['email']
|
||||||
), kwargs={
|
), kwargs={
|
||||||
'document_id': document.pk,
|
'document_id': instance.pk,
|
||||||
'as_attachment': as_attachment
|
'as_attachment': self.as_attachment
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
# TODO: Pluralize
|
|
||||||
messages.success(
|
|
||||||
request, _('Successfully queued for delivery via email.')
|
|
||||||
)
|
|
||||||
return HttpResponseRedirect(next)
|
|
||||||
else:
|
|
||||||
form = DocumentMailForm(as_attachment=as_attachment)
|
|
||||||
|
|
||||||
context = {
|
class MailDocumentLinkView(MailDocumentView):
|
||||||
'form': form,
|
as_attachment = False
|
||||||
'next': next,
|
object_permission = permission_mailing_link
|
||||||
'submit_label': _('Send'),
|
success_message = _('%(count)d document link queued for email delivery')
|
||||||
'submit_icon': 'fa fa-envelope'
|
success_message_plural = _(
|
||||||
}
|
'%(count)d document links queued for email delivery'
|
||||||
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)
|
|
||||||
)
|
)
|
||||||
|
title = 'Email document link'
|
||||||
|
title_plural = 'Email document links'
|
||||||
|
title_document = 'Email link for document: %s'
|
||||||
|
|||||||
Reference in New Issue
Block a user