Merge branch 'feature/send_email' into development
This commit is contained in:
19
apps/mailer/__init__.py
Normal file
19
apps/mailer/__init__.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from navigation.api import register_links
|
||||
from documents.models import Document
|
||||
from acls.api import class_permissions
|
||||
|
||||
from .permissions import PERMISSION_MAILING_LINK, PERMISSION_MAILING_SEND_DOCUMENT
|
||||
|
||||
send_document_link = {'text': _(u'email link'), 'view': 'send_document_link', 'args': 'object.pk', 'famfam': 'email_link', 'permissions': [PERMISSION_MAILING_LINK]}
|
||||
send_document = {'text': _(u'email document'), 'view': 'send_document', 'args': 'object.pk', 'famfam': 'email_open', 'permissions': [PERMISSION_MAILING_SEND_DOCUMENT]}
|
||||
|
||||
register_links(Document, [send_document_link, send_document])
|
||||
|
||||
class_permissions(Document, [
|
||||
PERMISSION_MAILING_LINK,
|
||||
PERMISSION_MAILING_SEND_DOCUMENT
|
||||
])
|
||||
0
apps/mailer/conf/__init__.py
Normal file
0
apps/mailer/conf/__init__.py
Normal file
18
apps/mailer/conf/settings.py
Normal file
18
apps/mailer/conf/settings.py
Normal file
@@ -0,0 +1,18 @@
|
||||
"""Configuration options for the mailer app"""
|
||||
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from smart_settings.api import register_settings
|
||||
|
||||
register_settings(
|
||||
namespace=u'mailer',
|
||||
module=u'mailer.conf.settings',
|
||||
settings=[
|
||||
# Links
|
||||
{'name': u'LINK_SUBJECT_TEMPLATE', 'global_name': u'MAILER_LINK_SUBJECT_TEMPLATE', 'default': 'Link for document: {{ document }}', 'description': _(u'Template for the document link email form subject line.')},
|
||||
{'name': u'LINK_BODY_TEMPLATE', 'global_name': u'MAILER_LINK_BODY_TEMPLATE', 'default': 'To access this document click on the following link: <a href="{{ link }}">{{ link }}</a><br /><br />\n\n--------<br />\nThis email has been sent from Mayan EDMS (http://www.mayan-edms.com)', 'description': _(u'Template for the document link email form body line.')},
|
||||
# Attachment
|
||||
{'name': u'DOCUMENT_SUBJECT_TEMPLATE', 'global_name': u'MAILER_DOCUMENT_SUBJECT_TEMPLATE', 'default': 'Document: {{ document }}', 'description': _(u'Template for the document email form subject line.')},
|
||||
{'name': u'DOCUMENT_BODY_TEMPLATE', 'global_name': u'MAILER_DOCUMENT_BODY_TEMPLATE', 'default': 'Attached to this email is the document: {{ document }}<br /><br />\n\n--------<br />\nThis email has been sent from Mayan EDMS (http://www.mayan-edms.com)', 'description': _(u'Template for the document email form body line.')},
|
||||
]
|
||||
)
|
||||
23
apps/mailer/forms.py
Normal file
23
apps/mailer/forms.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
from django import forms
|
||||
from django.utils.translation import ugettext as _
|
||||
|
||||
from .conf import settings
|
||||
|
||||
|
||||
class DocumentMailForm(forms.Form):
|
||||
def __init__(self, *args, **kwargs):
|
||||
as_attachment = kwargs.pop('as_attachment', False)
|
||||
super(DocumentMailForm, self).__init__(*args, **kwargs)
|
||||
if as_attachment:
|
||||
self.fields['subject'].initial = settings.DOCUMENT_SUBJECT_TEMPLATE
|
||||
self.fields['body'].initial = settings.DOCUMENT_BODY_TEMPLATE
|
||||
else:
|
||||
self.fields['subject'].initial = settings.LINK_SUBJECT_TEMPLATE
|
||||
self.fields['body'].initial = settings.LINK_BODY_TEMPLATE
|
||||
|
||||
|
||||
email = forms.EmailField(label=_(u'Email address'))
|
||||
subject = forms.CharField(label=_(u'Subject'), required=False)#, initial=_(u'Link for document: {{ document }}'))
|
||||
body = forms.CharField(label=_(u'Body'), widget=forms.widgets.Textarea(), required=False)#, initial=_(u'To access this document click on the following link: <a href="{{ link }}">{{ link }}</a>'))
|
||||
3
apps/mailer/models.py
Normal file
3
apps/mailer/models.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
10
apps/mailer/permissions.py
Normal file
10
apps/mailer/permissions.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from permissions.models import PermissionNamespace, Permission
|
||||
|
||||
mailer_namespace = PermissionNamespace('mailing', _(u'Mailing'))
|
||||
|
||||
PERMISSION_MAILING_LINK = Permission.objects.register(mailer_namespace, 'mail_link', _(u'Send document link via email'))
|
||||
PERMISSION_MAILING_SEND_DOCUMENT = Permission.objects.register(mailer_namespace, 'mail_document', _(u'Send document via email'))
|
||||
6
apps/mailer/urls.py
Normal file
6
apps/mailer/urls.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.conf.urls.defaults import patterns, url
|
||||
|
||||
urlpatterns = patterns('mailer.views',
|
||||
url(r'^(?P<document_id>\d+)/send/link/$', 'send_document_link', (), 'send_document_link'),
|
||||
url(r'^(?P<document_id>\d+)/send/document/$', 'send_document_link', {'as_attachment': True}, 'send_document'),
|
||||
)
|
||||
111
apps/mailer/views.py
Normal file
111
apps/mailer/views.py
Normal file
@@ -0,0 +1,111 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
from django.shortcuts import render_to_response
|
||||
from django.template import RequestContext
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.contrib import messages
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.core.mail import EmailMultiAlternatives
|
||||
from django.utils.html import strip_tags
|
||||
from django.template import Context, Template
|
||||
from django.contrib.sites.models import Site
|
||||
|
||||
from documents.models import Document, RecentDocument
|
||||
from permissions.models import Permission
|
||||
from acls.models import AccessEntry
|
||||
|
||||
from .permissions import PERMISSION_MAILING_LINK, PERMISSION_MAILING_SEND_DOCUMENT
|
||||
from .forms import DocumentMailForm
|
||||
|
||||
|
||||
def send_document_link(request, document_id=None, document_id_list=None, as_attachment=False):
|
||||
if document_id:
|
||||
documents = [get_object_or_404(Document, pk=document_id)]
|
||||
elif document_id_list:
|
||||
documents = [get_object_or_404(Document, pk=document_id) for document_id in document_id_list.split(',')]
|
||||
|
||||
if as_attachment:
|
||||
permission = PERMISSION_MAILING_SEND_DOCUMENT
|
||||
else:
|
||||
permission = PERMISSION_MAILING_LINK
|
||||
|
||||
try:
|
||||
Permission.objects.check_permissions(request.user, [permission])
|
||||
except PermissionDenied:
|
||||
documents = AccessEntry.objects.filter_objects_by_access(permission, request.user, documents)
|
||||
|
||||
if not documents:
|
||||
messages.error(request, _(u'Must provide at least one document.'))
|
||||
return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
|
||||
|
||||
post_action_redirect = reverse('document_list_recent')
|
||||
|
||||
next = request.POST.get('next', request.GET.get('next', request.META.get('HTTP_REFERER', post_action_redirect)))
|
||||
|
||||
for document in documents:
|
||||
RecentDocument.objects.add_document_for_user(request.user, document)
|
||||
|
||||
if request.method == 'POST':
|
||||
form = DocumentMailForm(request.POST, as_attachment=as_attachment)
|
||||
if form.is_valid():
|
||||
context = Context({
|
||||
'link': 'http://%s%s' % (Site.objects.get_current().domain, reverse('document_view_simple', args=[document.pk])),
|
||||
'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))
|
||||
|
||||
email_msg = EmailMultiAlternatives(subject_text, body_text_content, request.user.email, [form.cleaned_data['email']])
|
||||
email_msg.attach_alternative(body_html_content, 'text/html')
|
||||
if as_attachment:
|
||||
for document in documents:
|
||||
descriptor = document.open()
|
||||
email_msg.attach(document.filename, descriptor.read(), document.file_mimetype)
|
||||
descriptor.close()
|
||||
|
||||
try:
|
||||
email_msg.send()
|
||||
except Exception, exc:
|
||||
if as_attachment:
|
||||
messages.error(request, _(u'Error sending document: %(document)s, via email; %(error)s.') % {
|
||||
'document': document, 'error': exc})
|
||||
else:
|
||||
messages.error(request, _(u'Error sending document link for document %(document)s; %(error)s.') % {
|
||||
'document': document, 'error': exc})
|
||||
|
||||
else:
|
||||
if as_attachment:
|
||||
messages.success(request, _(u'Successfully sent document via email.'))
|
||||
else:
|
||||
messages.success(request, _(u'Successfully sent document link via email.'))
|
||||
return HttpResponseRedirect(next)
|
||||
else:
|
||||
form = DocumentMailForm(as_attachment=as_attachment)
|
||||
|
||||
context = {
|
||||
'form': form,
|
||||
'next': next,
|
||||
'submit_label': _(u'Send'),
|
||||
'submit_icon_famfam': 'email_go'
|
||||
}
|
||||
if len(documents) == 1:
|
||||
context['object'] = documents[0]
|
||||
if as_attachment:
|
||||
context['title'] = _(u'Email document: %s') % ', '.join([unicode(d) for d in documents])
|
||||
else:
|
||||
context['title'] = _(u'Email link for document: %s') % ', '.join([unicode(d) for d in documents])
|
||||
elif len(documents) > 1:
|
||||
if as_attachment:
|
||||
context['title'] = _(u'Email documents: %s') % ', '.join([unicode(d) for d in documents])
|
||||
else:
|
||||
context['title'] = _(u'Email links for documents: %s') % ', '.join([unicode(d) for d in documents])
|
||||
|
||||
return render_to_response('generic_form.html', context,
|
||||
context_instance=RequestContext(request))
|
||||
@@ -165,6 +165,7 @@ INSTALLED_APPS = (
|
||||
'document_comments',
|
||||
'metadata',
|
||||
'documents',
|
||||
'mailer',
|
||||
'linking',
|
||||
'document_indexing',
|
||||
'document_acls',
|
||||
|
||||
Reference in New Issue
Block a user