Projet

Général

Profil

0001-api-export-roles-associated-to-a-formdata-8485.patch

Frédéric Péters, 03 novembre 2015 20:12

Télécharger (6,34 ko)

Voir les différences:

Subject: [PATCH] api: export roles associated to a formdata (#8485)

 help/fr/api-get.page | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 tests/test_api.py    | 20 +++++++++++++++++++-
 wcs/formdata.py      | 22 ++++++++++++++++++++++
 3 files changed, 89 insertions(+), 1 deletion(-)
help/fr/api-get.page
67 67
                "liste": "lalettre@example.com"
68 68
            }
69 69
        }
70
    },
71
    "roles": {
72
      "_receiver": [
73
        {
74
          "emails_to_members": false,
75
          "name": "Agents traitants",
76
          "text": "",
77
          "emails": [],
78
          "slug": "agents-traitants",
79
          "details": "",
80
          "id": "1",
81
          "allows_backoffice_access": true
82
        }
83
      ],
84
      "concerned": [
85
        {
86
          "emails_to_members": false,
87
          "name": "Agents traitants",
88
          "text": "",
89
          "emails": [],
90
          "slug": "agents-traitants",
91
          "details": "",
92
          "id": "1",
93
          "allows_backoffice_access": true
94
        }
95
      ],
96
      "actions": [
97
        {
98
          "emails_to_members": false,
99
          "name": "Agents traitants",
100
          "text": "",
101
          "emails": [],
102
          "slug": "agents-traitants",
103
          "details": "",
104
          "name": "test",
105
          "id": "1",
106
          "allows_backoffice_access": true
107
        }
108
      ]
70 109
    }
71 110
}
72 111
</code>
......
75 114
Seuls les champs ayant un <em>nom de variable</em> sont exportés dans <code>fields</code>.
76 115
</p>
77 116

  
117
<p>
118
Concernant les rôles et fonctions de workflow, les différents intervenants sont
119
listés dans l'attribut <code>roles</code>, en différentes séries qui vont
120
dépendre de fonctions attachées au workflow.  Deux séries sont particulières,
121
la série <code>concerned</code> reprend les rôles concernés par la demande et
122
la série <code>actions</code> reprend les rôles disposant d'une capacité
123
d'action sur la demande.
124
</p>
125

  
78 126
<note>
79 127
 <p>
80 128
  Il est bien sûr nécessaire de disposer des autorisations nécessaires pour
tests/test_api.py
17 17
from wcs.formdef import FormDef
18 18
from wcs.categories import Category
19 19
from wcs.data_sources import NamedDataSource
20
from wcs.workflows import Workflow
20 21
from wcs import fields
21 22
from wcs.api import sign_url
22 23

  
......
410 411
    Role.wipe()
411 412
    role = Role(name='test')
412 413
    role.store()
414
    another_role = Role(name='another')
415
    another_role.store()
413 416
    FormDef.wipe()
414 417
    formdef = FormDef()
415 418
    formdef.name = 'test'
......
421 424
        fields.ItemField(id='4', label='foobar5', varname='item',
422 425
            data_source={'type': 'foobar'}),
423 426
    ]
424
    formdef.workflow_roles = {'_receiver': role.id}
427
    workflow = Workflow.get_default_workflow()
428
    workflow.roles['_foobar'] = 'Foobar'
429
    workflow.id = '2'
430
    workflow.store()
431
    formdef.workflow_id = workflow.id
432
    formdef.workflow_roles = {'_receiver': role.id,
433
                              '_foobar': another_role.id}
425 434
    formdef.store()
426 435
    item_field = formdef.fields[4]
427 436

  
......
442 451
            formdata.data, item_field.id)
443 452
    formdata.user_id = local_user.id
444 453
    formdata.just_created()
454
    formdata.status = 'wf-new'
455
    formdata.evolution[-1].status = 'wf-new'
445 456
    formdata.store()
446 457

  
447 458
    resp = get_app(pub).get(
......
469 480
    assert resp.json['fields']['item'] == 'foo'
470 481
    assert resp.json['fields']['item_raw'] == '1'
471 482
    assert resp.json['fields']['item_structured'] == {'id': '1', 'text': 'foo', 'more': 'XXX'}
483
    assert resp.json['workflow']['status']['name'] == 'New'
484

  
485
    assert [x.get('id') for x in resp.json['roles']['_receiver']] == [str(role.id)]
486
    assert [x.get('id') for x in resp.json['roles']['_foobar']] == [str(another_role.id)]
487
    assert [x.get('id') for x in resp.json['roles']['concerned']] == [str(role.id), str(another_role.id)]
488
    assert [x.get('id') for x in resp.json['roles']['actions']] == [str(role.id)]
472 489

  
473 490
def test_user_forms(pub, local_user):
474 491
    FormDef.wipe()
......
478 495
        fields.StringField(id='0', label='foobar', varname='foobar'),
479 496
        fields.StringField(id='1', label='foobar2'),]
480 497
    formdef.store()
498
    formdef.data_class().wipe()
481 499

  
482 500
    resp = get_app(pub).get(sign_uri('/api/user/forms', user=local_user))
483 501
    assert len(resp.json) == 0
wcs/formdata.py
601 601
        if self.workflow_data:
602 602
            data['workflow']['data'] = self.workflow_data
603 603

  
604
        # add a roles dictionary, with workflow functions and two special
605
        # entries for concerned/actions roles.
606
        data['roles'] = {}
607
        workflow_roles = {}
608
        if self.formdef.workflow_roles:
609
            workflow_roles.update(self.formdef.workflow_roles)
610
        if self.workflow_roles:
611
            workflow_roles.update(self.workflow_roles)
612
        for workflow_role in workflow_roles:
613
            data['roles'][workflow_role] = workflow_roles.get(workflow_role)
614
        data['roles']['concerned'] = self.get_concerned_roles()
615
        data['roles']['actions'] = self.get_actions_roles()
616

  
617
        for role_key in data['roles']:
618
            # exclude special _submitter value
619
            role_list = [x for x in data['roles'][role_key] if x != '_submitter']
620
            # get role objects
621
            role_list = [Role.get(x, ignore_errors=True) for x in role_list]
622
            # export as json dicts
623
            role_list = [x.get_json_export_dict() for x in role_list if x is not None]
624
            data['roles'][role_key] = role_list
625

  
604 626
        return data
605 627

  
606 628
    def export_to_json(self, include_files=True):
607
-