Add persistent error logging to the mailer app.
This commit is contained in:
14
mayan/apps/mailer/admin.py
Normal file
14
mayan/apps/mailer/admin.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.contrib import admin
|
||||
|
||||
from .models import LogEntry
|
||||
|
||||
|
||||
class LogEntryAdmin(admin.ModelAdmin):
|
||||
date_hierarchy = 'datetime'
|
||||
list_display = ('datetime', 'message')
|
||||
readonly_fields = ('datetime', 'message')
|
||||
|
||||
|
||||
admin.site.register(LogEntry, LogEntryAdmin)
|
||||
@@ -5,11 +5,16 @@ from kombu import Exchange, Queue
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from acls import ModelPermission
|
||||
from common import MayanAppConfig, menu_object
|
||||
from common import MayanAppConfig, menu_object, menu_tools
|
||||
from documents.models import Document
|
||||
from mayan.celery import app
|
||||
from navigation import SourceColumn
|
||||
|
||||
from .links import link_send_document_link, link_send_document
|
||||
from .links import (
|
||||
link_document_mailing_error_log, link_send_document_link,
|
||||
link_send_document
|
||||
)
|
||||
from .models import LogEntry
|
||||
from .permissions import (
|
||||
permission_mailing_link, permission_mailing_send_document
|
||||
)
|
||||
@@ -22,6 +27,18 @@ class MailerApp(MayanAppConfig):
|
||||
def ready(self):
|
||||
super(MailerApp, self).ready()
|
||||
|
||||
SourceColumn(
|
||||
source=LogEntry,
|
||||
label='Date and time',
|
||||
attribute='datetime'
|
||||
)
|
||||
|
||||
SourceColumn(
|
||||
source=LogEntry,
|
||||
label='Message',
|
||||
attribute='message'
|
||||
)
|
||||
|
||||
ModelPermission.register(
|
||||
model=Document, permissions=(
|
||||
permission_mailing_link, permission_mailing_send_document
|
||||
@@ -45,3 +62,5 @@ class MailerApp(MayanAppConfig):
|
||||
link_send_document_link, link_send_document
|
||||
), sources=(Document,)
|
||||
)
|
||||
|
||||
menu_tools.bind_links(links=(link_document_mailing_error_log,))
|
||||
|
||||
@@ -5,7 +5,8 @@ from django.utils.translation import ugettext_lazy as _
|
||||
from navigation import Link
|
||||
|
||||
from .permissions import (
|
||||
permission_mailing_link, permission_mailing_send_document
|
||||
permission_mailing_link, permission_mailing_send_document,
|
||||
permission_view_error_log
|
||||
)
|
||||
|
||||
link_send_document = Link(
|
||||
@@ -16,3 +17,7 @@ link_send_document_link = Link(
|
||||
permissions=(permission_mailing_link,), text=_('Email link'),
|
||||
view='mailer:send_document_link', args='object.pk'
|
||||
)
|
||||
link_document_mailing_error_log = Link(
|
||||
icon='fa fa-envelope', permissions=(permission_view_error_log,),
|
||||
text=_('Document mailing error log'), view='mailer:error_log',
|
||||
)
|
||||
|
||||
@@ -5,18 +5,18 @@ from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
|
||||
DEFAULT_DOCUMENT_BODY_TEMPLATE = _(
|
||||
'Attached to this email is the document: {{ document }}<br /><br />\n\n\
|
||||
--------<br />\nThis email has been sent from \
|
||||
%(project_title)s (%(project_website)s)'
|
||||
'Attached to this email is the document: {{ document }}\n\n '
|
||||
'--------\n '
|
||||
'This email has been sent from %(project_title)s (%(project_website)s)'
|
||||
) % {
|
||||
'project_title': settings.PROJECT_TITLE,
|
||||
'project_website': settings.PROJECT_WEBSITE
|
||||
}
|
||||
|
||||
DEFAULT_LINK_BODY_TEMPLATE = _(
|
||||
'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 %(project_title)s (%(project_website)s)'
|
||||
'To access this document click on the following link: '
|
||||
'{{ link }}\n\n--------\n '
|
||||
'This email has been sent from %(project_title)s (%(project_website)s)'
|
||||
) % {
|
||||
'project_title': settings.PROJECT_TITLE,
|
||||
'project_website': settings.PROJECT_WEBSITE
|
||||
|
||||
28
mayan/apps/mailer/migrations/0001_initial.py
Normal file
28
mayan/apps/mailer/migrations/0001_initial.py
Normal file
@@ -0,0 +1,28 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models, migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='LogEntry',
|
||||
fields=[
|
||||
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
|
||||
('datetime', models.DateTimeField(auto_now_add=True, verbose_name='Date time')),
|
||||
('message', models.TextField(verbose_name='Message', editable=False, blank=True)),
|
||||
],
|
||||
options={
|
||||
'ordering': ('-datetime',),
|
||||
'get_latest_by': 'datetime',
|
||||
'verbose_name': 'Log entry',
|
||||
'verbose_name_plural': 'Log entries',
|
||||
},
|
||||
bases=(models.Model,),
|
||||
),
|
||||
]
|
||||
0
mayan/apps/mailer/migrations/__init__.py
Normal file
0
mayan/apps/mailer/migrations/__init__.py
Normal file
27
mayan/apps/mailer/models.py
Normal file
27
mayan/apps/mailer/models.py
Normal file
@@ -0,0 +1,27 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import logging
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from djcelery.models import PeriodicTask, IntervalSchedule
|
||||
from documents.models import Document
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class LogEntry(models.Model):
|
||||
datetime = models.DateTimeField(
|
||||
auto_now_add=True, editable=False, verbose_name=_('Date time')
|
||||
)
|
||||
message = models.TextField(
|
||||
blank=True, editable=False, verbose_name=_('Message')
|
||||
)
|
||||
|
||||
class Meta:
|
||||
get_latest_by = 'datetime'
|
||||
ordering = ('-datetime',)
|
||||
verbose_name = _('Log entry')
|
||||
verbose_name_plural = _('Log entries')
|
||||
@@ -12,3 +12,6 @@ permission_mailing_link = namespace.add_permission(
|
||||
permission_mailing_send_document = namespace.add_permission(
|
||||
name='mail_document', label=_('Send document via email')
|
||||
)
|
||||
permission_view_error_log = namespace.add_permission(
|
||||
name='view_error_log', label=_('View document mailing error log')
|
||||
)
|
||||
|
||||
@@ -8,8 +8,8 @@ from .literals import (
|
||||
DEFAULT_DOCUMENT_BODY_TEMPLATE, DEFAULT_LINK_BODY_TEMPLATE
|
||||
)
|
||||
|
||||
|
||||
namespace = Namespace(name='mailer', label=_('Mailing'))
|
||||
|
||||
setting_link_subject_template = namespace.add_setting(
|
||||
default=_('Link for document: {{ document }}'),
|
||||
help_text=_('Template for the document link email form subject line.'),
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.core.mail import EmailMultiAlternatives
|
||||
|
||||
from documents.models import Document
|
||||
from mayan.celery import app
|
||||
|
||||
from .models import LogEntry
|
||||
|
||||
|
||||
@app.task(ignore_result=True)
|
||||
def task_send_document(subject_text, body_text_content, sender, recipient, document_id, as_attachment=False):
|
||||
@@ -17,4 +21,9 @@ def task_send_document(subject_text, body_text_content, sender, recipient, docum
|
||||
document.label, descriptor.read(), document.file_mimetype
|
||||
)
|
||||
|
||||
email_msg.send()
|
||||
try:
|
||||
email_msg.send()
|
||||
except Exception as exception:
|
||||
LogEntry.objects.create(message=exception)
|
||||
else:
|
||||
LogEntry.objects.all().delete()
|
||||
|
||||
@@ -2,6 +2,8 @@ from __future__ import unicode_literals
|
||||
|
||||
from django.conf.urls import patterns, url
|
||||
|
||||
from .views import LogEntryListView
|
||||
|
||||
urlpatterns = patterns(
|
||||
'mailer.views',
|
||||
url(
|
||||
@@ -10,6 +12,9 @@ urlpatterns = patterns(
|
||||
),
|
||||
url(
|
||||
r'^(?P<document_id>\d+)/send/document/$', 'send_document_link',
|
||||
{'as_attachment': True}, 'send_document'
|
||||
{'as_attachment': True}, name='send_document'
|
||||
),
|
||||
url(
|
||||
r'^log/$', LogEntryListView.as_view(), name='error_log'
|
||||
),
|
||||
)
|
||||
|
||||
@@ -12,16 +12,28 @@ from django.utils.html import strip_tags
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from acls.models import AccessControlList
|
||||
from common.generics import SingleObjectListView
|
||||
from documents.models import Document
|
||||
from permissions import Permission
|
||||
|
||||
from .forms import DocumentMailForm
|
||||
from .models import LogEntry
|
||||
from .permissions import (
|
||||
permission_mailing_link, permission_mailing_send_document
|
||||
permission_mailing_link, permission_mailing_send_document,
|
||||
permission_view_error_log
|
||||
)
|
||||
from .tasks import task_send_document
|
||||
|
||||
|
||||
class LogEntryListView(SingleObjectListView):
|
||||
extra_context = {
|
||||
'hide_object': True,
|
||||
'title': _('Document mailing error log'),
|
||||
}
|
||||
model = LogEntry
|
||||
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 = [get_object_or_404(Document, pk=document_id)]
|
||||
|
||||
Reference in New Issue
Block a user