Projet

Général

Profil

Télécharger (3,3 ko) Statistiques
| Branche: | Tag: | Révision:

root / auquotidien / modules / abelium_domino_vars.py @ 8b02623d

1
from decimal import Decimal
2
import logging
3

    
4
from quixote.publish import get_publisher
5

    
6
from qommon import _
7
from qommon.substitution import Substitutions
8
from wcs.publisher import WcsPublisher
9

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

    
12
SESSION_CACHE = 'abelium_domino_variable_cache'
13

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

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

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

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

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

    
80
Substitutions.register_dynamic_source(DominoVariables)
81
WcsPublisher.register_extra_source(DominoVariables)
(4-4/27)