summaryrefslogtreecommitdiffstats
path: root/ipawebui/widgets.py
blob: 1cf1adb97f3a57e2db2c1f1d7a57d15da136cd20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# Authors: Jason Gerard DeRose <jderose@redhat.com>
#
# Copyright (C) 2009  Red Hat
# see file 'COPYING' for use and warranty information
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; version 2 only
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

"""
Custom IPA widgets.
"""

from textwrap import dedent
from wehjit import Collection, base, freeze, builtins
from wehjit.util import Alternator
from wehjit import Static, Dynamic, StaticProp, DynamicProp
from ipaserver.rpcserver import extract_query


class IPAPlugins(base.Container):
    plugins = Static('plugins', default=tuple())
    kind = Static('kind')

    @DynamicProp
    def row(self):
        return Alternator(['odd', 'even'])

    xml = """
    <div
        xmlns:py="http://genshi.edgewall.org/"
        class="${css_classes}"
        id="${id}"
    >
    <p py:content="'%d %s plugins' % (len(plugins), kind)" />

    <div py:for="p in plugins">
    <h2 id="${p.name}"><a href="#${p.name}" py:content="p.name" /></h2>

    <table class="${row.reset()}">

    <tr class="${row.next()}">
    <td>module</td>
    <td>
    <a
        title="Link to module documentation"
        href="http://freeipa.org/developer-docs/${p.module}-module.html"
        py:content="p.module"
    />
    </td>
    </tr>

    <tr class="${row.next()}">
    <td>base(s)</td>
    <td py:content="', '.join(p.bases)" />
    </tr>

    <tr py:if="p.doc" class="${row.next()}">
    <td>docstring</td>
    <td><pre py:content="p.doc" /></td>
    </tr>

    <tr
        py:for="child in children"
        py:replace="child.generate(plugin=p, row=row)"
    />

    </table>
    </div>

    </div>
    """

    style_global = (
        ('tr.odd', (
            ('background-color', '#ddd'),
        )),
        ('tr.even', (
            ('background-color', '#eee'),
        )),

        ('td', (
            ('vertical-align', 'top'),
            ('padding', '0.25em 0.5em'),
        )),
    )

    style = (
        ('', (
            ('font-size', '%(font_size_mono)s'),
            ('font-family', 'monospace'),
        )),

        ('table', (
            ('width', '100%%'),
        )),

        ('pre', (
            ('margin', '0'),
        )),

        ('th', (
            ('color', '#0a0'),
        )),

        ('h2', (
            ('font-family', 'monospace'),
            ('font-weight', 'normal'),
            ('margin-top', '1.5em'),
            ('margin-bottom', '0'),
        )),

        ('h2 a', (
            ('text-decoration', 'none'),
            ('color', 'inherit'),
        )),

        ('h2 a:hover', (
            ('background-color', '#eee'),
        )),

        ('h2:target', (
            ('color', '#e02'),
        )),
    )


class API(base.Widget):
    api = Static('api')

    @DynamicProp
    def row(self):
        return Alternator(['odd', 'even'])

    xml = """
    <div
        xmlns:py="http://genshi.edgewall.org/"
        class="${css_classes}"
        id="${id}"
    >
    <p py:content="'%d namespaces in API' % len(api)" />
    <table>
    <tr py:for="key in api" class="${row.next()}">
    <td>
    <a href="${key}" py:content="'api.' + key" />
    </td>
    <td py:content="repr(api[key])" />
    </tr>
    </table>
    </div>
    """


class Command(base.Widget):
    xml = """
    <table
        xmlns:py="http://genshi.edgewall.org/"
        py:strip="True"
    >

    <tr py:if="plugin.obj" class="${row.next()}">
    <td>Object</td>
    <td>
    <a href="Object#${plugin.obj.name}" py:content="plugin.obj.fullname" />
    </td>
    </tr>

    <tr py:if="plugin.args" class="${row.next()}">
    <th colspan="2" py:content="'args (%d)' % len(plugin.args)" />
    </tr>
    <tr py:for="arg in plugin.args()" class="${row.next()}">
    <td py:content="arg.name"/>
    <td py:content="repr(arg)" />
    </tr>

    <tr py:if="plugin.options" class="${row.next()}">
    <th colspan="2" py:content="'options (%d)' % len(plugin.options)" />
    </tr>
    <tr py:for="option in plugin.options()" class="${row.next()}">
    <td py:content="option.name"/>
    <td py:content="repr(option)" />
    </tr>

    <tr py:if="plugin.output" class="${row.next()}">
    <th colspan="2" py:content="'output (%d)' % len(plugin.output)" />
    </tr>
    <tr py:for="param in plugin.output()" class="${row.next()}">
    <td py:content="param.name"/>
    <td py:content="repr(param)" />
    </tr>

    </table>
    """


