Projet

Général

Profil

0001-workflows-allow-attachments-in-emails-8274.patch

Thomas Noël, 30 septembre 2017 19:56

Télécharger (4,93 ko)

Voir les différences:

Subject: [PATCH] workflows: allow attachments in emails (#8274)

 wcs/workflows.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 53 insertions(+), 1 deletion(-)
wcs/workflows.py
38 38
import qommon.errors
39 39

  
40 40
from wcs.roles import Role, logged_users_role, get_user_roles
41
from wcs.fields import FileField
41 42
from wcs.formdef import FormDef
42 43
from wcs.formdata import Evolution
43 44

  
......
2015 2016
    subject = None
2016 2017
    body = None
2017 2018
    custom_from = None
2019
    attachments = None
2018 2020

  
2019 2021
    comment = None
2020 2022

  
......
2053 2055
            return _('Send mail (not completed)')
2054 2056

  
2055 2057
    def get_parameters(self):
2056
        return ('to', 'subject', 'body', 'custom_from')
2058
        return ('to', 'subject', 'body', 'attachments', 'custom_from')
2057 2059

  
2058 2060
    def fill_admin_form(self, form):
2059 2061
        self.add_parameters_widgets(form, self.get_parameters())
......
2076 2078
                     value=self.body, cols=80, rows=10,
2077 2079
                     validation_function=ComputedExpressionWidget.validate_ezt,
2078 2080
                     hint=_('Available variables: url, url_status, details, name, number, comment, field_NAME'))
2081

  
2082
        attachments_fields = [(None, '---', None)]
2083
        attachments_varnameless_fields = []
2084
        for field in self.parent.parent.get_backoffice_fields():
2085
            if field.key != 'file':
2086
                continue
2087
            if field.varname:
2088
                codename = 'form_var_%s_raw' % field.varname
2089
            else:
2090
                codename = 'form_f%s' % field.id  # = form_fbo<n>
2091
                attachments_varnameless_fields.append(codename)
2092
            attachments_fields.append((codename, field.label, codename))
2093
        # filter: don't show removed fields without varname
2094
        attachments = [attachment for attachment in self.attachments or []
2095
                       if ((not attachment.startswith('form_fbo')) or
2096
                           (attachment in attachments_varnameless_fields))]
2097
        if 'attachments' in parameters:
2098
            if len(attachments_fields) > 1:
2099
                form.add(WidgetList, '%sattachments' % prefix, title=_('Attachments'),
2100
                         element_type=SingleSelectWidgetWithOther,
2101
                         value=attachments,
2102
                         add_element_label=_('Add attachment'),
2103
                         element_kwargs={'render_br': False, 'options': attachments_fields})
2104
            else:
2105
                form.add(WidgetList, '%sattachments' % prefix,
2106
                         title=_('Attachments (Python expressions)'),
2107
                         element_type=StringWidget,
2108
                         value=attachments,
2109
                         add_element_label=_('Add attachment'),
2110
                         element_kwargs={'render_br': False, 'size': 50},
2111
                         advanced=not(bool(attachments)))
2112

  
2079 2113
        if 'custom_from' in parameters:
2080 2114
            form.add(ComputedExpressionWidget, '%scustom_from' % prefix,
2081 2115
                     title=_('Custom From Address'), value=self.custom_from,
......
2152 2186
        if self.custom_from:
2153 2187
            email_from = self.compute(self.custom_from)
2154 2188

  
2189
        attachments = []
2190
        if self.attachments:
2191
            global_eval_dict = get_publisher().get_global_eval_dict()
2192
            local_eval_dict = get_publisher().substitutions.get_context_variables()
2193
            for attachment in self.attachments:
2194
                try:
2195
                    picklableupload = eval(attachment, global_eval_dict, local_eval_dict)
2196
                except:
2197
                    continue
2198
                if picklableupload:
2199
                    try:
2200
                        picklableupload = FileField.convert_value_from_anything(picklableupload)
2201
                    except ValueError:
2202
                        continue
2203
                    attachments.append(picklableupload)
2204

  
2155 2205
        if len(addresses) > 1:
2156 2206
            emails.email(mail_subject, mail_body, email_rcpt=None,
2157 2207
                    bcc=addresses, email_from=email_from,
2158 2208
                    exclude_current_user=False,
2209
                    attachments=attachments,
2159 2210
                    fire_and_forget=True)
2160 2211
        else:
2161 2212
            emails.email(mail_subject, mail_body, email_rcpt=addresses,
2162 2213
                    email_from=email_from, exclude_current_user=False,
2214
                    attachments=attachments,
2163 2215
                    fire_and_forget=True)
2164 2216
register_item_class(SendmailWorkflowStatusItem)
2165 2217

  
2166
-