# coding: utf-8
"""
Generic controller base classes.
respond(Request) -> Response
"""
import werkzeug
from werkzeug import Response
from svv.html import tags as html
from svv import pdf, utils
import datetime
class AppHandler (object):
"""
Per-request handler type, containing the request context and implementing the response.
Works on a generic level, i.e. WSGI/Werkzeug URL-mapped request in, response out.
"""
def __init__ (self, app, request, urlmap) :
"""
Initialize for processing the given Request, to prepare for action-method later on
app - the global Application state
request - the request state
urlmap - the URL map we were matched through
"""
self.app = app
self.request = request
self.urlmap = urlmap
def url_for (self, endpoint, fragment=None, **values) :
"""
Return an URL for the given endpoint, applying the given URL-embedded values
endpoint - endpoint mapped in svv.urls to target
fragment - (optional) #fragment to add to URL
**values - url-values for mapped endpoint
"""
url = self.urlmap.build(endpoint, values)
if fragment :
# XXX: encode
url += '#' + fragment
return url
def redirect_for (self, endpoint, **values) :
"""
Return a Response that will redirect the client to the given endpoint.
"""
return werkzeug.redirect(self.url_for(endpoint, **values))
@property
def POST (self) :
"""
Access request's POST data
"""
# MultiDict from werkzeug.Request
return self.request.form
def respond (self, **url_values) :
"""
Handle request that was mapped to ourselves via the URL routing, using given dict of values from URL.
"""
# process e.g. POST data for e.g. redirect
response = self.process(**url_values)
if not response :
# assume superclass does something else if process didn't handle it
pass
return response
def process (self, **args) :
"""
Process incoming POST data, optionally returning a redirect response.
"""
# default to ignore
pass
class PageHandler (AppHandler) :
"""
Specialized AppHandler for normal HTML page views.
Renders the layout template and HTML.
"""
# XXX: how to handle site title vs page title?
def format_title (self) :
"""
Render site/page title as plain text.
"""
return u"SpeksiVVuokraus"
def build_breadcrumb (self) :
"""
Return an optional list of (title, target, args) segments leading up to and including this page.
"""
return None
def render_header (self) :
"""
Page header.
"""
from svv import urls
# link to main page
return html.a(href=self.url_for(urls.Index))(
self.format_title()
)
def render_menu (self) :
"""
Site navigation menu (vertical menu next to content)
"""
# XXX: circular-import hack for urls
# it's not sustainable for a base class to referr to its subclasses at define time
from svv import urls
# XXX: menu def, should be somewhere elsewhere..
menu = [
("Kalenteri", urls.CalendarView),
("Uusi tilaus", urls.NewOrderView),
("Tilaukset", urls.OrdersView),
("Tilaajat", urls.CustomersView),
("Inventaari", urls.InventoryView),
]
# render
return html.ul(
html.li(
html.a(href=self.url_for(target))(title)
) for title, target in menu
)
def render_navigation (self) :
"""
Page navigation menu (compact horizontal above content), used for breadcrumb
"""
breadcrumb = self.build_breadcrumb()
if not breadcrumb :
# optional
return None
return html.ul(
html.li(
html.a(href=self.url_for(target, **args))(
title
)
) for title, target, args in breadcrumb
)
def render_footer (self) :
"""
Page footer.
"""
# current tz-aware time
now = datetime.datetime.now(utils.LocalTimezone())
return (
html.div(class_='right')(
# timestamp with timezone offset
now.strftime('%Y/%m/%d %H:%M:%S %Z (GMT%z)')
)
)
def render_content (self, **args) :
"""
Render and return HTML for page content.
XXX: must be HTML object, support just text?
"""
raise NotImplementedError()
def render_layout (self, content) :
"""
Render the page layout for the given content block.
"""
# XXX: layout template, should be somewhere far away
css = [
"/static/layout.css",
"/static/style.css",
"/static/forms.css",
"/static/tables.css",
"/static/cal.css",
"/static/treelist.css",
"/static/jquery-ui.theme/jquery-ui.css",
]
scripts = [
"/static/js/jquery.js",
"/static/js/jquery-ui.min.js", # fairly big
"/static/js/jquery-ui-timepicker.js",
"/static/js/jquery-ui-datepicker-fi.js",
"/static/js/forms.js",
#"/static/js/prototype.js",
#"/static/js/scriptaculous/scriptaculous.js",
#"/static/js/treelist.js"
]
return html.document(html.html(
html.head(
html.title(
# plaintext title
self.format_title()
),
(
html.link(rel='Stylesheet', type="text/css", href=src) for src in css
),
# XXX: script can't self-close?
(
html.script(src=src, _selfclosing=False) for src in scripts
),
),
html.body(
html.div(id='header')(
self.render_header()
),
html.div(id='container')(
html.div(id='menu')(
self.render_menu()
),
# XXX: breadcrumb etc.
# html.div(id='nav')(
# self.render_navgation()
#),
# the actual page content (pre-rendered)
html.div(id='content')(
content
),
),
html.div(id='footer')(
self.render_footer()
),
),
))
def render (self, **url_values) :
"""
Render full page HTML
"""
# render page content
content = self.render_content(**url_values)
# render into layout
response = self.render_layout(content)
# ok
return response
def respond (self, **url_values) :
"""
Build and return a response from the following steps:
* process()
* render() -> render_content() as HTML
"""
# optional processing
response = super(PageHandler, self).respond(**url_values)
if not response :
# render page HTML as unicode
html = unicode(self.render(**url_values))
# response object
# XXX: charset?
return Response(html, mimetype='text/html')
# ok
return response
class DocumentHandler (AppHandler) :
"""
PDF generation/export
"""
def respond (self, **url_values) :
"""
Generate the document, and return it as a .pdf file, with the filename generated from the document's title.
"""
# optional processing
response = super(DocumentHandler, self).respond(**url_values)
if not response :
pdf_file = self.generate(**url_values)
# file wrapper
# XXX: is this any use at all for StringIO?
pdf_file = werkzeug.wrap_file(self.request.environ, pdf_file)
# respond with file wrapper
response = Response(pdf_file, mimetype='application/pdf', direct_passthrough=True)
# ok
return response
def generate (self, **url_values) :
"""
Generate the PDF document as a file-like object.
"""
document, elements = self.generate_document(**url_values)
# render and return StringIO
return document.render_buf(elements)
def generate_document (self, **url_values) :
"""
Return the
(DocumentTemplate, elements)
to generate the PDF document from.
"""
raise NotImplementedError()
### XXX: random test controllers
class Index (PageHandler) :
DATA = (
(100, "Top A", []),
(200, "Top B", [
(210, "Mid BA", [
(211, "Sub BAA", []),
(212, "Sub BAB", []),
]),
(220, "Mid BB", []),
]),
(300, "Top C", [
(310, "Mid CA", []),
(320, "Mid CB", []),
]),
)
def render_content (self) :
# build data set
data = self.DATA
def render_item (id, text, children) :
return html.li(
html.div(class_='item')(
# item text
html.a(html.input(type='checkbox', name_=id, checked=(
'checked' if self.request.form.get(str(id)) else None
)), text, class_='text'),
),
# children
html.ul(render_item(*item) for item in children if item) if children else None,
#
class_=('more open' if children else None),
)
# XXX: nothing much to see here
return html.h1("Mui.")
# render it
return (
html.h3("Item list"),
html.form(action='.', method='post')(
html.ul(class_='treelist')(render_item(*item) for item in data),
html.input(type='submit'),
),
)