--- a/pvl/web/__init__.py Thu Jan 31 00:11:29 2013 +0200
+++ b/pvl/web/__init__.py Sun Feb 10 12:06:01 2013 +0200
@@ -2,15 +2,10 @@
Werkzeug-based web application.
"""
+# items
from pvl.web.html import tags as html
from pvl.web.application import Application, Handler
# werkzeug
-from werkzeug.wrappers import Request, Response
-from werkzeug.exceptions import (
- HTTPException,
- BadRequest, # 400
- NotFound, # 404
-)
-from werkzeug.utils import redirect
+from werkzeug.wrappers import Request
--- a/pvl/web/application.py Thu Jan 31 00:11:29 2013 +0200
+++ b/pvl/web/application.py Sun Feb 10 12:06:01 2013 +0200
@@ -6,6 +6,8 @@
from werkzeug.wrappers import Request, Response
from werkzeug.exceptions import HTTPException
+import pvl.web.response
+
import logging; log = logging.getLogger('pvl.web.application')
class Application (object) :
@@ -156,6 +158,13 @@
)
))
+ def response_file (self, file) :
+ """
+ Wrap a file for returning in a response with direct_passthrough=True.
+ """
+
+ return werkzeug.wrap_file(self.request.environ, file)
+
def process (self, **params) :
"""
Process request args to build internal request state.
@@ -177,9 +186,10 @@
return response
# render as html per default
+ # XXX: might also be string?
text = unicode(self.render_html())
- return Response(text, mimetype='text/html')
+ return pvl.web.response.html(text)
def cleanup (self) :
"""
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/pvl/web/response.py Sun Feb 10 12:06:01 2013 +0200
@@ -0,0 +1,33 @@
+from werkzeug.wrappers import Response
+from werkzeug.exceptions import (
+ HTTPException,
+ BadRequest, # 400
+ NotFound, # 404
+)
+from werkzeug.utils import redirect
+
+
+import json as _json
+
+def html (tag, **opts) :
+ """
+ Return text/html response for given pvl.web.html
+ """
+
+ return Response(unicode(tag), mimetype='text/html', **opts)
+
+def json (data, **opts) :
+ """
+ Return text/json response for given object.
+ """
+
+ return Response(_json.dumps(data), mimetype='text/json', **opts)
+
+def image (file, type='png') :
+ """
+ Return image/{type} response for given file-like object.
+ """
+
+ # respond with file wrapper
+ return Response(file, mimetype='image/{type}'.format(type=type), direct_passthrough=True)
+