-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
88 lines (67 loc) · 2.73 KB
/
app.py
File metadata and controls
88 lines (67 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import os
import subprocess
from flask import Flask, render_template, request, redirect, url_for, abort
APP_DIR = os.path.dirname(os.path.abspath(__file__))
DATA_DIR = os.environ.get("LABEL_PRINTER_DATA_DIR", ".")
IMAGE = os.path.join(DATA_DIR, "serial_qr.png")
app = Flask(__name__)
def _print_image():
subprocess.run(
["brother_ql", "print", "-l", "62", "--600dpi", IMAGE],
check=False,
)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/printlabel", methods=["GET", "POST"])
def print_label():
subprocess.run(["python3", os.path.join(APP_DIR, "generator.py")], check=False)
_print_image()
return redirect(url_for("index", message="Label gedruckt"))
@app.route("/printlabelWithDate", methods=["GET", "POST"])
def print_label_with_date():
subprocess.run(["python3", os.path.join(APP_DIR, "generator_with_date.py")], check=False)
_print_image()
return redirect(url_for("index", message="Label gedruckt"))
@app.route("/printlabelHistory", methods=["GET", "POST"])
def print_label_history():
_print_image()
return redirect(url_for("index", message="Label gedruckt"))
@app.route("/printlabelAsset", methods=["GET", "POST"])
def print_label_asset():
asset_id = request.args.get("id")
if not asset_id or asset_id == "0":
abort(500, description="(id) query parameter must be provided and non-zero")
subprocess.run(
["python3", os.path.join(APP_DIR, "generator_asset.py"), str(int(asset_id))],
check=False,
)
_print_image()
return redirect(url_for("index", message="Label gedruckt"))
@app.route("/printwlan", methods=["POST"])
def print_wlan_password():
# POST-only so the password doesn't end up in access logs / browser
# history as a query string. Accepted as form data OR JSON body.
data = request.get_json(silent=True) or request.form
pw = (data.get("pw") or "").strip()
ssid = (data.get("ssid") or "Contiva Guest").strip()
valid_until = (data.get("valid_until") or "").strip()
if not pw:
abort(400, description="(pw) body field is required")
cmd = [
"python3",
os.path.join(APP_DIR, "generator_wlan.py"),
"--pw", pw,
"--ssid", ssid,
]
if valid_until:
cmd.extend(["--valid-until", valid_until])
subprocess.run(cmd, check=False)
_print_image()
# Browser clients (the test form below) get redirected back to the index;
# API clients should look at the HTTP status. 302 is fine for both.
return redirect(url_for("index", message="WLAN-Passwort gedruckt"))
if __name__ == "__main__":
host = os.environ.get("LABEL_PRINTER_HOST", "0.0.0.0")
port = int(os.environ.get("LABEL_PRINTER_PORT", "3333"))
app.run(host=host, port=port)