class Object(base.Widget):
    xml = """
    <table
        xmlns:py="http://genshi.edgewall.org/"
        py:strip="True"
    >
    <tr py:if="plugin.methods" class="${row.next()}">
    <th colspan="2" py:content="'methods (%d)' % len(plugin.methods)" />
    </tr>
    <tr py:for="method in plugin.methods()" class="${row.next()}">
    <td><a href="${'Command#' + method.name}" py:content="method.name"/></td>
    <td py:content="method.summary" />
    </tr>

    <tr py:if="plugin.params" class="${row.next()}">
    <th colspan="2" py:content="'params (%d)' % len(plugin.params)" />
    </tr>
    <tr py:for="param in plugin.params()" class="${row.next()}">
    <td>${"param.name"}:</td>
    <td py:content="repr(param)" />
    </tr>

    </table>
    """



class Conditional(base.Container):

    mode = Static('mode', default='input')

    @DynamicProp
    def page_mode(self):
        if self.page is None:
            return
        return self.page.mode

    xml = """
    <div
        xmlns:py="http://genshi.edgewall.org/"
        py:if="mode == page_mode"
        py:strip="True"
    >
    <child py:for="child in children" py:replace="child.generate()" />
    </div>
    """


class Output(base.Widget):
    """
    Shows attributes form an LDAP entry.
    """

    order = Dynamic('order')
    labels = Dynamic('labels')
    result = Dynamic('result')

    xml = """
    <div
        xmlns:py="http://genshi.edgewall.org/"
        class="${klass}"
        id="${id}"
    >
    <table py:if="isinstance(result, dict)">
    <tr py:for="key in order" py:if="key in result">
    <th py:content="labels[key]" />
    <td py:content="result[key]" />
    </tr>
    </table>

    <table
        py:if="isinstance(result, (list, tuple)) and len(result) > 0"
    >
    <tr>
    <th
        py:for="key in order"
        py:if="key in result[0]"
        py:content="labels[key]"
    />
    </tr>
    <tr py:for="entry in result">
    <td
        py:for="key in order"
        py:if="key in result[0]"
        py:content="entry[key]"
    />
    </tr>
    </table>
    </div>
    """

    style = (
        ('table', (
            ('empty-cells', 'show'),
            ('border-collapse', 'collapse'),
        )),

        ('th', (
            ('text-align', 'right'),
            ('padding', '.25em 0.5em'),
            ('line-height', '%(height_bar)s'),
            ('vertical-align', 'top'),
        )),

        ('td', (
            ('padding', '.25em'),
            ('vertical-align', 'top'),
            ('text-align', 'left'),
            ('line-height', '%(height_bar)s'),
        )),
    )


class Hidden(base.Field):
    xml = """
    <input
        xmlns:py="http://genshi.edgewall.org/"
        type="hidden"
        name="${name}"
    />
    """


class Notification(base.Widget):
    message = Dynamic('message')
    error = Dynamic('error', default=False)

    @property
    def extra_css_classes(self):
        if self.error:
            yield 'error'
        else:
            yield 'okay'

    xml = """
    <p
        xmlns:py="http://genshi.edgewall.org/"
        class="${klass}"
        id="${id}"
        py:if="message"
        py:content="message"
    />
    """

    style = (
        ('', (
            ('font-weight', 'bold'),
            ('-moz-border-radius', '100%%'),
            ('background-color', '#eee'),
            ('border', '2px solid #966'),
            ('padding', '0.5em'),
            ('text-align', 'center'),
        )),
    )


class PageCmd(builtins.PageApp):
    cmd = Static('cmd')
    mode = Dynamic('mode', default='input')

    def controller(self, environ):
        query = extract_query(environ)
        self.mode = query.pop('__mode__', 'input')
        if self.mode == 'input':
            return
        soft = self.cmd.soft_validate(query)
        errors = soft['errors']
        values = soft['values']
        if errors:
            self.mode = 'input'
            for key in self.form:
                if key in errors:
                    self.form[key].error = errors[key]
                if key in values:
                    self.form[key].value = values[key]
            return
        output = self.cmd(**query)
        if isinstance(output, dict) and 'summary' in output:
            self.notification.message = output['summary']
        params = self.cmd.output_params
        if params:
            order = list(params)
            labels = dict((p.name, p.label) for p in params())
        else:
            order = sorted(entry)
            labels = dict((k, k) for k in order)
        self.show.order = order
        self.show.labels = labels
        self.show.result = output.get('result')


def create_widgets():
    widgets = Collection('freeIPA')
    widgets.register_builtins()

    widgets.register(API)
    widgets.register(IPAPlugins)
    widgets.register(Command)
    widgets.register(Object)
    widgets.register(Conditional)
    widgets.register(Output)
    widgets.register(Hidden)
    widgets.register(Notification)

    widgets.register(PageCmd)


    freeze(widgets)
    return widgets