author | Tero Marttila <terom@paivola.fi> |
Wed, 10 Oct 2012 23:29:38 +0300 | |
changeset 5 | 91970ce3fc6b |
parent 4 | b09436772d46 |
child 6 | 0f243c59d5d1 |
permissions | -rw-r--r-- |
3 | 1 |
# encoding: utf-8 |
2 |
||
4 | 3 |
# response types |
1
731d2df704f0
fixup index + non-chunked response (?) + hosts + evil hardcoded db url
Tero Marttila <terom@paivola.fi>
parents:
0
diff
changeset
|
4 |
from werkzeug.wrappers import Response |
3 | 5 |
from werkzeug.exceptions import ( |
6 |
HTTPException, |
|
7 |
BadRequest, # 400 |
|
8 |
NotFound, # 404 |
|
9 |
) |
|
4 | 10 |
from werkzeug.utils import redirect |
11 |
||
12 |
# view |
|
13 |
from pvl.html import tags as html |
|
3 | 14 |
|
15 |
class Handler (object) : |
|
16 |
""" |
|
17 |
Per-Request controller/view, containing the request context and generating the response. |
|
18 |
""" |
|
19 |
||
20 |
TITLE = None |
|
21 |
CSS = ( |
|
22 |
#"/static/layout.css", |
|
0 | 23 |
"/static/style.css", |
3 | 24 |
) |
0 | 25 |
|
4 | 26 |
def __init__ (self, app, request, urls) : |
3 | 27 |
""" |
28 |
app - wsgi.Application |
|
29 |
request - werkzeug.Request |
|
4 | 30 |
urls - werkzeug.routing.MapAdapter |
3 | 31 |
""" |
32 |
||
33 |
self.app = app |
|
34 |
self.db = app.db |
|
35 |
self.request = request |
|
4 | 36 |
self.urlmap = urls |
3 | 37 |
|
38 |
def title (self) : |
|
39 |
""" |
|
40 |
Render site/page title as text. |
|
41 |
""" |
|
42 |
||
43 |
if self.TITLE : |
|
44 |
return u"Päivölä Verkko :: {title}".format(title=self.TITLE) |
|
45 |
else : |
|
46 |
return u"Päivölä Verkko" |
|
47 |
||
48 |
def render (self) : |
|
49 |
""" |
|
50 |
Render page content (as <body>...</body>). |
|
51 |
""" |
|
52 |
||
53 |
raise NotImplementedError() |
|
54 |
||
55 |
def render_html (self) : |
|
56 |
""" |
|
57 |
Render page layout (as <html>). |
|
58 |
""" |
|
59 |
||
5
91970ce3fc6b
pvl.verkko.hosts: split up Handlers
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
60 |
title = self.title() |
91970ce3fc6b
pvl.verkko.hosts: split up Handlers
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
61 |
|
3 | 62 |
return html.html( |
63 |
html.head( |
|
5
91970ce3fc6b
pvl.verkko.hosts: split up Handlers
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
64 |
html.title(title), |
3 | 65 |
( |
66 |
html.link(rel='Stylesheet', type="text/css", href=src) for src in self.CSS |
|
67 |
), |
|
0 | 68 |
), |
3 | 69 |
html.body( |
5
91970ce3fc6b
pvl.verkko.hosts: split up Handlers
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
70 |
html.h1(title), |
3 | 71 |
self.render() |
72 |
) |
|
0 | 73 |
) |
74 |
||
4 | 75 |
def process (self, **params) : |
3 | 76 |
""" |
4 | 77 |
Process request args to build internal request state. |
3 | 78 |
""" |
79 |
||
80 |
pass |
|
81 |
||
4 | 82 |
def respond (self, **params) : |
3 | 83 |
""" |
4 | 84 |
Generate a response, or raise an HTTPException |
3 | 85 |
|
86 |
Does an HTML layout'd response per default. |
|
4 | 87 |
|
88 |
**params - url-mapped parameters |
|
3 | 89 |
""" |
90 |
||
4 | 91 |
# XXX: returning e.g. redirect? |
92 |
self.process(**params) |
|
93 |
||
94 |
# render as html per default |
|
95 |
text = unicode(html.document(self.render_html())) |
|
3 | 96 |
|
4 | 97 |
return Response(text, mimetype='text/html') |
3 | 98 |
|
99 |
class Index (Handler) : |
|
100 |
def render (self) : |
|
101 |
return ( |
|
102 |
html.ul( |
|
103 |
html.a(href='/hosts')("DHCP Hosts"), |
|
104 |
) |
|
1
731d2df704f0
fixup index + non-chunked response (?) + hosts + evil hardcoded db url
Tero Marttila <terom@paivola.fi>
parents:
0
diff
changeset
|
105 |
) |
0 | 106 |