From ba48a7e0fdcb08252e69fa2f70d11e3922e017a2 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Sun, 16 Dec 2018 04:15:31 -0400 Subject: [PATCH] Initial implementation of mailer events Signed-off-by: Roberto Rosario --- mayan/apps/mailer/apps.py | 46 ++++++++++++++++++++++----------- mayan/apps/mailer/events.py | 11 ++++++++ mayan/apps/mailer/models.py | 10 ++++--- mayan/apps/mailer/tasks.py | 10 +++++-- mayan/apps/mailer/views.py | 1 + mayan/apps/task_manager/apps.py | 2 +- 6 files changed, 59 insertions(+), 21 deletions(-) create mode 100644 mayan/apps/mailer/events.py diff --git a/mayan/apps/mailer/apps.py b/mayan/apps/mailer/apps.py index aaad974d2a..e9713521f4 100644 --- a/mayan/apps/mailer/apps.py +++ b/mayan/apps/mailer/apps.py @@ -15,10 +15,16 @@ from mayan.apps.common import ( menu_secondary, menu_setup, menu_tools ) from mayan.apps.common.widgets import TwoStateWidget +from mayan.apps.events import ModelEventType +from mayan.apps.events.links import ( + link_events_for_object, link_object_event_types_user_subcriptions_list +) +from mayan.apps.events.permissions import permission_events_view from mayan.apps.navigation import SourceColumn from mayan.celery import app from .classes import MailerBackend +from .events import event_email_sent from .links import ( link_send_document, link_send_document_link, link_send_multiple_document, link_send_multiple_document_link, link_system_mailer_error_log, @@ -43,6 +49,7 @@ class MailerApp(MayanAppConfig): def ready(self): super(MailerApp, self).ready() + from actstream import registry Document = apps.get_model( app_label='documents', model_name='Document' @@ -53,6 +60,25 @@ class MailerApp(MayanAppConfig): MailerBackend.initialize() + ModelEventType.register( + model=UserMailer, event_types=(event_email_sent,) + ) + + ModelPermission.register( + model=Document, permissions=( + permission_mailing_link, permission_mailing_send_document + ) + ) + + ModelPermission.register( + model=UserMailer, permissions=( + permission_acl_edit, permission_acl_view, + permission_events_view, permission_user_mailer_delete, + permission_user_mailer_edit, permission_user_mailer_view, + permission_user_mailer_use + ) + ) + SourceColumn( source=LogEntry, label=_('Date and time'), attribute='datetime' ) @@ -78,20 +104,6 @@ class MailerApp(MayanAppConfig): source=UserMailer, label=_('Label'), attribute='backend_label' ) - ModelPermission.register( - model=Document, permissions=( - permission_mailing_link, permission_mailing_send_document - ) - ) - - ModelPermission.register( - model=UserMailer, permissions=( - permission_acl_edit, permission_acl_view, - permission_user_mailer_delete, permission_user_mailer_edit, - permission_user_mailer_view, permission_user_mailer_use - ) - ) - app.conf.task_queues.append( Queue('mailing', Exchange('mailing'), routing_key='mailing'), ) @@ -106,7 +118,9 @@ class MailerApp(MayanAppConfig): menu_list_facet.bind_links( links=( - link_acl_list, link_user_mailer_log_list, + link_acl_list, link_events_for_object, + link_object_event_types_user_subcriptions_list, + link_user_mailer_log_list, ), sources=(UserMailer,) ) @@ -142,3 +156,5 @@ class MailerApp(MayanAppConfig): menu_tools.bind_links(links=(link_system_mailer_error_log,)) menu_setup.bind_links(links=(link_user_mailer_setup,)) + + registry.register(UserMailer) diff --git a/mayan/apps/mailer/events.py b/mayan/apps/mailer/events.py new file mode 100644 index 0000000000..881c2b214c --- /dev/null +++ b/mayan/apps/mailer/events.py @@ -0,0 +1,11 @@ +from __future__ import absolute_import, unicode_literals + +from django.utils.translation import ugettext_lazy as _ + +from mayan.apps.events import EventTypeNamespace + +namespace = EventTypeNamespace(name='mailing', label=_('Mailing')) + +event_email_sent = namespace.add_event_type( + name='email_send', label=_('Email sent') +) diff --git a/mayan/apps/mailer/models.py b/mayan/apps/mailer/models.py index 064b1e15b7..aea74ef813 100644 --- a/mayan/apps/mailer/models.py +++ b/mayan/apps/mailer/models.py @@ -12,6 +12,7 @@ from django.utils.html import strip_tags from django.utils.module_loading import import_string from django.utils.translation import ugettext_lazy as _ +from .events import event_email_sent from .managers import UserMailerManager from .utils import split_recipient_list @@ -126,7 +127,7 @@ class UserMailer(models.Model): return super(UserMailer, self).save(*args, **kwargs) - def send(self, to, subject='', body='', attachments=None): + def send(self, to, subject='', body='', attachments=None, _user=None): """ Send a simple email. There is no document or template knowledge. attachments is a list of dictionaries with the keys: @@ -157,8 +158,11 @@ class UserMailer(models.Model): self.error_log.create(message=exception) else: self.error_log.all().delete() + event_email_sent.commit( + actor=_user, target=self, #action_object=self.document_type + ) - def send_document(self, document, to, subject='', body='', as_attachment=False): + def send_document(self, document, to, subject='', body='', as_attachment=False, _user=None): """ Send a document using this user mailing profile. """ @@ -189,7 +193,7 @@ class UserMailer(models.Model): return self.send( attachments=attachments, body=body_html_content, - subject=subject_text, to=to, + subject=subject_text, to=to, user=user ) def test(self, to): diff --git a/mayan/apps/mailer/tasks.py b/mayan/apps/mailer/tasks.py index 58807f5305..e086cf5b8c 100644 --- a/mayan/apps/mailer/tasks.py +++ b/mayan/apps/mailer/tasks.py @@ -1,18 +1,20 @@ from __future__ import unicode_literals from django.apps import apps +from django.contrib.auth import get_user_model from mayan.celery import app @app.task(ignore_result=True) -def task_send_document(body, sender, subject, recipient, user_mailer_id, as_attachment=False, document_id=None): +def task_send_document(body, sender, subject, recipient, user_mailer_id, as_attachment=False, document_id=None, user_id=None): Document = apps.get_model( app_label='documents', model_name='Document' ) UserMailer = apps.get_model( app_label='mailer', model_name='UserMailer' ) + User = get_user_model() if document_id: document = Document.objects.get(pk=document_id) @@ -20,8 +22,12 @@ def task_send_document(body, sender, subject, recipient, user_mailer_id, as_atta document = None user_mailer = UserMailer.objects.get(pk=user_mailer_id) + if user_id: + user = User.objects.get(pk=user_id) + else: + user = None user_mailer.send_document( as_attachment=as_attachment, body=body, document=document, - subject=subject, to=recipient + subject=subject, to=recipient, _user=user ) diff --git a/mayan/apps/mailer/views.py b/mayan/apps/mailer/views.py index fc8cf4b63e..1a91ddf9aa 100644 --- a/mayan/apps/mailer/views.py +++ b/mayan/apps/mailer/views.py @@ -100,6 +100,7 @@ class MailDocumentView(MultipleObjectFormActionView): 'sender': self.request.user.email, 'subject': form.cleaned_data['subject'], 'user_mailer_id': form.cleaned_data['user_mailer'].pk, + 'user_id': self.request.user.pk, } ) diff --git a/mayan/apps/task_manager/apps.py b/mayan/apps/task_manager/apps.py index 8b92c1dc9b..af98b0c022 100644 --- a/mayan/apps/task_manager/apps.py +++ b/mayan/apps/task_manager/apps.py @@ -19,7 +19,7 @@ from .links import ( class TaskManagerApp(MayanAppConfig): app_namespace = 'task_manager' app_url = 'task_manager' - has_tests = True + has_tests = False name = 'mayan.apps.task_manager' verbose_name = _('Task manager')