From f9def18b32c68a232b142b5526accd271eec919b Mon Sep 17 00:00:00 2001 From: Thomas NOEL Date: Tue, 26 Sep 2017 17:13:47 +0200 Subject: [PATCH] workflows: allow attachments in emails (#8274) --- wcs/workflows.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/wcs/workflows.py b/wcs/workflows.py index 191aada7..edf7c5aa 100644 --- a/wcs/workflows.py +++ b/wcs/workflows.py @@ -38,6 +38,7 @@ from quixote.html import htmltext import qommon.errors from wcs.roles import Role, logged_users_role, get_user_roles +from wcs.fields import FileField from wcs.formdef import FormDef from wcs.formdata import Evolution @@ -2015,6 +2016,7 @@ class SendmailWorkflowStatusItem(WorkflowStatusItem): subject = None body = None custom_from = None + attachments = None comment = None @@ -2053,7 +2055,7 @@ class SendmailWorkflowStatusItem(WorkflowStatusItem): return _('Send mail (not completed)') def get_parameters(self): - return ('to', 'subject', 'body', 'custom_from') + return ('to', 'subject', 'body', 'attachments', 'custom_from') def fill_admin_form(self, form): self.add_parameters_widgets(form, self.get_parameters()) @@ -2076,6 +2078,38 @@ class SendmailWorkflowStatusItem(WorkflowStatusItem): value=self.body, cols=80, rows=10, validation_function=ComputedExpressionWidget.validate_ezt, hint=_('Available variables: url, url_status, details, name, number, comment, field_NAME')) + + attachments_fields = [(None, '---', None)] + attachments_varnameless_fields = [] + for field in self.parent.parent.get_backoffice_fields(): + if field.key != 'file': + continue + if field.varname: + codename = 'form_var_%s_raw' % field.varname + else: + codename = 'form_f%s' % field.id # = form_fbo + attachments_varnameless_fields.append(codename) + attachments_fields.append((codename, field.label, codename)) + # filter: don't show removed fields without varname + attachments = [attachment for attachment in self.attachments or [] + if ((not attachment.startswith('form_fbo')) or + (attachment in attachments_varnameless_fields))] + if 'attachments' in parameters: + if len(attachments_fields) > 1: + form.add(WidgetList, '%sattachments' % prefix, title=_('Attachments'), + element_type=SingleSelectWidgetWithOther, + value=attachments, + add_element_label=_('Add attachment'), + element_kwargs={'render_br': False, 'options': attachments_fields}) + else: + form.add(WidgetList, '%sattachments' % prefix, + title=_('Attachments (Python expressions)'), + element_type=StringWidget, + value=attachments, + add_element_label=_('Add attachment'), + element_kwargs={'render_br': False, 'size': 50}, + advanced=not(bool(attachments))) + if 'custom_from' in parameters: form.add(ComputedExpressionWidget, '%scustom_from' % prefix, title=_('Custom From Address'), value=self.custom_from, @@ -2152,14 +2186,32 @@ class SendmailWorkflowStatusItem(WorkflowStatusItem): if self.custom_from: email_from = self.compute(self.custom_from) + attachments = [] + if self.attachments: + global_eval_dict = get_publisher().get_global_eval_dict() + local_eval_dict = get_publisher().substitutions.get_context_variables() + for attachment in self.attachments: + try: + picklableupload = eval(attachment, global_eval_dict, local_eval_dict) + except: + continue + if picklableupload: + try: + picklableupload = FileField.convert_value_from_anything(picklableupload) + except ValueError: + continue + attachments.append(picklableupload) + if len(addresses) > 1: emails.email(mail_subject, mail_body, email_rcpt=None, bcc=addresses, email_from=email_from, exclude_current_user=False, + attachments=attachments, fire_and_forget=True) else: emails.email(mail_subject, mail_body, email_rcpt=addresses, email_from=email_from, exclude_current_user=False, + attachments=attachments, fire_and_forget=True) register_item_class(SendmailWorkflowStatusItem) -- 2.14.2