Start work on HTML UI
parent
0c5237c1a9
commit
4f5aa00041
|
@ -1,10 +1,17 @@
|
||||||
from . import model
|
from . import model
|
||||||
|
import jinja2
|
||||||
import json
|
import json
|
||||||
import milla
|
import milla
|
||||||
|
|
||||||
|
|
||||||
|
class XHTMLResponse(milla.Response):
|
||||||
|
default_content_type = 'application/xhtml+xml'
|
||||||
|
|
||||||
|
|
||||||
class BaseController(milla.controllers.Controller):
|
class BaseController(milla.controllers.Controller):
|
||||||
|
|
||||||
|
TMPL_LOADER = jinja2.PackageLoader(__name__.rsplit('.', 1)[0])
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
# allowed_methods must be set on the instance rather than the
|
# allowed_methods must be set on the instance rather than the
|
||||||
# class because of how Milla does attribute copying to the
|
# class because of how Milla does attribute copying to the
|
||||||
|
@ -18,6 +25,28 @@ class BaseController(milla.controllers.Controller):
|
||||||
super(BaseController, self).__before__(request)
|
super(BaseController, self).__before__(request)
|
||||||
self.session = model.Session()
|
self.session = model.Session()
|
||||||
|
|
||||||
|
offers = [
|
||||||
|
'application/json',
|
||||||
|
'application/xhtml+xml',
|
||||||
|
'text/html',
|
||||||
|
]
|
||||||
|
default_format = request.ResponseClass.default_content_type
|
||||||
|
want_fmt = request.accept.best_match(offers, default_format)
|
||||||
|
if want_fmt == 'application/json':
|
||||||
|
request.want = 'json'
|
||||||
|
elif want_fmt == 'text/html':
|
||||||
|
request.want = 'html'
|
||||||
|
else:
|
||||||
|
request.ResponseClass = XHTMLResponse
|
||||||
|
request.want = 'xhtml'
|
||||||
|
|
||||||
|
env = jinja2.Environment(loader=self.TMPL_LOADER)
|
||||||
|
env.globals.update(
|
||||||
|
url=request.create_href,
|
||||||
|
static=request.static_resource,
|
||||||
|
)
|
||||||
|
self.render = lambda t, **k: env.get_template(t).render(**k)
|
||||||
|
|
||||||
def __after__(self, request):
|
def __after__(self, request):
|
||||||
self.session.rollback()
|
self.session.rollback()
|
||||||
self.session.bind.dispose()
|
self.session.bind.dispose()
|
||||||
|
@ -37,9 +66,12 @@ class ZoneListController(BaseController):
|
||||||
|
|
||||||
def GET(self, request):
|
def GET(self, request):
|
||||||
response = request.ResponseClass()
|
response = request.ResponseClass()
|
||||||
response.content_type = 'application/json'
|
zones = self.session.query(model.Zone)
|
||||||
zones = map(model.Zone.as_dict, self.session.query(model.Zone))
|
if request.want == 'json':
|
||||||
json.dump(list(zones), response.body_file)
|
response.content_type = 'application/json'
|
||||||
|
json.dump(list(map(model.Zone.as_dict, zones)), response.body_file)
|
||||||
|
else:
|
||||||
|
response.text = self.render('index.html.j2', zones=zones)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
HEAD = GET
|
HEAD = GET
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<title>Zones</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Zones</h1>
|
||||||
|
{% for zone in zones -%}
|
||||||
|
<div><a href="{{ zone.name }}">{{ zone.name }}</a></div>
|
||||||
|
{% endfor -%}
|
||||||
|
</body>
|
||||||
|
</html>
|
Reference in New Issue