1
|
from quixote import get_publisher, redirect, get_request
|
2
|
from quixote.directory import Directory, AccessControlled
|
3
|
from quixote.html import TemplateIO, htmltext
|
4
|
|
5
|
from qommon import _
|
6
|
from qommon import get_cfg, get_logger
|
7
|
from qommon.form import Form, StringWidget, CheckboxWidget, SingleSelectWidget
|
8
|
from qommon.backoffice.menu import html_top
|
9
|
from quixote.html import htmltext
|
10
|
|
11
|
from payments import Regie
|
12
|
|
13
|
|
14
|
# constants
|
15
|
ABELIUM_DOMINO = 'abelium_domino'
|
16
|
ACTIVATED = 'activated'
|
17
|
WSDL_URL = 'wsdl_url'
|
18
|
SERVICE_URL = 'service_url'
|
19
|
DOMAIN = 'domain'
|
20
|
LOGIN = 'login'
|
21
|
PASSWORD = 'password'
|
22
|
INVOICE_REGIE = 'invoice_regie'
|
23
|
|
24
|
try:
|
25
|
import abelium_domino_ws
|
26
|
except ImportError, e:
|
27
|
abelium_domino_ws = None
|
28
|
import_error = e
|
29
|
|
30
|
def get_abelium_cfg(publisher):
|
31
|
if not publisher:
|
32
|
publisher = get_publisher()
|
33
|
return publisher.cfg.get(ABELIUM_DOMINO, {})
|
34
|
|
35
|
def is_activated(publisher=None):
|
36
|
cfg = get_abelium_cfg(publisher)
|
37
|
return cfg.get(ACTIVATED, False) and abelium_domino_ws is not None
|
38
|
|
39
|
def get_client(publisher=None):
|
40
|
publisher = publisher or get_publisher()
|
41
|
|
42
|
cfg = get_abelium_cfg(publisher)
|
43
|
try:
|
44
|
publisher._ws_cache = abelium_domino_ws.DominoWs(
|
45
|
url=cfg.get(WSDL_URL, ''),
|
46
|
domain=cfg.get(DOMAIN,''),
|
47
|
login=cfg.get(LOGIN, ''),
|
48
|
password=cfg.get(PASSWORD, ''),
|
49
|
location=cfg.get(SERVICE_URL),
|
50
|
logger=get_logger())
|
51
|
except IOError:
|
52
|
return None
|
53
|
return publisher._ws_cache
|
54
|
|
55
|
def get_family(user, publisher=None):
|
56
|
family = None
|
57
|
if user is None:
|
58
|
return None
|
59
|
client = get_client(publisher)
|
60
|
if not client:
|
61
|
return None
|
62
|
if hasattr(user, 'abelium_domino_code_famille'):
|
63
|
family = client.get_family_by_code_interne(
|
64
|
user.abelium_domino_code_famille)
|
65
|
if family is None and user.email:
|
66
|
family = client.get_family_by_mail(user.email)
|
67
|
return family
|
68
|
|
69
|
def get_invoice_regie(publisher=None):
|
70
|
cfg = get_abelium_cfg(publisher)
|
71
|
regie_id = cfg.get(INVOICE_REGIE)
|
72
|
if not regie_id:
|
73
|
return None
|
74
|
return Regie.get(regie_id, ignore_errors=True)
|
75
|
|
76
|
class AbeliumDominoDirectory(Directory):
|
77
|
_q_exports = [ '' , 'debug' ]
|
78
|
label = N_('Domino')
|
79
|
|
80
|
def debug(self):
|
81
|
from abelium_domino_vars import SESSION_CACHE
|
82
|
html_top(ABELIUM_DOMINO)
|
83
|
r = TemplateIO(html=True)
|
84
|
r += htmltext('<form method="post"><button>Lancer le cron</button></form>')
|
85
|
if get_request().get_method() == 'POST':
|
86
|
try:
|
87
|
from abelium_domino_synchro import synchronize_domino
|
88
|
synchronize_domino(get_publisher())
|
89
|
except Exception, e:
|
90
|
r += htmltext('<pre>%s</pre>') % repr(e)
|
91
|
r += htmltext('<p>code interne: %s</p>') % getattr(get_request().user, str('abelium_domino_code_famille'), None)
|
92
|
r += htmltext('<dl>')
|
93
|
context = get_publisher().substitutions.get_context_variables()
|
94
|
for var in sorted(context.keys()):
|
95
|
value = context[var]
|
96
|
if value:
|
97
|
r += htmltext('<dt>%s</dt>') % var
|
98
|
r += htmltext('<dd>%s</dt>') % value
|
99
|
r += htmltext('</dl>')
|
100
|
delattr(get_request().session, SESSION_CACHE)
|
101
|
|
102
|
def _q_index(self):
|
103
|
publisher = get_publisher()
|
104
|
cfg = get_cfg(ABELIUM_DOMINO, {})
|
105
|
form = self.form(cfg)
|
106
|
|
107
|
title = _('Abelium Domino')
|
108
|
html_top(ABELIUM_DOMINO, title = title)
|
109
|
r = TemplateIO(html=True)
|
110
|
r += htmltext('<h2>%s</h2>') % title
|
111
|
|
112
|
if form.is_submitted() and not form.has_errors():
|
113
|
if form.get_widget('cancel').parse():
|
114
|
return redirect('..')
|
115
|
if form.get_widget('submit').parse():
|
116
|
for name in [f[0] for f in self.form_desc] + [INVOICE_REGIE]:
|
117
|
widget = form.get_widget(name)
|
118
|
if widget:
|
119
|
cfg[name] = widget.parse()
|
120
|
publisher.cfg[ABELIUM_DOMINO] = cfg
|
121
|
publisher.write_cfg()
|
122
|
return redirect('.')
|
123
|
|
124
|
if abelium_domino_ws:
|
125
|
r += form.render()
|
126
|
else:
|
127
|
message = _('The Abelium Domino module is not '
|
128
|
'activated because of this error when '
|
129
|
'loading it: %r') % import_error
|
130
|
r += htmltext('<p class="errornotice">%s</p>') % message
|
131
|
r += htmltext('<dl style="display: none">')
|
132
|
context = get_publisher().substitutions.get_context_variables()
|
133
|
for var in sorted(context.keys()):
|
134
|
value = context[var]
|
135
|
if value:
|
136
|
r += htmltext('<dt>%s</dt>') % var
|
137
|
r += htmltext('<dd>%s</dt>') % value
|
138
|
r += htmltext('</dl>')
|
139
|
return r.getvalue()
|
140
|
|
141
|
form_desc = (
|
142
|
# name, required, title, kind
|
143
|
(ACTIVATED, False, _('Activated'), bool),
|
144
|
(WSDL_URL, True, _('WSDL URL'), str),
|
145
|
(SERVICE_URL, False, _('Service URL'), str),
|
146
|
(DOMAIN, True, _('Domain'), str),
|
147
|
(LOGIN, True, _('Login'), str),
|
148
|
(PASSWORD, True, _('Password'), str),
|
149
|
)
|
150
|
|
151
|
|
152
|
def form(self, initial_value={}):
|
153
|
form = Form(enctype='multipart/form-data')
|
154
|
kinds = { str: StringWidget, bool: CheckboxWidget }
|
155
|
for name, required, title, kind in self.form_desc:
|
156
|
widget = kinds[kind]
|
157
|
form.add(widget, name, required=required, title=title,
|
158
|
value=initial_value.get(name, ''))
|
159
|
options = [(regie.id, regie.label) \
|
160
|
for regie in Regie.values()]
|
161
|
options.insert(0, (None, _('None')))
|
162
|
form.add(SingleSelectWidget, INVOICE_REGIE,
|
163
|
title=_('Regie which will receive payments'),
|
164
|
value=initial_value.get(INVOICE_REGIE),
|
165
|
options=options)
|
166
|
|
167
|
form.add_submit('submit', _('Submit'))
|
168
|
form.add_submit('cancel', _('Cancel'))
|
169
|
|
170
|
return form
|