Projet

Général

Profil

0001-wf-add-unlink-user-action-69120.patch

Paul Marillonnet, 07 novembre 2022 17:16

Télécharger (5,08 ko)

Voir les différences:

Subject: [PATCH] wf: add unlink user action (#69120)

 tests/workflow/test_carddata.py | 36 ++++++++++++++++++++++++
 tests/workflow/test_formdata.py | 36 ++++++++++++++++++++++++
 wcs/wf/unlink_user.py           | 50 +++++++++++++++++++++++++++++++++
 3 files changed, 122 insertions(+)
 create mode 100644 wcs/wf/unlink_user.py
tests/workflow/test_carddata.py
1618 1618
    formdata.just_created()
1619 1619
    formdata.perform_workflow()
1620 1620
    assert carddef.data_class().count() == 0
1621

  
1622

  
1623
def test_unlink_user(pub):
1624
    CardDef.wipe()
1625
    FormDef.wipe()
1626
    pub.user_class.wipe()
1627

  
1628
    user = pub.user_class()
1629
    user.email = 'test@example.net'
1630
    user.name_identifiers = ['xyz']
1631
    user.store()
1632

  
1633
    wf = Workflow(name='test-unlink-user')
1634
    wf.possible_status = Workflow.get_default_workflow().possible_status[:]
1635
    unlink = wf.possible_status[1].add_action('unlink_user', id='_unlink', prepend=True)
1636
    unlink.label = 'Unlink User'
1637
    unlink.varname = 'mycard'
1638
    wf.store()
1639

  
1640
    carddef = CardDef()
1641
    carddef.name = 'Person'
1642
    carddef.workflow_id = wf.id
1643
    carddef.store()
1644
    carddef.data_class().wipe()
1645

  
1646
    carddata = carddef.data_class()()
1647
    carddata.data = {'0': 'Foo', '1': 'Bar'}
1648
    carddata.user_id = user.id
1649
    carddata.just_created()
1650
    carddata.store()
1651

  
1652
    assert carddef.data_class().select()[0].user.id == user.id
1653

  
1654
    carddata.perform_workflow()
1655

  
1656
    assert carddef.data_class().select()[0].user is None
tests/workflow/test_formdata.py
398 398
    formdata.perform_workflow()
399 399
    assert target_formdef.data_class().count() == 2
400 400
    assert formdata.submission_context == {'a': 'b'}
401

  
402

  
403
def test_unlink_user(pub):
404
    CardDef.wipe()
405
    FormDef.wipe()
406
    pub.user_class.wipe()
407

  
408
    user = pub.user_class()
409
    user.email = 'test@example.net'
410
    user.name_identifiers = ['xyz']
411
    user.store()
412

  
413
    wf = Workflow(name='test-unlink-user')
414
    wf.possible_status = Workflow.get_default_workflow().possible_status[:]
415
    unlink = wf.possible_status[1].add_action('unlink_user', id='_unlink', prepend=True)
416
    unlink.label = 'Unlink User'
417
    unlink.varname = 'mycard'
418
    wf.store()
419

  
420
    formdef = FormDef()
421
    formdef.name = 'Person'
422
    formdef.workflow_id = wf.id
423
    formdef.store()
424
    formdef.data_class().wipe()
425

  
426
    formdata = formdef.data_class()()
427
    formdata.data = {'0': 'Foo', '1': 'Bar'}
428
    formdata.user_id = user.id
429
    formdata.just_created()
430
    formdata.store()
431

  
432
    assert formdef.data_class().select()[0].user.id == user.id
433

  
434
    formdata.perform_workflow()
435

  
436
    assert formdef.data_class().select()[0].user is None
wcs/wf/unlink_user.py
1
# w.c.s. - web application for online forms
2
# Copyright (C) 2005-2022  Entr'ouvert
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, see <http://www.gnu.org/licenses/>.
16

  
17
from quixote import get_publisher, get_session
18

  
19
from wcs.carddef import CardDef
20
from wcs.workflows import WorkflowStatusItem, register_item_class
21

  
22
from ..qommon import _
23

  
24

  
25
class UnlinkUserStatusItem(WorkflowStatusItem):
26
    description = _('Unlink user')
27
    key = 'unlink_user'
28
    category = 'formdata-action'
29

  
30
    def perform(self, formdata):
31
        datatype = 'form'
32
        if isinstance(formdata.formdef, CardDef):
33
            datatype = 'card'
34
        if not formdata.user_id:
35
            if datatype == 'card':
36
                error = _('No user linked to card.')
37
            else:
38
                error = _('No user linked to form.')
39
            get_publisher().record_error(
40
                error,
41
                formdata=formdata,
42
                status_item=self,
43
            )
44
        else:
45
            get_session().mark_anonymous_formdata(formdata)
46
            formdata.user_id = None
47
            formdata.store()
48

  
49

  
50
register_item_class(UnlinkUserStatusItem)
0
-