Project

General

Profile

« Previous | Next » 

Revision 84fd22a0

Added by Benjamin Dauvergne about 12 years ago

[domino] finish family update workflow

- when creating or updating a family, store the family.code_interne value into
the user object for later retrieval,
- add web service API to retrieve person to contact for children

View differences:

extra/modules/abelium_domino_workflow.py
1
from quixote import get_request
1
import re
2
import time
3

  
4
from quixote import get_request, get_publisher, get_session
5
from quixote.directory import Directory
2 6

  
3 7
from qommon.substitution import Substitutions
8
from qommon.form import Form, StringWidget
9
import qommon.misc
10
from qommon import get_logger
4 11
from publisher import WcsPublisher
5 12

  
6
from wcs.workflows import Workflow, WorkflowStatusItem, register_item_class, \
13
from wcs.workflows import Workflow, WorkflowStatusJumpItem, register_item_class, \
7 14
        render_list_of_roles, get_role_translation
15
from wcs.forms.common import FormStatusPage
8 16

  
17
from abelium_domino_ui import (is_activated, abelium_domino_ws, get_client, get_family)
18
import abelium_domino_ws
9 19

  
10

  
11
class AbeliumDominoRegisterFamilyWorkflowStatusItem(WorkflowStatusItem):
20
class AbeliumDominoRegisterFamilyWorkflowStatusItem(WorkflowStatusJumpItem):
21
    status = None
12 22
    description = N_('Abelium Domino: Register a Family')
13 23
    key = 'abelium-domino-register-family'
14
    endpoint = False
24
    label = None
15 25

  
16 26
    def render_as_line(self):
17 27
        return _('Register a Family into Abelium Domino')
18 28

  
19
    def get_parameters(self):
20
        return ()
29
    def get_family(self, formdata):
30
        try:
31
            user = formdata.get_user()
32
            if user:
33
                family = get_family(user)
34
                if family:
35
                    family.complete()
36
                return family
37
        except abelium_domino_ws.DominoException:
38
            pass
39
        return None
40

  
41
    def fill_form(self, form, formdata, user):
42
        family = self.get_family(formdata)
43
        if 'family_id' not in form._names:
44
            form.add(StringWidget, 'family_id', title=_('Family internal code'),
45
                     value=family and family.code_interne.encode('utf8'))
46
            if not family:
47
                form.add_submit('create_button%s' % self.id, _('Create the family'))
48
            form.add_submit('update_button%s' % self.id, _('Update the family'))
49

  
50
    def update(self, form, formdata, user, evo):
51
        fid_widget = form.get_widget('family_id')
52
        code_interne = fid_widget.parse()
53
        try:
54
            code_interne = int(code_interne)
55
        except ValueError:
56
            raise ValueError('Le code interne est invalide')
57
        code_interne = '%05d' % code_interne
58
        family = get_client().get_family_by_code_interne(code_interne)
59
        if not family:
60
            raise ValueError('Le code interne est invalide')
61
        family.complete()
62
        self.extract_family(form, formdata, user, evo, family)
63
        family.save()
64
        return family
21 65

  
22
    def add_parameters_widgets(self, form, parameters, prefix='', formdef=None):
23
        pass
66
    def create(self, form, formdata, user, evo):
67
        family = abelium_domino_ws.Family(client=get_client())
68
        self.extract_family(form, formdata, user, evo, family)
69
        return family
24 70

  
25
    def perform(self, formdata):
26
        family_data = {}
27
        for k, v in formdata.get_as_dict().items():
28
            if k.startswith('var_'):
29
                family_data['domino_'+k] = v
30
        # XXX: store family data locally, this should be changed to send it
31
        # over to the abelium domino web service.
32
        get_request().user.family_data = family_data
33
        get_request().user.store()
71
    def extract_family(self, form, formdata, user, evo, family):
72
        formdef = formdata.formdef
73
        children = [abelium_domino_ws.Child() for i in range(5)]
74
        max_i = 0
75
        for field in formdef.fields:
76
            value = formdata.data.get(field.id)
77
            if value in (None, ''):
78
                continue
79
            if hasattr(field, 'date_in_the_past'):
80
                value = time.strftime('%Y%m%d', value)
81
            value = unicode(value, 'utf8')
82
            if field.prefill and \
83
               field.prefill.get('type') == 'formula':
84
                v = field.prefill.get('value', '').strip()
85
                i = None
86
                name = None
87
                m = re.search('domino_var_([^ ]*)_enfant([0-9]*)', v)
88
                m2 = re.search('domino_var_([^ ]*)', v)
89
                if m:
90
                    name, i = m.groups()
91
                    try:
92
                        i = int(i)
93
                    except ValueError:
94
                        continue
95
                    max_i = max(i, max_i)
96
                    print 'enfant', name, i-1, value
97
                    setattr(children[i-1], name, value)
98
                elif m2:
99
                    name = m2.group(1)
100
                    print 'family', name, value
101
                    setattr(family, name, value)
102
        for child1, child2 in zip(family.children, children):
103
            child1.__dict__.update(child2.__dict__)
104
        family.save()
105
        if max_i > len(family.children): # add new children
106
            for child in children[len(family.children):max_i]:
107
                family.add_child(child)
34 108

  
109
    def submit_form(self, form, formdata, user, evo):
110
        logger = get_logger()
111
        try:
112
            if form.get_submit() == 'update_button%s' % self.id:
113
                family = self.update(form, formdata, user, evo)
114
                msg = _('Sucessfully updated the family %s')
115
                log_msg = _('Sucessfully updated the family %s of %s')
116
            elif form.get_submit() == 'create_button%s' % self.id:
117
                family = self.create(form, formdata, user, evo)
118
                msg = _('Sucessfully created the family %s')
119
                log_msg = _('Sucessfully created the family %s of %s')
120
            else:
121
                raise NotImplemented
122
            code_interne = family.code_interne.encode('utf8')
123
            msg = msg % code_interne
124
            logger.info(_('Sucessfully created the family %s of %s'),
125
                    code_interne, formdata.get_user())
126
            form_user = formdata.get_user()
127
            form_user.abelium_domino_code_famille = code_interne
128
            form_user.store()
129
        except Exception, e:
130
            raise
131
            if form.get_submit() == 'update_button%s' % self.id:
132
                msg = _('Unable to update family: %s') % str(e)
133
            elif form.get_submit() == 'create_button%s' % self.id:
134
                msg = _('Unable to create family: %s') % str(e)
135
            evo.comment = msg
136
            logger.exception(msg  % formdata.get_user())
137
        else:
138
            evo.comment = msg
139
            wf_status = self.get_status()
140
            if wf_status:
141
                evo.status = 'wf-%s' % wf_status.id
142
        return False
35 143

  
36 144
register_item_class(AbeliumDominoRegisterFamilyWorkflowStatusItem)

Also available in: Unified diff