Project

General

Profile

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

root / extra / modules / abelium_domino_ui.ptl @ 84fd22a0

1
from quixote import get_publisher, redirect, get_request
2
from quixote.directory import Directory, AccessControlled
3

    
4
from qommon import get_cfg
5
from qommon.form import Form, StringWidget, CheckboxWidget, SingleSelectWidget
6
from qommon.admin.menu import html_top
7

    
8
from payments import Regie
9

    
10

    
11
# constants
12
ABELIUM_DOMINO = 'abelium_domino'
13
ACTIVATED = 'activated'
14
WSDL_URL = 'wsdl_url'
15
SERVICE_URL = 'service_url'
16
DOMAIN = 'domain'
17
LOGIN = 'login'
18
PASSWORD = 'password'
19
INVOICE_REGIE = 'invoice_regie'
20

    
21
try:
22
    import abelium_domino_ws
23
except ImportError, e:
24
    abelium_domino_ws = None
25
    import_error = e
26

    
27
def get_abelium_cfg(publisher):
28
    if not publisher:
29
        publisher = get_publisher()
30
    return publisher.cfg.get(ABELIUM_DOMINO, {})
31

    
32
def is_activated(publisher=None):
33
    cfg = get_abelium_cfg(publisher)
34
    return cfg.get(ACTIVATED, False) and abelium_domino_ws is not None
35

    
36
def get_client(publisher=None):
37
    publisher = publisher or get_publisher()
38

    
39
    cfg = get_abelium_cfg(publisher)
40
    publisher._ws_cache = abelium_domino_ws.DominoWs(
41
        url=cfg.get(WSDL_URL, ''),
42
        domain=cfg.get(DOMAIN,''),
43
        login=cfg.get(LOGIN, ''),
44
        password=cfg.get(PASSWORD, ''),
45
        location=cfg.get(SERVICE_URL))
46
    return publisher._ws_cache
47

    
48
def get_family(user, publisher=None):
49
    family = None
50
    if user is None:
51
        return None
52
    client = get_client(publisher)
53
    if hasattr(user, 'abelium_domino_code_famille'):
54
        family = client.get_family_by_code_interne(
55
            user.abelium_domino_code_famille)
56
    if family is None and user.email:
57
        family = client.get_family_by_mail(user.email)
58
    return family
59

    
60
def get_invoice_regie(publisher=None):
61
    cfg = get_abelium_cfg(publisher)
62
    regie_id = cfg.get(INVOICE_REGIE)
63
    if not regie_id:
64
        return None
65
    return Regie.get(regie_id, ignore_errors=True)
66

    
67
class AbeliumDominoDirectory(Directory):
68
    _q_exports = [ '' , 'debug' ]
69
    label = N_('Domino')
70

    
71
    def debug [html] (self):
72
        from abelium_domino_vars import SESSION_CACHE
73
        html_top(ABELIUM_DOMINO)
74
        '<p>code interne: %s</p>' % getattr(get_request().user, str('abelium_domino_code_famille'), None)
75
        '<dl>'
76
        context = get_publisher().substitutions.get_context_variables()
77
        for var in sorted(context.keys()):
78
            value = context[var]
79
            if value:
80
                '<dt>%s</dt>' % var
81
                '<dd>%s</dt>' % value
82
        '</dl>'
83
        delattr(get_request().session, SESSION_CACHE)
84

    
85
    def _q_index [html] (self):
86
        publisher = get_publisher()
87
        cfg = get_cfg(ABELIUM_DOMINO, {})
88
        form = self.form(cfg)
89

    
90
        title = _('Abelium Domino')
91
        html_top(ABELIUM_DOMINO, title = title)
92
        '<h2>%s</h2>' % title
93

    
94
        if form.is_submitted() and not form.has_errors():
95
            if form.get_widget('cancel').parse():
96
                return redirect('..')
97
            if form.get_widget('submit').parse():
98
                for name in [f[0] for f in self.form_desc] + [INVOICE_REGIE]:
99
                    widget = form.get_widget(name)
100
                    if widget:
101
                        cfg[name] = widget.parse()
102
                publisher.cfg[ABELIUM_DOMINO] = cfg
103
                publisher.write_cfg()
104
                return redirect('.')
105

    
106
        if abelium_domino_ws:
107
            form.render()
108
        else:
109
            message = _('The Abelium Domino module is not '
110
                    'activated because of this error when '
111
                    'loading it: %r') % import_error
112
            '<p class="errornotice">%s</p>' % message
113
        '<dl>'
114
        context = get_publisher().substitutions.get_context_variables()
115
        for var in sorted(context.keys()):
116
            value = context[var]
117
            if value:
118
                '<dt>%s</dt>' % var
119
                '<dd>%s</dt>' % unicode(value).encode(get_publisher().site_charset)
120
        '</dl>'
121

    
122
    form_desc = (
123
            # name, required, title, kind
124
            (ACTIVATED, False, _('Activated'), bool),
125
            (WSDL_URL, True, _('WSDL URL'), str),
126
            (SERVICE_URL, False, _('Service URL'), str),
127
            (DOMAIN, True, _('Domain'), str),
128
            (LOGIN, True, _('Login'), str),
129
            (PASSWORD, True, _('Password'), str),
130
    )
131

    
132

    
133
    def form(self, initial_value={}):
134
        form = Form(enctype='multipart/form-data')
135
        kinds = { str: StringWidget, bool: CheckboxWidget }
136
        for name, required, title, kind in self.form_desc:
137
            widget = kinds[kind]
138
            form.add(widget, name, required=required, title=title,
139
                    value=initial_value.get(name, ''))
140
        options = [(regie.id, regie.label) \
141
                        for regie in Regie.values()]
142
        options.insert(0, (None, _('None')))
143
        form.add(SingleSelectWidget, INVOICE_REGIE,
144
                title=_('Regie which will receive payments'),
145
                value=initial_value.get(INVOICE_REGIE),
146
                options=options)
147

    
148
        form.add_submit('submit', _('Submit'))
149
        form.add_submit('cancel', _('Cancel'))
150

    
151
        return form
(3-3/32)