]> git.aero2k.de Git - sirius.git/commitdiff
New default styling for messages sent via the web interface
authorJoe Rickerby <joerick@mac.com>
Wed, 10 Jan 2018 14:59:42 +0000 (14:59 +0000)
committerJoe Rickerby <joerick@mac.com>
Wed, 10 Jan 2018 14:59:42 +0000 (14:59 +0000)
sirius/coding/default_template.html [new file with mode: 0644]
sirius/coding/templating.py
sirius/web/external_api.py
sirius/web/printer_print.py

diff --git a/sirius/coding/default_template.html b/sirius/coding/default_template.html
new file mode 100644 (file)
index 0000000..855ea23
--- /dev/null
@@ -0,0 +1,48 @@
+<html>
+  <head>
+    <style>
+      html, body {
+        margin: 0;
+        padding: 0;
+        background-color: white;
+      }
+
+      body {
+        /*
+           first line must be white because of a bug in the image encoder,
+           that inverts the image if the first pixel is black
+        */
+        margin-top: 1px;
+        margin-bottom: 80px;
+        border: 1px;
+        font-family: Helvetica, Arial, sans-serif;
+        font-size: 30px;
+      }
+      h1, h2, h3, h4, h5, h6 {
+        margin-bottom: 20px;
+      }
+
+      li {
+        margin-bottom: 0.5em;
+      }
+
+      .header {
+        font-weight: 800;
+        text-transform: uppercase;
+        font-size: 18px;
+        margin-bottom: 18px;
+        text-align: center;
+      }
+
+      img {
+        max-width: 100%;
+      }
+    </style>
+  </head>
+  <body>
+    <div class="header">
+      {{ date.strftime('%H:%M | %d-%b-%Y') }}{% if from_name %} | {{ from_name }}{% endif %}
+    </div>
+    {{ raw_html|safe }}
+  </body>
+</html>
index 565e303e73c8b8b91de90847d8b04df508e04e01..188ad4dce387fa4cfb22a5a9480d64f641806599 100644 (file)
@@ -1,20 +1,20 @@
+import os, datetime
 import jinja2
 
-DEFAULT_TEMPLATE = '''\
-<html>
-  <body>
-    {{ raw_html|safe }}
-  </body>
-</html>
-'''
+DEFAULT_TEMPLATE_FILE = os.path.join(os.path.dirname(__file__), 'default_template.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)
+def default_template(raw_html, from_name):
+    with open(DEFAULT_TEMPLATE_FILE) as f:
+        template = f.read()
+
+    t = ENV.from_string(template)
+    return t.render(
+        raw_html=raw_html,
+        date=datetime.datetime.now(),
+        from_name=from_name
+    )
index ae7cfbd770a4f3758a9ee69ff354828ac45e8cc5..817cd035e5ab8f2dbe9d8eb47d34ad1f2944d195 100644 (file)
@@ -45,7 +45,11 @@ def print_html(printer_id):
         flask.abort(500)
 
     pixels = image_encoding.default_pipeline(
-      templating.default_template(task['message']))
+        templating.default_template(
+            task['message'],
+            from_name=login.current_user.username
+        )
+    )
 
     hardware_message = None
     if task['face'] == "noface":
index 0916b3542218a6cfaddb21aece1c44e1dede4596..a768cad789ab0e51cd8b027ce5657e26eb8727e7 100644 (file)
@@ -76,7 +76,7 @@ def printer_print(printer_id):
         # TODO: move image encoding into a pthread.
         # TODO: use templating to avoid injection attacks
         pixels = image_encoding.default_pipeline(
-            templating.default_template(form.message.data))
+            templating.default_template(form.message.data, from_name=login.current_user.username))
 
         hardware_message = None
         if form.face.data == "noface":
@@ -139,7 +139,7 @@ def preview(user_id, username, printer_id):
 
     message = flask.request.data
     pixels = image_encoding.default_pipeline(
-        templating.default_template(message))
+        templating.default_template(message, from_name=login.current_user.username))
     png = io.BytesIO()
     pixels.save(png, "PNG")