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