Projet

Général

Profil

0001-misc-auto-enable-geolocation-when-there-s-such-an-ac.patch

Frédéric Péters, 01 novembre 2021 15:21

Télécharger (11,9 ko)

Voir les différences:

Subject: [PATCH] misc: auto-enable geolocation when there's such an action in
 workflow (#13670)

 tests/admin_pages/test_form.py | 26 ----------------
 tests/test_snapshots.py        |  3 +-
 tests/workflow/test_all.py     | 55 ++++++++++++++++++++++++++++++++++
 wcs/admin/forms.py             | 45 ++++++----------------------
 wcs/backoffice/cards.py        |  6 ----
 wcs/formdef.py                 |  2 ++
 wcs/workflows.py               | 19 ++++++++++--
 7 files changed, 84 insertions(+), 72 deletions(-)
tests/admin_pages/test_form.py
321 321
    assert_option_display(resp, 'Online Status', 'Inactive by date')
322 322

  
323 323

  
324
def test_forms_edit_geolocation(pub, formdef):
325
    create_superuser(pub)
326
    create_role(pub)
327

  
328
    app = login(get_app(pub))
329
    resp = app.get('/backoffice/forms/1/')
330

  
331
    # enable geolocation
332
    resp = resp.click('Geolocation')
333
    resp.forms[0]['geoloc_label'] = 'Foobar'
334
    resp = resp.forms[0].submit()
335
    assert resp.location == 'http://example.net/backoffice/forms/1/'
336
    resp = resp.follow()
337
    assert_option_display(resp, 'Geolocation', 'Enabled')
338
    assert FormDef.get(formdef.id).geolocations == {'base': 'Foobar'}
339

  
340
    # and disable it
341
    resp = resp.click('Geolocation')
342
    resp.forms[0]['geoloc_label'] = ''
343
    resp = resp.forms[0].submit()
344
    assert resp.location == 'http://example.net/backoffice/forms/1/'
345
    resp = resp.follow()
346
    assert_option_display(resp, 'Geolocation', 'Disabled')
347
    assert FormDef.get(formdef.id).geolocations is None
348

  
349

  
350 324
def test_form_title_change(pub):
351 325
    create_superuser(pub)
352 326
    create_role(pub)
tests/test_snapshots.py
345 345
    resp = resp.click(href='%s/view/' % snapshot.id)
346 346
    assert 'This card model is readonly' in resp.text
347 347
    assert '<p>%s</p>' % localstrftime(snapshot.timestamp) in resp.text
348
    resp = resp.click('Geolocation')
348
    # check option dialogs only have a cancel button
349
    resp = resp.click('User support')
349 350
    assert [x[0].name for x in resp.form.fields.values() if x[0].tag == 'button'] == ['cancel']
350 351
    assert pub.custom_view_class.count() == 0  # custom views are not restore on preview
351 352

  
tests/workflow/test_all.py
3115 3115
    assert formdata.get_criticality_level_object().name == 'green'
3116 3116

  
3117 3117

  
3118
def test_geolocate_action_enable_geolocation(two_pubs):
3119
    # switch to a workflow with geolocation
3120
    formdef = FormDef()
3121
    formdef.name = 'foo'
3122
    formdef.fields = []
3123
    formdef.store()
3124

  
3125
    workflow = Workflow(name='wf')
3126
    st1 = workflow.add_status('Status1', 'st1')
3127
    item = GeolocateWorkflowStatusItem()
3128
    item.method = 'address_string'
3129
    item.address_string = '{{form_var_string}}, paris, france'
3130
    item.parent = st1
3131
    st1.items.append(item)
3132
    workflow.store()
3133

  
3134
    formdef.change_workflow(workflow)
3135
    assert formdef.geolocations
3136

  
3137
    if two_pubs.is_using_postgresql():
3138
        conn, cur = sql.get_connection_and_cursor()
3139
        assert column_exists_in_table(cur, formdef.table_name, 'geoloc_base')
3140
        conn.commit()
3141
        cur.close()
3142

  
3143
    # change to current workflow
3144
    workflow = Workflow(name='wf2')
3145
    st1 = workflow.add_status('Status1', 'st1')
3146
    workflow.store()
3147

  
3148
    formdef = FormDef()
3149
    formdef.name = 'bar'
3150
    formdef.fields = []
3151
    formdef.workflow = workflow
3152
    formdef.store()
3153
    assert not formdef.geolocations
3154

  
3155
    item = GeolocateWorkflowStatusItem()
3156
    item.method = 'address_string'
3157
    item.address_string = '{{form_var_string}}, paris, france'
3158
    item.parent = st1
3159
    st1.items.append(item)
3160
    workflow.store()
3161
    get_response().process_after_jobs()
3162

  
3163
    formdef.refresh_from_storage()
3164
    assert formdef.geolocations
3165

  
3166
    if two_pubs.is_using_postgresql():
3167
        conn, cur = sql.get_connection_and_cursor()
3168
        assert column_exists_in_table(cur, formdef.table_name, 'geoloc_base')
3169
        conn.commit()
3170
        cur.close()
3171

  
3172

  
3118 3173
def test_geolocate_address(two_pubs):
3119 3174
    if two_pubs.is_using_postgresql():
3120 3175
        two_pubs.loggederror_class.wipe()
wcs/admin/forms.py
211 211
        'keywords',
212 212
        'category',
213 213
        'management',
214
        'geolocations',
215 214
        'appearance',
216 215
        'templates',
217 216
        'user_support',
......
371 370
        )
372 371
        return self.handle(form, _('Category'))
373 372

  
374
    def geolocations(self):
375
        form = Form(enctype='multipart/form-data')
