Project

General

Profile

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

root / extra / modules / abelium_domino_vars.py @ 84fd22a0

1
from decimal import Decimal
2
import logging
3

    
4
from quixote.publish import get_publisher
5

    
6
from qommon.substitution import Substitutions
7
from publisher import WcsPublisher
8

    
9
from abelium_domino_ui import (is_activated, abelium_domino_ws, get_client, get_family)
10

    
11
SESSION_CACHE = 'abelium_domino_variable_cache'
12

    
13
class DominoVariables(object):
14
    VARIABLE_TEMPLATE = 'domino_var_%s'
15
    CHILD_VARIABLE_TEMPLATE = 'domino_var_%s_enfant%s'
16

    
17
    CHILD_COLUMNS = abelium_domino_ws.Child.COLUMNS
18
    FAMILY_COLUMNS = abelium_domino_ws.Family.COLUMNS \
19
        + abelium_domino_ws.Family.MORE_COLUMNS
20
    def __init__(self, publisher=None, request=None):
21
        self.publisher = publisher
22
        self.request = request
23

    
24
    def get_substitution_variables(self):
25
        vars = {}
26
        if not is_activated() or not self.request or not self.request.user \
27
                or not getattr(self.request.user, 'email'):
28
            return vars
29

    
30
        # test cache
31
        cache = getattr(self.request.session, SESSION_CACHE, None)
32
        if cache is not None:
33
            return cache
34
        # call the web service
35
        try:
36
            charset = get_publisher().site_charset
37
            family = get_family(self.request.user)
38
            if family:
39
                family.complete()
40
                for i, child in enumerate(family.children):
41
                    for remote_name, name, converter, desc in self.CHILD_COLUMNS:
42
                        v = getattr(child, name, None)
43
                        if v is None:
44
                            continue
45
                        if hasattr(v, 'encode'):
46
                            v = v.encode(charset)
47
                        vars[self.CHILD_VARIABLE_TEMPLATE % (name, i+1)] = v
48
                vars[self.VARIABLE_TEMPLATE % 'nombre_enfants'] = len(family.children)
49
                for remote_name, name, converted, desc in self.FAMILY_COLUMNS:
50
                    if hasattr(family, name):
51
                        v = getattr(family, name)
52
                        if v is None:
53
                            continue
54
                        if hasattr(v, 'encode'):
55
                            v = v.encode(charset)
56
                        vars[self.VARIABLE_TEMPLATE % name] = v
57
                amount = Decimal(0)
58
                for invoice in family.invoices:
59
                    amount += invoice.reste_du
60
                if amount:
61
                    vars['user_famille_reste_du'] = str(amount)
62
        except abelium_domino_ws.DominoException:
63
            logging.exception('unable to call the domino ws for user %s', self.request.user.id)
64
        setattr(self.request.session, SESSION_CACHE, vars)
65
        self.request.session.store()
66
        return vars
67

    
68
    def get_substitution_variables_list(cls):
69
        if not is_activated():
70
            return ()
71
        vars = []
72
        for remote_name, name, converted, desc in cls.FAMILY_COLUMNS:
73
            vars.append((_('Domino'), cls.VARIABLE_TEMPLATE % name, desc))
74
        for remote_name, name, converted, desc in cls.CHILD_COLUMNS:
75
            vars.append((_('Domino'), cls.CHILD_VARIABLE_TEMPLATE % (name, '{0,1,2,..}'), desc))
76
        return vars
77
    get_substitution_variables_list = classmethod(get_substitution_variables_list)
78

    
79
Substitutions.register_dynamic_source(DominoVariables)
80
WcsPublisher.register_extra_source(DominoVariables)
(4-4/32)