diff --git a/ipa_webui/controller.py b/ipa_webui/controller.py index f0d627ece..a2a270cbd 100644 --- a/ipa_webui/controller.py +++ b/ipa_webui/controller.py @@ -20,22 +20,52 @@ Controller classes. """ -from ipalib.plugable import ReadOnly +import simplejson +from ipalib.plugable import ReadOnly, lock class Controller(ReadOnly): - def __init__(self, cmd, template): - self.cmd = cmd - self.template = template + exposed = True - def serialize(self, **kw): + def __init__(self, template=None): + self.template = template + lock(self) + + def output_xhtml(self, **kw): return self.template.serialize( output='xhtml-strict', - format='pretty+nice', + format='pretty', **kw ) + def output_json(self, **kw): + return simplejson.dumps(kw, sort_keys=True, indent=4) + def __call__(self, **kw): - return self.serialize( - result=self.cmd(**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) diff --git a/ipa_webui/templates/form.kid b/ipa_webui/templates/form.kid new file mode 100644 index 000000000..640157005 --- /dev/null +++ b/ipa_webui/templates/form.kid @@ -0,0 +1,16 @@ + + + + + Hello + + + + + + +
+
+ + + diff --git a/ipa_webui/templates/main.kid b/ipa_webui/templates/main.kid index 2eee036cf..692f2b575 100644 --- a/ipa_webui/templates/main.kid +++ b/ipa_webui/templates/main.kid @@ -2,11 +2,13 @@ - Hello + FreeIPA -
Hello
+

+ +

diff --git a/ipa_webui/tests/test_controllers.py b/ipa_webui/tests/test_controllers.py index 71e5c0e21..f5944dd9e 100644 --- a/ipa_webui/tests/test_controllers.py +++ b/ipa_webui/tests/test_controllers.py @@ -23,6 +23,7 @@ Test the `ipa_webui.controller` module. from ipa_webui import controller + class test_Controller(object): """ Test the `controller.Controller` class. @@ -32,15 +33,15 @@ class test_Controller(object): """ Test the `ipa_webui.controller.Controller.__init__()` method. """ - cmd = 'The command.' + o = controller.Controller() + assert o.template is None template = 'The template.' - o = controller.Controller(cmd, template) - assert o.cmd is cmd + o = controller.Controller(template) assert o.template is template - def test_serialize(self): + def test_output_xhtml(self): """ - Test the `ipa_webui.controller.Controller.serialize` method. + Test the `ipa_webui.controller.Controller.output_xhtml` method. """ class Template(object): def __init__(self): @@ -52,20 +53,18 @@ class test_Controller(object): self.kw = kw return dict(kw) - d = dict(output='xhtml-strict', format='pretty+nice') + d = dict(output='xhtml-strict', format='pretty') t = Template() - o = controller.Controller(None, t) - assert o.serialize() == d + o = controller.Controller(t) + assert o.output_xhtml() == d assert t.calls == 1 - def test_call(self): + def test_output_json(self): """ - Test the `ipa_webui.controller.Controller.__call__` method. + Test the `ipa_webui.controller.Controller.output_json` method. """ - class Template(object): - def serialize(self, **kw): - return 'Your login is %s.' % kw['result'] - def cmd(**kw): - return kw['first'][0] + kw['last'] - o = controller.Controller(cmd, Template()) - assert o(first='John', last='Doe') == 'Your login is JDoe.' + o = controller.Controller() + assert o.output_json() == '{}' + e = '{\n "age": 27, \n "first": "John", \n "last": "Doe"\n}' + j = o.output_json(last='Doe', first='John', age=27) + assert j == e diff --git a/webui-cherry.py b/webui-cherry.py index f85a791af..985a838b0 100755 --- a/webui-cherry.py +++ b/webui-cherry.py @@ -19,18 +19,29 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA """ -A lightweight test server running using cherrypy. +A web-UI test server using cherrypy. """ from cherrypy import expose, config, quickstart -from ipa_webui.templates import main +from ipa_webui.templates import form, main +from ipa_webui import controller +from ipalib import api +from ipalib import load_plugins + + +api.finalize() + class root(object): - @expose - def index(self): - return main.serialize( - output='xhtml-strict', - format='pretty+nice', - ) + index = controller.Index(api, main) + + def __init__(self): + for cmd in api.Command(): + ctr = controller.Command(cmd, form) + setattr(self, cmd.name, ctr) + + + + quickstart(root())