Projet

Général

Profil

0001-workflows-add-option-to-configure-notification-targe.patch

Frédéric Péters, 14 mai 2022 11:55

Télécharger (4,76 ko)

Voir les différences:

Subject: [PATCH] workflows: add option to configure notification target URL
 (#64584)

 tests/workflow/test_notification.py | 44 +++++++++++++++++++++++++++++
 wcs/wf/notification.py              | 24 ++++++++++++++--
 2 files changed, 65 insertions(+), 3 deletions(-)
tests/workflow/test_notification.py
278 278
    logged_error = sql_pub.loggederror_class.select()[2]
279 279
    assert logged_error.summary == 'Failed to compute template'
280 280
    assert logged_error.formdata_id == str(formdata.id)
281

  
282

  
283
def test_notifications_target_url(sql_pub, http_requests):
284
    pub = sql_pub
285
    pub.substitutions.feed(pub)
286
    pub.user_class.wipe()
287
    user = pub.user_class()
288
    user.name_identifiers = ['xxx']
289
    user.store()
290

  
291
    FormDef.wipe()
292

  
293
    formdef = FormDef()
294
    formdef.name = 'baz'
295
    formdef.fields = []
296
    formdef.store()
297
    formdef.data_class().wipe()
298

  
299
    formdata = formdef.data_class()()
300
    formdata.user_id = user.id
301
    formdata.just_created()
302
    formdata.store()
303

  
304
    if not pub.site_options.has_section('variables'):
305
        pub.site_options.add_section('variables')
306
    pub.site_options.set('variables', 'portal_url', 'https://portal/')
307

  
308
    item = SendNotificationWorkflowStatusItem()
309
    item.title = 'xxx'
310
    item.body = 'XXX'
311
    item.target_url = '{{ portal_url }}plop'
312

  
313
    http_requests.empty()
314
    item.perform(formdata)
315
    assert http_requests.count() == 1
316
    assert http_requests.get_last('url') == 'https://portal/api/notification/add/'
317
    assert json.loads(http_requests.get_last('body')) == {
318
        'body': 'XXX',
319
        'url': 'https://portal/plop',
320
        'id': 'formdata:%s' % formdata.get_display_id(),
321
        'origin': '',
322
        'summary': 'xxx',
323
        'name_ids': ['xxx'],
324
    }
wcs/wf/notification.py
39 39
    title = None
40 40
    body = None
41 41
    origin = None
42
    target_url = None
42 43

  
43 44
    # webservice parameters
44 45
    varname = 'notification'
......
70 71
        return _(self.description)
71 72

  
72 73
    def get_parameters(self):
73
        return ('to', 'users_template', 'title', 'body', 'origin', 'condition')
74
        return ('to', 'users_template', 'title', 'body', 'target_url', 'origin', 'condition')
74 75

  
75 76
    def add_parameters_widgets(self, form, parameters, prefix='', formdef=None, **kwargs):
76 77
        if 'to' in parameters:
......
116 117
                rows=5,
117 118
                validation_function=ComputedExpressionWidget.validate_template,
118 119
            )
120
        if 'target_url' in parameters:
121
            form.add(
122
                StringWidget,
123
                '%surl' % prefix,
124
                title=_('URL'),
125
                value=self.url,
126
                required=False,
127
                advanced=True,
128
                hint=_(
129
                    'Defaults to card/form URL. Common variables are available with the {{variable}} syntax.'
130
                ),
131
            )
119 132
        if 'origin' in parameters:
120 133
            form.add(
121 134
                StringWidget,
......
140 153

  
141 154
    def get_computed_strings(self):
142 155
        yield from super().get_computed_strings()
143
        yield from [self.title, self.body, self.users_template]
156
        yield from [self.title, self.body, self.users_template, self.target_url]
144 157

  
145 158
    def perform(self, formdata):
146 159
        if not (self.is_available() and (self.to or self.users_template) and self.title and self.body):
......
222 235
        if not name_ids:
223 236
            return
224 237

  
238
        if self.target_url:
239
            target_url = self.compute(self.target_url, allow_python=False, allow_ezt=False)
240
        else:
241
            target_url = formdata.get_url()
242

  
225 243
        self.post_data = {
226 244
            'summary': title,
227 245
            'body': body,
228
            'url': formdata.get_url(),
246
            'url': target_url,
229 247
            'origin': self.origin or '',
230 248
            'id': 'formdata:%s' % formdata.get_display_id(),
231 249
            'name_ids': list(name_ids),
232
-