Project

General

Profile

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

root / extra / modules / abelium_domino_vars.py @ 5aa61d1d

1
from decimal import Decimal
2

    
3
from qommon.substitution import Substitutions
4
from publisher import WcsPublisher
5

    
6
from abelium_domino_ui import (is_activated, abelium_domino_ws, get_client)
7

    
8
SESSION_CACHE = 'abelium_domino_variable_cache'
9

    
10
def get_famille_for_user(domino_client, user):
11
    # TODO
12
    return None
13

    
14
class DominoVariables(object):
15
    VARIABLE_TEMPLATE = 'user_famille_%s'
16

    
17
    def __init__(self, publisher=None, request=None):
18
        self.publisher = publisher
19
        self.request = request
20

    
21
    def get_substitution_variables(self):
22
        print 'in get_substitution_variables'
23
        if not is_activated() or not self.request or not self.request.user \
24
                or not self.request.user.email:
25
            return {}
26

    
27
        # test cache
28
        cache = getattr(self.request.session, SESSION_CACHE, None)
29
        if cache:
30
            print 'cache', cache
31
            return cache
32
        # call the web service
33
        client = get_client()
34
        family = client.get_family_by_mail(self.request.user.email)
35
        family.complete()
36
        columns = abelium_domino_ws.Family.MORE_COLUMNS
37
        vars = {}
38
        if family:
39
            for remote_name, name, converted, desc in columns:
40
                if hasattr(family, name):
41
                    v = getattr(family, name)
42
                    vars[self.VARIABLE_TEMPLATE % name] = v
43
            amount = Decimal(0)
44
            for invoice in family.invoices:
45
                amount += invoice.reste_du
46
            if amount:
47
                vars['user_famille_reste_du'] = str(amount)
48

    
49
        setattr(self.request.session, SESSION_CACHE, vars)
50
        self.request.session.store()
51
        return vars
52

    
53

    
54
    def get_substitution_variables_list(cls):
55
        if not is_activated():
56
            return ()
57
        columns = abelium_domino_ws.Family.MORE_COLUMNS
58
        vars = []
59
        for remote_name, name, converted, desc in columns:
60
            vars.append((_('Domino'), cls.VARIABLE_TEMPLATE % name, desc))
61
        return vars
62
    get_substitution_variables_list = classmethod(get_substitution_variables_list)
63

    
64
Substitutions.register_dynamic_source(DominoVariables)
65
WcsPublisher.register_extra_source(DominoVariables)
(4-4/30)