From f90756865b55e7732782211e3a31c88b7a366f5b Mon Sep 17 00:00:00 2001 From: Joe Rickerby Date: Wed, 10 Apr 2019 10:17:11 +0100 Subject: [PATCH] Use device key domain for references to device keys --- sirius/models/print_keys.py | 15 +++++++++++++-- sirius/web/templates/print_key.html | 16 ++++++++-------- sirius/web/templates/printer_overview.html | 5 ++--- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/sirius/models/print_keys.py b/sirius/models/print_keys.py index c82cabb..5f0966c 100644 --- a/sirius/models/print_keys.py +++ b/sirius/models/print_keys.py @@ -1,4 +1,5 @@ -import datetime, random, string +import datetime, random, string, os +from flask import request from sirius.models.db import db from sqlalchemy.dialects import postgresql from sqlalchemy.orm.attributes import flag_modified @@ -35,4 +36,14 @@ class PrintKey(db.Model): remote_side=[id]) def senders_formatted(self): - return ', '.join((s or 'Anonymous') for s in self.senders) \ No newline at end of file + return ', '.join((s or 'Anonymous') for s in self.senders) + + @property + def url(self): + if 'DEVICE_KEY_DOMAIN' in os.environ: + return 'https://%s/%s' % (os.environ['DEVICE_KEY_DOMAIN'], self.secret) + else: + # if a DEVICE_KEY_DOMAIN is not defined, we need a request object + # to get the full URL. + return '%sprintkey/%s' % (request.url_root, self.secret) + \ No newline at end of file diff --git a/sirius/web/templates/print_key.html b/sirius/web/templates/print_key.html index 963c7c4..ea47316 100644 --- a/sirius/web/templates/print_key.html +++ b/sirius/web/templates/print_key.html @@ -181,7 +181,7 @@
Device Key:
- {{request.url}} + {{print_key.url}}

A Device Key is a secret URL for a device, containing both @@ -207,7 +207,7 @@

Little Printers
- Open + Open | Download
@@ -280,7 +280,7 @@

You could do this from IFTTT, a Glitch app, your web app, or the command line. For example:

-
curl '{{request.url}}?from=example' \
+                
curl '{{print_key.url}}?from=example' \
     -H 'content-type: text/plain' \
     -d 'boop beep boop'
@@ -295,7 +295,7 @@ Content-Type: text/html

Renders and prints HTML. Surprisingly powerful! Supports HTML elements, CSS styling, and embedded images.

-
curl '{{request.url}}?from=htmlexample' \
+                
curl '{{print_key.url}}?from=htmlexample' \
     -H 'content-type: text/html' \
     -d "<h1>Yep, you can do this</h1><small>or this</small>"
@@ -306,7 +306,7 @@ Content-Type: image/jpeg

Prints out the image data from the request body, in a lovely dithered style.

-
cat mindblown.gif | curl '{{request.url}}?from=image-example' \
+                
cat mindblown.gif | curl '{{print_key.url}}?from=image-example' \
     -X POST \
     -H 'content-type: image/gif' \
     --data-binary @-
@@ -321,7 +321,7 @@

Send HTML using the html key or plain text using the text key.

-
curl '{{request.url}}?from=jsonexample' \
+                
curl '{{print_key.url}}?from=jsonexample' \
     -H 'content-type: application/json' \
     -d '{"html":"<h1>header!</h1>"}'
@@ -330,7 +330,7 @@ Content-Type: text/plain

Prints out plain text.

-
curl '{{request.url}}?from=text-example' \
+                
curl '{{print_key.url}}?from=text-example' \
     -H 'content-type: text/plain' \
     -d "I'm a little teapot"
@@ -340,7 +340,7 @@

This URL is also a status API e.g.

-
curl '{{request.url}}'
+
curl '{{print_key.url}}'

This returns the status of the printer in JSON format.

diff --git a/sirius/web/templates/printer_overview.html b/sirius/web/templates/printer_overview.html index 42fcbb8..34d2fbb 100644 --- a/sirius/web/templates/printer_overview.html +++ b/sirius/web/templates/printer_overview.html @@ -49,9 +49,8 @@ {% for print_key in printer.print_keys %} - - {{ print_key.secret }} + + {{ print_key.url }} {{ print_key.created.strftime('%H:%M %d/%m/%Y') }} -- 2.47.3