--- a/pvl/verkko/web.py Wed Oct 10 22:10:16 2012 +0300
+++ b/pvl/verkko/web.py Wed Oct 10 22:45:50 2012 +0300
@@ -1,31 +1,105 @@
+# encoding: utf-8
+
from werkzeug.wrappers import Response
# view
from pvl.html import tags as html
-def render_layout (title, content) :
- css = [
- "/static/layout.css",
+# errors
+from werkzeug.exceptions import (
+ HTTPException,
+ BadRequest, # 400
+ NotFound, # 404
+)
+
+class Handler (object) :
+ """
+ Per-Request controller/view, containing the request context and generating the response.
+ """
+
+ TITLE = None
+ CSS = (
+ #"/static/layout.css",
"/static/style.css",
- ]
+ )
- return unicode(html.document(html.html(
- html.head(
- html.title(title),
- (
- html.link(rel='Stylesheet', type="text/css", href=src) for src in css
+ def __init__ (self, app, request, path) :
+ """
+ app - wsgi.Application
+ request - werkzeug.Request
+ """
+
+ self.app = app
+ self.db = app.db
+ self.request = request
+
+ # TODO
+ self.path = path
+
+ @property
+ def title (self) :
+ """
+ Render site/page title as text.
+ """
+
+ if self.TITLE :
+ return u"Päivölä Verkko :: {title}".format(title=self.TITLE)
+ else :
+ return u"Päivölä Verkko"
+
+ def render (self) :
+ """
+ Render page content (as <body>...</body>).
+ """
+
+ raise NotImplementedError()
+
+ def render_html (self) :
+ """
+ Render page layout (as <html>).
+ """
+
+ return html.html(
+ html.head(
+ html.title(self.title),
+ (
+ html.link(rel='Stylesheet', type="text/css", href=src) for src in self.CSS
+ ),
),
- ),
- html.body(
- html.h1(title),
- content
+ html.body(
+ html.h1(self.title),
+ self.render()
+ )
)
- )))
-def render_index () :
- return Response(render_layout("Verkko", (
- html.ul(
- html.a(href='/hosts')("DHCP Hosts"),
+ def process (self) :
+ """
+ TODO: process request args to build internal state
+ """
+
+ pass
+
+ def respond (self) :
+ """
+ Generate a response.
+
+ Does an HTML layout'd response per default.
+ """
+
+ try :
+ # XXX: returning e.g. redirect? args?
+ self.process()
+
+ return Response(unicode(html.document(self.render_html())), mimetype='text/html')
+
+ except HTTPException as ex :
+ return ex
+
+class Index (Handler) :
+ def render (self) :
+ return (
+ html.ul(
+ html.a(href='/hosts')("DHCP Hosts"),
+ )
)
- )), content_type='text/html')