Project

General

Profile

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

root / extra / modules / abelium_domino_ui.ptl @ 5aa61d1d

1
from quixote import get_publisher, redirect
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 wcs.formdef import FormDef
9

    
10
# constants
11
ABELIUM_DOMINO = 'abelium_domino'
12
ACTIVATED = 'activated'
13
WSDL_URL = 'wsdl_url'
14
SERVICE_URL = 'service_url'
15
DOMAIN = 'domain'
16
LOGIN = 'login'
17
PASSWORD = 'password'
18
INVOICE_FORMDEF = 'invoice_formdef'
19
INVOICE_STATUS = 'invoice_status'
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
    cfg = get_abelium_cfg(publisher)
38
    return abelium_domino_ws.DominoWs(
39
            url=cfg.get(WSDL_URL, ''),
40
            domain=cfg.get(DOMAIN,''),
41
            login=cfg.get(LOGIN, ''),
42
            password=cfg.get(PASSWORD, ''),
43
            location=cfg.get(SERVICE_URL))
44

    
45
def get_invoice_formdef():
46
    cfg = get_abelium_cfg()
47
    formdef_id = cfg.get(INVOICE_FORMDEF)
48
    if not formdef_id:
49
        return None
50
    return FormDef.get(formdef_id, ignore_errors=True)
51

    
52
def get_invoice_formdef_and_status():
53
    cfg = get_abelium_cfg()
54
    formdef_id = cfg.get(INVOICE_FORMDEF)
55
    formdef_status = cfg.get(INVOICE_STATUS)
56
    if not formdef_id or not formdef_status:
57
        return None, None
58
    return FormDef.get(formdef_id, ignore_errors=True), formdef_status
59

    
60
class AbeliumDominoDirectory(Directory):
61
    _q_exports = [ '' , 'debug' ]
62
    label = N_('Domino')
63

    
64
    def debug [html] (self):
65
        html_top(ABELIUM_DOMINO)
66
        '<dl>'
67
        context = get_publisher().substitutions.get_context_variables()
68
        for var in sorted(context.keys()):
69
            value = context[var]
70
            if value:
71
                '<dt>%s</dt>' % var
72
                '<dd>%s</dt>' % unicode(value).encode(get_publisher().site_charset)
73
        '</dl>'
74

    
75

    
76
    def _q_index [html] (self):
77
        publisher = get_publisher()
78
        cfg = get_cfg(ABELIUM_DOMINO, {})
79
        form = self.form(cfg)
80

    
81
        title = _('Abelium Domino')
82
        html_top(ABELIUM_DOMINO, title = title)
83
        '<h2>%s</h2>' % title
84

    
85
        if form.is_submitted() and not form.has_errors():
86
            if form.get_widget('cancel').parse():
87
                return redirect('..')
88
            if form.get_widget('submit').parse():
89
                for name in [f[0] for f in self.form_desc] + [INVOICE_FORMDEF, INVOICE_STATUS]:
90
                    widget = form.get_widget(name)
91
                    if widget:
92
                        cfg[name] = widget.parse()
93
                publisher.cfg[ABELIUM_DOMINO] = cfg
94
                publisher.write_cfg()
95
                return redirect('.')
96

    
97
        if abelium_domino_ws:
98
            form.render()
99
        else:
100
            message = _('The Abelium Domino module is not '
101
                    'activated because of this error when '
102
                    'loading it: %r') % import_error
103
            '<p class="errornotice">%s</p>' % message
104

    
105
    form_desc = (
106
            # name, required, title, kind
107
            (ACTIVATED, False, _('Activated'), bool),
108
            (WSDL_URL, True, _('WSDL URL'), str),
109
            (SERVICE_URL, False, _('Service URL'), str),
110
            (DOMAIN, True, _('Domain'), str),
111
            (LOGIN, True, _('Login'), str),
112
            (PASSWORD, True, _('Password'), str),
113
    )
114

    
115

    
116
    def form(self, initial_value={}):
117
        form = Form(enctype='multipart/form-data')
118
        kinds = { str: StringWidget, bool: CheckboxWidget }
119
        for name, required, title, kind in self.form_desc:
120
            widget = kinds[kind]
121
            form.add(widget, name, required=required, title=title,
122
                    value=initial_value.get(name, ''))
123
        options = [(formdef.id, formdef.name) \
124
                        for formdef in FormDef.values()]
125
        options.insert(0, (None, _('None')))
126
        form.add(SingleSelectWidget, INVOICE_FORMDEF,
127
                title=_('Form to attache invoices'),
128
                value=initial_value.get(INVOICE_FORMDEF),
129
                options=options)
130
        formdef = get_invoice_formdef()
131
        if formdef:
132
            workflow = formdef.get_workflow()
133
            if workflow:
134
                options = [(status.id, status.name) \
135
                        for status in workflow.possible_status]
136
                options.insert(0, (None, _('None')))
137
                form.add(SingleSelectWidget, INVOICE_STATUS,
138
                        title=_('Status for newly created invoices'),
139
                        value=initial_value.get(INVOICE_STATUS),
140
                        options=options)
141
        form.add_submit('submit', _('Submit'))
142
        form.add_submit('cancel', _('Cancel'))
143

    
144
        return form
145

    
(3-3/30)