]> git.aero2k.de Git - sirius.git/commitdiff
Implement the skeleton functionality for printing.
authorTom Hunger <tehunger@gmail.com>
Thu, 15 Jan 2015 15:49:04 +0000 (15:49 +0000)
committerTom Hunger <tehunger@gmail.com>
Thu, 15 Jan 2015 16:02:50 +0000 (16:02 +0000)
The actual print encoding still has a bug (the messages are rejected
by the printer with return status 0x90).

sirius/web/printer_overview.py
sirius/web/printer_print.py
sirius/web/templates/overview.html
sirius/web/templates/printer_overview.html
sirius/web/templates/printer_print.html

index ad74eb54ae92fe608f1282d38ac1e1cd2f563a17..7c3656de80e16e255df5581db40f94e11b2653e9 100644 (file)
@@ -14,6 +14,8 @@ def printer_overview(user_id, username, printer_id):
     assert username == login.current_user.username
 
     printer = hardware.Printer.query.get(printer_id)
+    if printer is None:
+        flask.abort(404)
 
     return flask.render_template(
         'printer_overview.html',
index 35e9ea1f1a2be544cb9b05786004d20b25e01a04..e7e1b4bbfbc8286c39f67b8ec8f92f94cc0f4fba 100644 (file)
@@ -1,14 +1,65 @@
 import flask
-
 from flask.ext import login
+import flask_wtf
+import wtforms
+
+from sirius.models import hardware
+from sirius.protocol import protocol_loop
+from sirius.protocol import messages
+from sirius.coding import image_encoding
 
 blueprint = flask.Blueprint('printer_print', __name__)
 
 
+class PrintForm(flask_wtf.Form):
+    target_printer = wtforms.SelectField(
+        'target_printer',
+        coerce=int,
+        validators=[wtforms.validators.DataRequired()],
+    )
+    message = wtforms.StringField(
+        'message',
+        validators=[wtforms.validators.DataRequired()],
+    )
+
+
 @login.login_required
-@blueprint.route('/<int:user_id>/<username>/print')
-def printer_print(user_id, username):
+@blueprint.route('/<int:user_id>/<username>/printer/<int:printer_id>/print', methods=['GET', 'POST'])
+def printer_print(user_id, username, printer_id):
     assert user_id == login.current_user.id
     assert username == login.current_user.username
 
-    return flask.render_template('printer_print.html')
+    printer = hardware.Printer.query.get(printer_id)
+    if printer is None:
+        flask.abort(404)
+
+    form = PrintForm()
+    # Note that the form enforces access permissions: People can't
+    # submit a valid printer-id that's not owned by the user:
+    form.target_printer.choices = [
+        (x.id, x.name) for x in login.current_user.printers.all()]
+
+    if form.validate_on_submit():
+        flask.flash('Sent your message to the printer!')
+
+        # TODO: move image encoding into a pthread.
+        # TODO: use templating to avoid injection attacks
+        pixels = image_encoding.html_to_png(
+            '<html><body>{}</body></html>'.format(form.message.data))
+        hardware_message = messages.SetDeliveryAndPrint(
+            device_address=printer.device_address,
+            pixels=pixels,
+        )
+        protocol_loop.send_message(printer.device_address, hardware_message)
+
+        return flask.redirect(flask.url_for(
+            'printer_overview.printer_overview',
+            user_id=login.current_user.id,
+            username=login.current_user.username,
+            printer_id=printer.id))
+
+    return flask.render_template(
+        'printer_print.html',
+        printer=printer,
+        form=form,
+    )
index f9d789c89d9e120c8a9984c85fc2095b3b0b034b..019411d68ec22c613a577ddad8051ec37335527e 100644 (file)
        </li>
 </ul>
 {% endfor %}
+
+<div>
+       <a href="{{ url_for('landing.claim',
+                         user_id=current_user.id,
+                         username=current_user.username) }}">
+               Claim another printer
+       </a>.
+</div>
+
 {% endif %}
 <h1>Printers of friends</h1>
 {% endblock %}
index 0c0042670a2ea87f836dbb9fa9c124548dc57f7a..dae4ba52717296d345cb222b43319aadb0401caf 100644 (file)
@@ -4,4 +4,12 @@
 
 {% block page_content %}
 <h1>Printer {{ printer.name }}</h1>
+
+<a href="{{ url_for('printer_print.printer_print',
+               user_id=current_user.id,
+               username=current_user.username,
+               printer_id=printer.id) }}">
+       Print a thing
+</a>
+<h2>Messages</h2>
 {% endblock %}
index 5a7b32600abc429923586dc7abd5754a3a69e9ec..018533ae784f208c0a5f0700cb632fb2fb71f7df 100644 (file)
@@ -1,7 +1,17 @@
 {% extends "base.html" %}
 
-{% block title %}Overview{% endblock %}
+{% block title %}Print{% endblock %}
 
 {% block page_content %}
-<h1>Print a thing</h1>
+<h1>Print a thing on {{ printer.name }}</h1>
+
+<form method="POST">
+       {{ form.csrf_token }}
+       {{ form.target_printer.errors }}
+       {{ form.target_printer }}
+       {{ form.message.errors }}
+       {{ form.message }}
+       <input type="submit" value="Print now">
+</form>
+
 {% endblock %}