From c5200805e1154881cec1529a35371d69d495317f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Enrique=20G=C3=B3mez=20G=C3=B3mez?= Date: Thu, 28 Jul 2022 21:13:34 +0000 Subject: [PATCH] Uses websockets to refresh the page when QR code scanned --- README.md | 17 +++++++++++++++++ main.py | 27 +++++++++++++++++++++++---- templates/template.html | 30 +++++++++++++++++++++++++----- templates/thanks.html | 11 +++++++---- 4 files changed, 72 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index f6ae5ce..abdd26b 100644 --- a/README.md +++ b/README.md @@ -14,3 +14,20 @@ To create an empty `database.db` sqlite file with the table described in ```bash ./init_db.py ``` + +## Nginx reverse proxy configuration + +Add this code to the nginx configuration file for the virtual host: + +```nginx + location /socket.io { + set $webapp http://attendance.lxd:5000; + include proxy_params; + proxy_http_version 1.1; + proxy_buffering off; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "Upgrade"; + proxy_pass $webapp; + } +``` + diff --git a/main.py b/main.py index 2db1fd2..31a771e 100755 --- a/main.py +++ b/main.py @@ -6,7 +6,10 @@ from datetime import datetime from flask import Flask, request, render_template from flask_uuid import FlaskUUID from flask_qrcode import QRcode -# TODO: use sockets to broadcast when a hit to the expected uuid has been +from flask_socketio import SocketIO +from werkzeug.middleware.proxy_fix import ProxyFix + +# WIP: use sockets to broadcast when a hit to the expected uuid has been # received, so that connected clients (the Chromium public browser) can # refresh the qr code and update the list of users # https://flask-socketio.readthedocs.io @@ -14,6 +17,10 @@ from flask_qrcode import QRcode app = Flask(__name__) FlaskUUID(app) QRcode(app) +app.config['SECRET_KEY'] = '53a8373e7ae652cd38beba15454b1dc4' +#app = ProxyFix(app, x_for=1, x_host=1) +app.wsgi_app = ProxyFix(app.wsgi_app) +socketio = SocketIO(app, async_mode=None) def get_db_connection(): conn = sqlite3.connect('database.db') @@ -42,8 +49,9 @@ def catch_uuids(id): conn = get_db_connection() existing = conn.execute( 'SELECT * FROM hits WHERE uuid = ?', (str(id),)).fetchone() - # TODO: verify that the uuid was generated by us (otherwise any uuid, - # like one generated by the user, would be accepted) + existing = False # Temporary, for easier development + if id.fields[5] != uuid.getnode(): + error = 'DIFFERENT_NODE' if not ua: error = 'NO_USERNAME' elif existing: @@ -51,6 +59,8 @@ def catch_uuids(id): else: conn.execute("INSERT INTO hits (uuid, user) VALUES (?, ?)", (str(id), ua)) conn.commit() + socketio.emit('qr_used', {'data': (time, ua, str(id))}) + data = conn.execute( 'SELECT * FROM hits WHERE user = ? ORDER BY id DESC LIMIT 10', (ua,) ).fetchall() @@ -58,5 +68,14 @@ def catch_uuids(id): return render_template('thanks.html', user=ua, time=time, error=error, hits=data) +@socketio.on('message') +def handle_message(data): + print('received message: ' + data) + +@socketio.on('my_event') +def handle_my_custom_event(json): + print('received json: ' + str(json)) + if __name__ == '__main__': - app.run(host='0.0.0.0') + #app.run(host='0.0.0.0') + socketio.run(app, host='0.0.0.0') diff --git a/templates/template.html b/templates/template.html index 5a7b865..bed9666 100644 --- a/templates/template.html +++ b/templates/template.html @@ -1,18 +1,38 @@ - Example + + + +Example

QR Code:

For URL {{ request.url_root + next_uuid }}

- +

Last 10 users:

-
  • + + diff --git a/templates/thanks.html b/templates/thanks.html index 1e7dad3..8c9fc16 100644 --- a/templates/thanks.html +++ b/templates/thanks.html @@ -10,16 +10,19 @@ {% elif error == 'ALREADY_USED' %}

    Error

    Code has been used already.

    + {% elif error == 'DIFFERENT_NODE' %} +

    Error

    +

    Code was not generated by this system.

    {% else %}

    Thanks

    At {{ time }},

    -

    Registered (entrance? exit?) for user {{ user }} .

    +

    Registered entrance or exit for user {{ user }} .

    Last 10 registers for {{ user }}:

    -
  • + {% endif %}