]> git.aero2k.de Git - sirius.git/commitdiff
Use device key domain for references to device keys
authorJoe Rickerby <joerick@mac.com>
Wed, 10 Apr 2019 09:17:11 +0000 (10:17 +0100)
committerJoe Rickerby <joerick@mac.com>
Wed, 10 Apr 2019 09:17:11 +0000 (10:17 +0100)
sirius/models/print_keys.py
sirius/web/templates/print_key.html
sirius/web/templates/printer_overview.html

index c82cabbf30d0db5fff7d9119689e53500c9f3b49..5f0966cf72b9de9329c5827ecc492fb41c5fc697 100644 (file)
@@ -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
index 963c7c4689d8bb784f6facc40f71ead14b8851ff..ea473165d0378fcdd221e29179a91dfd5efc7d3b 100644 (file)
                 <img class="device-key-symbol" src="{{url_for('static', filename='device-key-large.svg')}}">
 
                 <div class="device-key-label">Device Key:</div>
-                <a class="device-key" href="{{request.url}}">{{request.url}}</a>
+                <a class="device-key" href="{{print_key.url}}">{{print_key.url}}</a>
 
                 <p class="what-is-this">
                     A Device Key is a secret URL for a device, containing both
                     <dd>
                         Little Printers<br>
                         <small>
-                            <a href="{{'co.nordprojects.littleprinter:///printers?add=' + request.url|urlencode}}">Open</a>
+                            <a href="{{'co.nordprojects.littleprinter:///printers?add=' + print_key.url|urlencode}}">Open</a>
                             | <a href="https://itunes.apple.com/us/app/little-printers/id1393105914?ls=1&mt=8"> Download</a>
                         </small>
                     </dd>
                 <p>
                     You could do this from IFTTT, a Glitch app, your web app, or the command line. For example:
                 </p>
-                <pre><code>curl '{{request.url}}?from=example' \
+                <pre><code>curl '{{print_key.url}}?from=example' \
     -H 'content-type: text/plain' \
     -d 'boop beep boop'</code></pre>
 
                     <code>Content-Type: text/html</code>
                 </p>
                 <p>Renders and prints HTML. Surprisingly powerful! Supports HTML elements, CSS styling, and embedded images.</p>
-                <pre><code>curl '{{request.url}}?from=htmlexample' \
+                <pre><code>curl '{{print_key.url}}?from=htmlexample' \
     -H 'content-type: text/html' \
     -d "&lt;h1&gt;Yep, you can do this&lt;/h1&gt;&lt;small&gt;or this&lt;/small&gt;"</code></pre>
 
                     <code>Content-Type: image/jpeg</code>
                 </p>
                 <p>Prints out the image data from the request body, in a lovely dithered style.</p>
-                <pre><code>cat mindblown.gif | curl '{{request.url}}?from=image-example' \
+                <pre><code>cat mindblown.gif | curl '{{print_key.url}}?from=image-example' \
     -X POST \
     -H 'content-type: image/gif' \
     --data-binary @-</code></pre>
                 <p>
                     Send HTML using the <tt>html</tt> key or plain text using the <tt>text</tt> key.
                 </p>
-                <pre><code>curl '{{request.url}}?from=jsonexample' \
+                <pre><code>curl '{{print_key.url}}?from=jsonexample' \
     -H 'content-type: application/json' \
     -d '{"html":"&lt;h1&gt;header!&lt;/h1&gt;"}'</code></pre>
 
                     <code>Content-Type: text/plain</code>
                 </p>
                 <p>Prints out plain text.</p>
-                <pre><code>curl '{{request.url}}?from=text-example' \
+                <pre><code>curl '{{print_key.url}}?from=text-example' \
     -H 'content-type: text/plain' \
     -d "I'm a little teapot"</code></pre>
 
 
                 <p>This URL is also a status API e.g.</p>
 
-                <pre><code>curl '{{request.url}}'</code></pre>
+                <pre><code>curl '{{print_key.url}}'</code></pre>
 
                 <p>This returns the status of the printer in JSON format.</p>
 
index 42fcbb8607d4b0d62f6d79a32a448d49a7402857..34d2fbb2ae5ef961162a2808e20d9c3a1194d089 100644 (file)
@@ -49,9 +49,8 @@
                                {% for print_key in printer.print_keys %}
                                <tr>
                                        <td>
-                                               <a href="{{ url_for('print_key_api.print_key',
-                                                                   print_key_secret=print_key.secret) }}">
-                                                       {{ print_key.secret }}
+                                               <a href="{{ print_key.url }}">
+                                                       {{ print_key.url }}
                                                </a>
                                        </td>
                                        <td>{{ print_key.created.strftime('%H:%M %d/%m/%Y') }}</td>