Projet

Général

Profil

0001-workflows-add-history-entry-after-successful-edit-ac.patch

Frédéric Péters, 03 avril 2021 20:49

Télécharger (6,74 ko)

Voir les différences:

Subject: [PATCH] workflows: add history entry after successful edit action,
 not before (#52629)

 tests/backoffice_pages/test_all.py |  3 +++
 tests/form_pages/test_all.py       |  8 ++++++++
 wcs/formdata.py                    |  3 ++-
 wcs/forms/root.py                  | 18 ++++++++++++++++--
 wcs/workflows.py                   | 13 +++++++++----
 5 files changed, 38 insertions(+), 7 deletions(-)
tests/backoffice_pages/test_all.py
2534 2534

  
2535 2535
    resp = app.get('/backoffice/management/form-title/%s/' % number31.id)
2536 2536
    assert (' with the number %s.' % number31.get_display_id()) in resp.text
2537
    assert len(form_class().get(number31.id).evolution) == 2  # (just submitted, new)
2537 2538
    resp = resp.form.submit('button_wfedit')
2538 2539
    resp = resp.follow()
2539 2540
    assert 'http://www.example.com/test.pdf' in resp.text  # make sure sidebar has details
......
2546 2547
    resp = resp.form.submit('submit')
2547 2548
    resp = resp.follow()
2548 2549
    assert form_class().get(number31.id).data['2'] == 'bar'
2550
    assert len(form_class().get(number31.id).evolution) == 3
2551
    assert form_class().get(number31.id).evolution[-1].who == str(user.id)
2549 2552
    number31.store()
2550 2553

  
2551 2554

  
tests/form_pages/test_all.py
2504 2504
    resp = page.forms[0].submit('button_editable')
2505 2505
    assert resp.location.startswith('http://example.net/test/%s/wfedit-' % data_id)
2506 2506
    resp = resp.follow()
2507
    # check there's no new "phantom" history entry
2508
    assert len(formdef.data_class().get(data_id).evolution) == 1
2507 2509
    assert resp.forms[0]['f1'].value == 'foo'
2508 2510
    resp.forms[0]['f1'] = 'foo2'
2509 2511

  
......
2518 2520
    resp = resp.follow()
2519 2521
    assert 'foo2' in resp.text  # modified value is there
2520 2522
    assert 'barXYZ' in resp.text  # unchanged value is still there
2523
    assert len(formdef.data_class().get(data_id).evolution) == 2  # new history entry
2524
    assert formdef.data_class().get(data_id).evolution[-1].who == '_submitter'
2525
    assert formdef.data_class().get(data_id).evolution[-1].status is None
2521 2526

  
2522 2527
    # modify workflow to jump to another status after the edition
2523 2528
    st2 = workflow.add_status('Status2', 'st2')
......
2545 2550
    assert 'foo3' in resp.text  # modified value is there
2546 2551
    assert 'barXYZ' in resp.text  # unchanged value is still there
2547 2552
    assert formdef.data_class().get(data_id).status == 'wf-%s' % st2.id
2553
    assert len(formdef.data_class().get(data_id).evolution) == 3  # single new history entry
2554
    assert formdef.data_class().get(data_id).evolution[-1].who == '_submitter'
2555
    assert formdef.data_class().get(data_id).evolution[-1].status == 'wf-%s' % st2.id
2548 2556

  
2549 2557
    # jump to a nonexistent status == do not jump, but add a LoggedError
2550 2558
    if pub.is_using_postgresql():
wcs/formdata.py
630 630
        except KeyError:
631 631
            return None
632 632

  
633
    def jump_status(self, status_id):
633
    def jump_status(self, status_id, user_id=None):
634 634
        if status_id == '_previous':
635 635
            previous_status = self.pop_previous_marked_status()
636 636
            if not previous_status:
......
655 655
        evo = Evolution(self)
656 656
        evo.time = time.localtime()
657 657
        evo.status = status
658
        evo.who = user_id
658 659
        self.evolution.append(evo)
659 660
        self.status = status
660 661
        self.store()
wcs/forms/root.py
34 34
from quixote.util import randbytes
35 35

  
36 36
from wcs.categories import Category
37
from wcs.formdata import FormData
37
from wcs.formdata import Evolution, FormData
38 38
from wcs.formdef import FormDef
39 39
from wcs.forms.common import FormStatusPage, FormTemplateMixin
40 40
from wcs.qommon.admin.texts import TextsDirectory
......
1414 1414
        url = None
1415 1415
        for item in wf_status.items:
1416 1416
            if item.id == self.edit_action_id:
1417
                user = get_request().user
1418
                user_id = None
1419
                if user:
1420
                    if get_request().is_in_frontoffice() and self.edited_data.is_submitter(user):
1421
                        user_id = '_submitter'
1422
                    else:
1423
                        user_id = user.id
1417 1424
                wf_status = item.get_target_status(self.edited_data)
1418 1425
                if wf_status:
1419
                    self.edited_data.jump_status(wf_status[0].id)
1426
                    self.edited_data.jump_status(wf_status[0].id, user_id=user_id)
1420 1427
                    url = self.edited_data.perform_workflow()
1428
                else:
1429
                    # add history entry
1430
                    evo = Evolution()
1431
                    evo.time = time.localtime()
1432
                    evo.who = user_id
1433
                    self.edited_data.evolution.append(evo)
1434
                    self.edited_data.store()
1421 1435
                break
1422 1436
        return redirect(url or '.')
1423 1437

  
wcs/workflows.py
1675 1675
                break
1676 1676
            if next_url:
1677 1677
                if not form.has_errors():
1678
                    filled.evolution.append(evo)
1679
                    if evo.status:
1680
                        filled.status = evo.status
1681
                    filled.store()
1678
                    if evo.parts or evo.status or evo.comment or evo.status:
1679
                        # add evolution entry only if there's some content
1680
                        # within, i.e. do not register anything in the case of
1681
                        # a single edit action (where the evolution should be
1682
                        # appended only after successful edit).
1683
                        filled.evolution.append(evo)
1684
                        if evo.status:
1685
                            filled.status = evo.status
1686
                        filled.store()
1682 1687
                return next_url
1683 1688

  
1684 1689
        if form.has_errors():
1685
-