]> git.aero2k.de Git - sirius.git/commitdiff
Split templating into its own module.
authorTom Hunger <tehunger@gmail.com>
Wed, 4 Feb 2015 20:10:21 +0000 (20:10 +0000)
committerTom Hunger <tehunger@gmail.com>
Wed, 4 Feb 2015 20:10:21 +0000 (20:10 +0000)
sirius/coding/templating.py [new file with mode: 0644]
sirius/web/printer_print.py

diff --git a/sirius/coding/templating.py b/sirius/coding/templating.py
new file mode 100644 (file)
index 0000000..565e303
--- /dev/null
@@ -0,0 +1,20 @@
+import jinja2
+
+DEFAULT_TEMPLATE = '''\
+<html>
+  <body>
+    {{ raw_html|safe }}
+  </body>
+</html>
+'''
+
+ENV = jinja2.Environment()
+
+
+
+# TODO apply scrubber library if wanted.
+# https://pypi.python.org/pypi/scrubber
+
+def default_template(raw_html):
+    t = ENV.from_string(DEFAULT_TEMPLATE)
+    return t.render(raw_html=raw_html)
index 2238a226cfb00975ab732c45a3d1b3b71232cadb..44cf4837b38a87542b340b9bf5099e52304a333d 100644 (file)
@@ -12,6 +12,7 @@ from sirius.models import messages as model_messages
 from sirius.protocol import protocol_loop
 from sirius.protocol import messages
 from sirius.coding import image_encoding
+from sirius.coding import templating
 from sirius import stats
 
 
@@ -51,7 +52,7 @@ def printer_print(user_id, username, printer_id):
     ]
     form.target_printer.choices = choices
 
-    # Set default value on get
+    # Set default printer on get
     if flask.request.method != 'POST':
         form.target_printer.data = printer.id
 
@@ -59,7 +60,7 @@ def printer_print(user_id, username, printer_id):
         # TODO: move image encoding into a pthread.
         # TODO: use templating to avoid injection attacks
         pixels = image_encoding.default_pipeline(
-            '<html><body>{}</body></html>'.format(form.message.data))
+            templating.default_template(form.message.data))
         hardware_message = messages.SetDeliveryAndPrint(
             device_address=printer.device_address,
             pixels=pixels,
@@ -116,7 +117,7 @@ def preview(user_id, username, printer_id):
 
     message = flask.request.data
     pixels = image_encoding.default_pipeline(
-        '<html><body>{}</body></html>'.format(message))
+        templating.default_template(message))
     png = io.BytesIO()
     pixels.save(png, "PNG")