Project

General

Profile

Download (5.68 KB) Statistics
| Branch: | Tag: | Revision:

root / extra / modules / abelium_domino_workflow.py @ 1fa4112b

1
import re
2
import time
3

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

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

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

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

    
20
class AbeliumDominoRegisterFamilyWorkflowStatusItem(WorkflowStatusJumpItem):
21
    status = None
22
    description = N_('Abelium Domino: Register a Family')
23
    key = 'abelium-domino-register-family'
24
    label = None
25

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

    
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
65

    
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
70

    
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)
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 %(code)s of %(user)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 %(code)s of %(user)s')
120
            else:
121
                raise NotImplemented
122
            code_interne = family.code_interne.encode('utf8')
123
            msg = msg % code_interne
124
            logger.info(log_msg, {'code': code_interne, 'user': formdata.get_user()})
125
            form_user = formdata.get_user()
126
            form_user.abelium_domino_code_famille = code_interne
127
            form_user.store()
128
        except Exception, e:
129
            raise
130
            if form.get_submit() == 'update_button%s' % self.id:
131
                msg = _('Unable to update family: %s') % str(e)
132
            elif form.get_submit() == 'create_button%s' % self.id:
133
                msg = _('Unable to create family: %s') % str(e)
134
            evo.comment = msg
135
            logger.exception(msg  % formdata.get_user())
136
        else:
137
            evo.comment = msg
138
            wf_status = self.get_status()
139
            if wf_status:
140
                evo.status = 'wf-%s' % wf_status.id
141
        return False
142

    
143
register_item_class(AbeliumDominoRegisterFamilyWorkflowStatusItem)
(5-5/32)