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.
"""
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)

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#">
<head>
<title>Hello</title>
<title>FreeIPA</title>
</head>
<body>
<div>Hello</div>
<p py:for="name in api.Command">
<a href="${name}" py:content="name"/>
</p>
</body>
</html>

View File

@ -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

View File

@ -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())