summaryrefslogtreecommitdiffstats
path: root/ipawebui/controller.py
blob: a2a270cbd4aae97afbbd12a0067221a3da9ac8ea (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
# Authors: Jason Gerard DeRose <jderose@redhat.com>
#
# Copyright (C) 2008  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

"""
Controller classes.
"""

import simplejson
from ipalib.plugable import ReadOnly, lock


class Controller(ReadOnly):
    exposed = True

    def __init__(self, template=None):
        self.template = template
        lock(self)

    def output_xhtml(self, **kw):
        return self.template.serialize(
            output='xhtml-strict',
            format='pretty',
            **kw
        )

    def output_json(self, **kw):
        return simplejson.dumps(kw, sort_keys=True, indent=4)

    def __call__(self, **kw):
        json = bool(kw.pop('_format', None) == 'json')
        result = self.run(**kw)
        assert type(result) is dict
        if json or self.template is None:
            return self.output_json(**result)
        return self.output_xhtml(**result)

    def run(self, **kw):
        return {}


class Command(Controller):
    def __init__(self, command, template=None):
        self.command = command
        super(Command, self).__init__(template)

    def run(self, **kw):
        return dict(command=self.command)


class Index(Controller):
    def __init__(self, api, template=None):
        self.api = api
        super(Index, self).__init__(template)

    def run(self):
        return dict(api=self.api)