More work on webui skeleton

This commit is contained in:
Jason Gerard DeRose 2008-10-06 18:25:57 -06:00
parent d38dcb6d39
commit 3082a5379a
5 changed files with 94 additions and 36 deletions

View File

@ -20,22 +20,52 @@
Controller classes. Controller classes.
""" """
from ipalib.plugable import ReadOnly import simplejson
from ipalib.plugable import ReadOnly, lock
class Controller(ReadOnly): class Controller(ReadOnly):
def __init__(self, cmd, template): exposed = True
self.cmd = cmd
self.template = template
def serialize(self, **kw): def __init__(self, template=None):
self.template = template
lock(self)
def output_xhtml(self, **kw):
return self.template.serialize( return self.template.serialize(
output='xhtml-strict', output='xhtml-strict',
format='pretty+nice', format='pretty',
**kw **kw
) )
def output_json(self, **kw):
return simplejson.dumps(kw, sort_keys=True, indent=4)
def __call__(self, **kw): def __call__(self, **kw):
return self.serialize( json = bool(kw.pop('_format', None) == 'json')
result=self.cmd(**kw), 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)

View File

@ -0,0 +1,16 @@
<?xml version='1.0' encoding='utf-8'?>
<html xmlns:py="http://purl.org/kid/ns#">
<head>
<title>Hello</title>
</head>
<body>
<table>
<tr py:for="param in command.params()">
<td py:content="param.name"/>
</tr>
</table>
</body>
</html>

View File

@ -2,11 +2,13 @@
<html xmlns:py="http://purl.org/kid/ns#"> <html xmlns:py="http://purl.org/kid/ns#">
<head> <head>
<title>Hello</title> <title>FreeIPA</title>
</head> </head>
<body> <body>
<div>Hello</div> <p py:for="name in api.Command">
<a href="${name}" py:content="name"/>
</p>
</body> </body>
</html> </html>

View File

@ -23,6 +23,7 @@ Test the `ipa_webui.controller` module.
from ipa_webui import controller from ipa_webui import controller
class test_Controller(object): class test_Controller(object):
""" """
Test the `controller.Controller` class. Test the `controller.Controller` class.
@ -32,15 +33,15 @@ class test_Controller(object):
""" """
Test the `ipa_webui.controller.Controller.__init__()` method. Test the `ipa_webui.controller.Controller.__init__()` method.
""" """
cmd = 'The command.' o = controller.Controller()
assert o.template is None
template = 'The template.' template = 'The template.'
o = controller.Controller(cmd, template) o = controller.Controller(template)
assert o.cmd is cmd
assert o.template is 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): class Template(object):
def __init__(self): def __init__(self):
@ -52,20 +53,18 @@ class test_Controller(object):
self.kw = kw self.kw = kw
return dict(kw) return dict(kw)
d = dict(output='xhtml-strict', format='pretty+nice') d = dict(output='xhtml-strict', format='pretty')
t = Template() t = Template()
o = controller.Controller(None, t) o = controller.Controller(t)
assert o.serialize() == d assert o.output_xhtml() == d
assert t.calls == 1 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): o = controller.Controller()
def serialize(self, **kw): assert o.output_json() == '{}'
return 'Your login is %s.' % kw['result'] e = '{\n "age": 27, \n "first": "John", \n "last": "Doe"\n}'
def cmd(**kw): j = o.output_json(last='Doe', first='John', age=27)
return kw['first'][0] + kw['last'] assert j == e
o = controller.Controller(cmd, Template())
assert o(first='John', last='Doe') == 'Your login is JDoe.'

View File

@ -19,18 +19,29 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # 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 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): class root(object):
@expose index = controller.Index(api, main)
def index(self):
return main.serialize( def __init__(self):
output='xhtml-strict', for cmd in api.Command():
format='pretty+nice', ctr = controller.Command(cmd, form)
) setattr(self, cmd.name, ctr)
quickstart(root()) quickstart(root())