diff --git a/mayan/apps/mailer/tests/test_actions.py b/mayan/apps/mailer/tests/test_actions.py index b53381e6e7..eed6d9f49a 100644 --- a/mayan/apps/mailer/tests/test_actions.py +++ b/mayan/apps/mailer/tests/test_actions.py @@ -9,6 +9,7 @@ from mayan.apps.documents.tests.mixins import DocumentTestMixin from mayan.apps.document_states.literals import WORKFLOW_ACTION_ON_ENTRY from mayan.apps.document_states.tests.mixins import WorkflowTestMixin from mayan.apps.document_states.tests.test_actions import ActionTestCase +from mayan.apps.metadata.tests.mixins import MetadataTypeTestMixin from ..permissions import permission_user_mailer_use from ..workflow_actions import EmailAction @@ -66,6 +67,69 @@ class EmailActionTestCase(MailerTestMixin, WorkflowTestMixin, ActionTestCase): self.assertEqual(mail.outbox[0].to, [TEST_EMAIL_ADDRESS]) +class EmailActionTemplateTestCase(MetadataTypeTestMixin, MailerTestMixin, WorkflowTestMixin, ActionTestCase): + def test_email_action_recipient_template(self): + self._create_test_metadata_type() + self.test_document_type.metadata.create(metadata_type=self.test_metadata_type) + self.test_document.metadata.create(metadata_type=self.test_metadata_type, value=TEST_EMAIL_ADDRESS) + + self._create_test_user_mailer() + + action = EmailAction( + form_data={ + 'mailing_profile': self.test_user_mailer.pk, + 'recipient': '{{{{ document.metadata_value_of.{} }}}}'.format(self.test_metadata_type.name), + 'subject': TEST_EMAIL_SUBJECT, + 'body': '', + } + ) + action.execute(context={'document': self.test_document}) + self.assertEqual(len(mail.outbox), 1) + self.assertEqual(mail.outbox[0].from_email, TEST_EMAIL_FROM_ADDRESS) + self.assertEqual(mail.outbox[0].to, [TEST_EMAIL_ADDRESS]) + + def test_email_action_subject_template(self): + self._create_test_metadata_type() + self.test_document_type.metadata.create(metadata_type=self.test_metadata_type) + self.test_document.metadata.create(metadata_type=self.test_metadata_type, value=TEST_EMAIL_SUBJECT) + + self._create_test_user_mailer() + + action = EmailAction( + form_data={ + 'mailing_profile': self.test_user_mailer.pk, + 'recipient': TEST_EMAIL_ADDRESS, + 'subject': '{{{{ document.metadata_value_of.{} }}}}'.format(self.test_metadata_type.name), + 'body': '', + } + ) + action.execute(context={'document': self.test_document}) + self.assertEqual(len(mail.outbox), 1) + self.assertEqual(mail.outbox[0].from_email, TEST_EMAIL_FROM_ADDRESS) + self.assertEqual(mail.outbox[0].to, [TEST_EMAIL_ADDRESS]) + + def test_email_action_body_template(self): + self._create_test_metadata_type() + self.test_document_type.metadata.create(metadata_type=self.test_metadata_type) + self.test_document.metadata.create(metadata_type=self.test_metadata_type, value=TEST_EMAIL_BODY) + + self._create_test_user_mailer() + + action = EmailAction( + form_data={ + 'mailing_profile': self.test_user_mailer.pk, + 'recipient': TEST_EMAIL_ADDRESS, + 'subject': TEST_EMAIL_SUBJECT, + 'body': '{{{{ document.metadata_value_of.{} }}}}'.format(self.test_metadata_type.name), + } + ) + action.execute(context={'document': self.test_document}) + self.assertEqual(len(mail.outbox), 1) + self.assertEqual(mail.outbox[0].from_email, TEST_EMAIL_FROM_ADDRESS) + self.assertEqual(mail.outbox[0].to, [TEST_EMAIL_ADDRESS]) + self.assertEqual(mail.outbox[0].body, TEST_EMAIL_BODY) + + class EmailActionViewTestCase(DocumentTestMixin, MailerTestMixin, WorkflowTestMixin, GenericViewTestCase): auto_upload_document = False diff --git a/mayan/apps/mailer/workflow_actions.py b/mayan/apps/mailer/workflow_actions.py index d55aacfe3c..c344082ac5 100644 --- a/mayan/apps/mailer/workflow_actions.py +++ b/mayan/apps/mailer/workflow_actions.py @@ -2,10 +2,12 @@ from __future__ import absolute_import, unicode_literals import logging +from django.template import Template, Context from django.utils.translation import ugettext_lazy as _ from mayan.apps.acls.models import AccessControlList from mayan.apps.document_states.classes import WorkflowAction +from mayan.apps.document_states.exceptions import WorkflowStateActionError from .models import UserMailer from .permissions import permission_user_mailer_use @@ -28,7 +30,8 @@ class EmailAction(WorkflowAction): 'class': 'django.forms.CharField', 'kwargs': { 'help_text': _( 'Email address of the recipient. Can be multiple addresses ' - 'separated by comma or semicolon.' + 'separated by comma or semicolon. A template can be used ' + 'to reference properties of the document.' ), 'required': True } @@ -36,17 +39,23 @@ class EmailAction(WorkflowAction): 'subject': { 'label': _('Subject'), 'class': 'django.forms.CharField', 'kwargs': { + 'help_text': _( + 'Subject of the email. Can be a string or a template.' + ), 'required': True } }, 'body': { 'label': _('Body'), 'class': 'django.forms.CharField', 'kwargs': { - 'help_text': _('Body of the email to send.'), + 'help_text': _( + 'Body of the email to send. Can be a string or a template.' + ), 'required': True } }, } + field_order = ('mailing_profile', 'recipient', 'subject', 'body') label = _('Send email') widgets = { 'body': { @@ -56,10 +65,42 @@ class EmailAction(WorkflowAction): permission = permission_user_mailer_use def execute(self, context): + try: + recipient = Template(self.form_data['recipient']).render( + context=Context(context) + ) + except Exception as exception: + raise WorkflowStateActionError( + _('Recipient template error: %s') % exception + ) + else: + logger.debug('Recipient result: %s', recipient) + + try: + subject = Template(self.form_data['subject']).render( + context=Context(context) + ) + except Exception as exception: + raise WorkflowStateActionError( + _('Subject template error: %s') % exception + ) + else: + logger.debug('Subject result: %s', subject) + + try: + body = Template(self.form_data['body']).render( + context=Context(context) + ) + except Exception as exception: + raise WorkflowStateActionError( + _('Body template error: %s') % exception + ) + else: + logger.debug('Body result: %s', body) + user_mailer = self.get_user_mailer() user_mailer.send( - to=self.form_data['recipient'], subject=self.form_data['subject'], - body=self.form_data['body'], + to=recipient, subject=subject, body=body, ) def get_form_schema(self, request): @@ -74,6 +115,7 @@ class EmailAction(WorkflowAction): self.fields['mailing_profile']['kwargs']['queryset'] = queryset return { + 'field_order': self.field_order, 'fields': self.fields, 'widgets': self.widgets }