summaryrefslogtreecommitdiffstats
path: root/ipawebui/engine.py
blob: dc59b092386de7ac9296910fb31249a884c09841 (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
# 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

"""
Engine to map ipalib plugins to wehjit widgets.
"""

from controllers import Command
from ipalib import crud

class ParamMapper(object):
    def __init__(self, api, app):
        self._api = api
        self._app = app
        self.__methods = dict()
        for name in dir(self):
            if name.startswith('_'):
                continue
            attr = getattr(self, name)
            if not callable(attr):
                continue
            self.__methods[name] = attr

    def __call__(self, param, cmd):
        key = param.__class__.__name__
        if key in self.__methods:
            method = self.__methods[key]
        else:
            method = self.Str
        return method(param, cmd)

    def Str(self, param, cmd):
        return self._app.new('TextRow',
            label=param.label,
            name=param.name,
            required=param.required,
            value=param.default,
        )

    def Password(self, param, cmd):
        return self._app.new('PasswordRow',
            name=param.name,
            required=param.required,
        )

    def Flag(self, param, cmd):
        return self._app.new('SelectRow',
            name=param.name,
            label=param.label,
        )


def filter_params(namespace):
    for param in namespace():
        if param.exclude and 'webui' in param.exclude:
            continue
        yield param


class Engine(object):

    cruds = frozenset(['add', 'show', 'mod', 'del', 'find'])

    def __init__(self, api, app):
        self.api = api
        self.app = app
        self.param_mapper = ParamMapper(api, app)
        self.pages = dict()
        self.jsonurl = self.api.Backend.jsonserver.url.rstrip('/')
        self.info_pages = []

    def add_object_menuitems(self, menu, name):
        obj = self.api.Object[name]
        for cmd in obj.methods():
            p = self.pages[cmd.name]
            menu.add(
                menu.new('MenuItem',
                    label=p.title,
                    href=p.url,
                )
            )

    def build(self):
        for obj in self.api.Object():
            if self.cruds.issubset(obj.methods) and obj.primary_key is not None:
                self.pages[obj.name] = self.build_cruds_page(obj)

        # Add landing page:
        landing = self.app.new('PageApp', id='', title='Welcome to FreeIPA')

        for page in self.pages.values() + [landing]:
            page.menu.label = 'FreeIPA'
            for name in sorted(self.pages):
                p = self.pages[name]
                page.menu.new_child('MenuItem', label=p.title, href=p.url)




        # Add in the info pages:
        page = self.app.new('PageApp', id='api', title='api')
        page.view.add(
            self.app.new('API', api=self.api)
        )
        self.info_pages.append(page)

        for kind in self.api:
            self.build_info_page(kind)
        for page in self.info_pages:
            for p in self.info_pages:
                page.menuset.add(
                    self.app.new('MenuItem',
                        href=p.url,
                        label=p.title,
                    )
                )

    def build_cruds_page(self, obj):
        page = self.app.new('PageGrid', title=obj.label, id=obj.name)

        # Setup CRUDS widget:
        page.cruds.autoload = True
        page.cruds.jsonrpc_url = self.api.Backend.jsonserver.url
        page.cruds.key = obj.primary_key.name
        page.cruds.method_create = obj.methods['add'].name
        page.cruds.method_retrieve = obj.methods['show'].name
        page.cruds.method_update = obj.methods['mod'].name
        page.cruds.method_delete = obj.methods['del'].name
        page.cruds.method_search = obj.methods['find'].name
        page.cruds.display_cols = tuple(
            dict(
                name=p.name,
                label=p.label,
                css_classes=None,
            )
            for p in obj.params()
        )

        # Setup the Grid widget:
        page.grid.cols = tuple(
            dict(
                name=p.name,
                label=p.label,
                css_classes=None,
            )
            for p in obj.params() if p.required
        )


        # Setup the create Dialog:
        cmd = obj.methods['add']
        page.create.title = cmd.summary.rstrip('.')
        for p in filter_params(cmd.params):
            page.create.fieldtable.add(self.param_mapper(p, cmd))

        # Setup the retrieve Dialog
        page.retrieve.title = 'Showing "{value}"'

        # Setup the update Dialog:
        page.update.title = 'Updating "{value}"'
        cmd = obj.methods['mod']
        for p in filter_params(cmd.options):
            page.update.fieldtable.add(self.param_mapper(p, cmd))

        # Setup the delete Dialog
        page.delete.title = 'Delete "{value}"?'

        return page

    def build_info_page(self, kind):
        # Add in the Object page:
        plugins = tuple(self.api[kind]())
        page = self.app.new('PageApp', id=kind, title=kind)
        info = self.app.new('IPAPlugins', kind=kind, plugins=plugins)
        quick_jump = self.app.new('QuickJump',
            options=tuple((p.name, p.name) for p in plugins)
        )
        page.view.add(info)
        page.actions.add(quick_jump)
        self.info_pages.append(page)
        if kind in self.app.widgets:
            info.add(
                self.app.new(kind)
            )
        return page