376
        geoloc_label = (self.formdef.geolocations or {}).get('base')
377
        form.add(
378
            StringWidget,
379
            'geoloc_label',
380
            title=_('Geolocation Label'),
381
            value=geoloc_label,
382
            size=50,
383
            hint=_('Location label (empty to disable geolocation)'),
384
        )
385
        return self.handle(form, _('Geolocation'))
386

  
387 373
    def appearance(self):
388 374
        form = Form(enctype='multipart/form-data')
389 375
        form.add(
......
465 451
                'keywords',
466 452
                'category_id',
467 453
                'skip_from_360_view',
468
                'geoloc_label',
469 454
                'appearance_keywords',
470 455
                'include_download_all_button',
471 456
                'digest_template',
......
481 466
                        has_error = getattr(self, 'clean_%s' % attr)(form)
482 467
                        if has_error:
483 468
                            continue
484
                    if attr == 'geoloc_label':
485
                        if widget.parse():
486
                            self.formdef.geolocations = {'base': widget.parse()}
487
                        else:
488
                            self.formdef.geolocations = None
469
                    new_value = widget.parse()
470
                    if attr == 'digest_template':
471
                        if self.formdef.default_digest_template != new_value:
472
                            self.changed = True
473
                            if not self.formdef.digest_templates:
474
                                self.formdef.digest_templates = {}
475
                            self.formdef.digest_templates['default'] = new_value
489 476
                    else:
490
                        new_value = widget.parse()
491
                        if attr == 'digest_template':
492
                            if self.formdef.default_digest_template != new_value:
493
                                self.changed = True
494
                                if not self.formdef.digest_templates:
495
                                    self.formdef.digest_templates = {}
496
                                self.formdef.digest_templates['default'] = new_value
497
                        else:
498
                            if getattr(self.formdef, attr, None) != new_value:
499
                                setattr(self.formdef, attr, new_value)
477
                        if getattr(self.formdef, attr, None) != new_value:
478
                            setattr(self.formdef, attr, new_value)
500 479
            if not form.has_errors():
501 480
                self.formdef.store(comment=_('Changed "%s" parameters') % title)
502 481
                return redirect('..')
......
800 779
            or C_('tracking code|Disabled'),
801 780
        )
802 781

  
803
        r += self.add_option_line(
804
            'options/geolocations',
805
            _('Geolocation'),
806
            self.formdef.geolocations and C_('geolocation|Enabled') or C_('geolocation|Disabled'),
807
        )
808

  
809 782
        if get_publisher().has_site_option('formdef-captcha-option'):
810 783
            r += self.add_option_line(
811 784
                'options/captcha',
wcs/backoffice/cards.py
157 157
        r += htmltext('<h3>%s</h3>') % _('Options')
158 158
        r += htmltext('<ul class="biglist optionslist">')
159 159

  
160
        r += self.add_option_line(
161
            'options/geolocations',
162
            _('Geolocation'),
163
            self.formdef.geolocations and C_('geolocation|Enabled') or C_('geolocation|Disabled'),
164
        )
165

  
166 160
        if (
167 161
            self.formdef.default_digest_template
168 162
            or self.formdef.lateral_template
wcs/formdef.py
1721 1721
                        formdata.store()
1722 1722

  
1723 1723
        self.workflow = new_workflow
1724
        if new_workflow.has_action('geolocate') and not self.geolocations:
1725
            self.geolocations = {'base': str(_('Geolocation'))}
1724 1726
        self.store(comment=_('Workflow change'))
1725 1727
        if formdata_count:
1726 1728
            # instruct formdef to update its security rules
wcs/workflows.py
505 505
    def store(self, comment=None, *args, **kwargs):
506 506
        assert not self.is_readonly()
507 507
        must_update = False
508
        has_geolocation = False
508 509
        if self.id:
509 510
            old_self = self.get(self.id, ignore_errors=True, ignore_migration=True)
510 511
            if old_self:
......
524 525
                    new_backoffice_fields = []
525 526
                if {x.id for x in old_backoffice_fields} != {x.id for x in new_backoffice_fields}:
526 527
                    must_update = True
528

  
529
                # if a geolocation action has been added tables may have to be updated
530
                if self.has_action('geolocate') and not old_self.has_action('geolocate'):
531
                    must_update = True
532
                    has_geolocation = True
533

  
527 534
        elif self.backoffice_fields_formdef:
528 535
            must_update = True
529 536

  
......
533 540

  
534 541
        def update(job=None):
535 542
            # instruct all related carddefs/formdefs to update.
536
            for form in itertools.chain(
543
            for formdef in itertools.chain(
537 544
                self.formdefs(ignore_migration=True, order_by='id'),
538 545
                self.carddefs(ignore_migration=True, order_by='id'),
539 546
            ):
540 547
                if must_update:
541
                    form.update_storage()
542
                form.data_class().rebuild_security()
548
                    if has_geolocation and not formdef.geolocations:
549
                        formdef.geolocations = {'base': str(_('Geolocation'))}
550
                        formdef.store(comment=_('Geolocation enabled by workflow'))
551
                    formdef.update_storage()
552
                formdef.data_class().rebuild_security()
543 553

  
544 554
        if get_response():
545 555
            get_response().add_after_job(_('Reindexing cards and forms after workflow change'), update)
......
595 605
        for action in self.global_actions or []:
596 606
            yield from action.items or []
597 607

  
608
    def has_action(self, action_type):
609
        return any(x.key == action_type for x in self.get_all_items())
610

  
598 611
    def add_global_action(self, name, id=None):
599 612
        if [x for x in self.global_actions if x.name == name]:
600 613
            raise DuplicateGlobalActionNameError()
601
-