Restructure the code and implement a printing queue #29

Merged
n07070 merged 21 commits from restructure-printing-queue into master 2026-05-27 00:00:56 +02:00
Showing only changes of commit a06086521a - Show all commits

View File

@@ -98,6 +98,9 @@ app.config["TEMPLATES_AUTO_RELOAD"] = True
# Printer connection # Printer connection
# Uses the class defined in the printer.py file # Uses the class defined in the printer.py file
printer = Printer(app, 0x04B8, 0x0E28) printer = Printer(app, 0x04B8, 0x0E28)
# printers = Printer(app)
# printers.discover_printers()
# printers.init()
printer.init_printer() printer.init_printer()
# Find out if we are running on a Raspberry Pi # Find out if we are running on a Raspberry Pi
@@ -141,6 +144,86 @@ def webcam():
app.logger.debug("Loading webcam interface") app.logger.debug("Loading webcam interface")
return render_template("webcam.html") return render_template("webcam.html")
@app.route("/web/print/sms", methods=["POST"])
@limiter.limit("6/minute", override_defaults=False)
def web_print_sms():
"""Prints a short message on a printer"""
app.logger.debug("Printing an sms via web method")
try:
txt = request.form["txt"]
except werkzeug.exceptions.BadRequestKeyError as e:
app.logger.error("Whoops, we are missing the txt input field. : %s ", str(e))
flash("Whoops, no forms submitted or missing signature : %s", str(e))
return redirect(url_for("index"))
try:
# comment: We try to get a signature
sign = request.form["signature"]
except werkzeug.exceptions.BadRequestKeyError as e:
app.logger.warning(
"No signature found for this print, using default signature.", str(e)
)
sign = configuration_file["defaults"]["signature"]
try:
web.print_sms(txt, sign)
except Exception as e:
app.logger.error("Whoops, we could not print an SMS because : %s ", str(e))
flash("Whoops, we could not print an SMS because : %s ", str(e))
return redirect(url_for("index"))
# end try
flash("The SMS has been printed !")
return redirect(url_for("index"))
@app.route("/web/print/img")
@limiter.limit("1/second", override_defaults=False)
def web_print_img():
"""Prints an image on a printer"""
app.logger.debug("Printing an image with web method")
try:
# comment: We try to get a signature
sign = request.form["signature"]
except werkzeug.exceptions.BadRequestKeyError as e:
app.logger.warning(
"No signature found for this print, using default signature.", str(e)
)
sign = configuration_file["defaults"]["signature"]
if request.method == "POST":
# check if the post request has the file part
if "img" not in request.files:
app.logger.error("Whoops, no images submitted : %s ", str(e))
app.logger.error("Error getting the files : %s", str(e))
flash("Whoops, no images submitted : %s", str(e))
return redirect(url_for("index"))
file = request.files["img"]
# If the user does not select a file, the browser submits an
# empty file without a filename.
if file.filename == "":
app.logger.error("Submitted file has no filename !")
flash("Submitted file has no filename !")
return redirect(url_for("index"))
try:
app.logger.debug("Sending the image to the printer.")
web.print_image(file, sign)
except Exception as e:
app.logger.error("The image could not be printed because : %s ", str(e))
flash("The image could not be printed because : %s ", str(e))
return redirect(url_for("index"))
else:
app.logger.error("Method not allowed, please POST")
flash("Method not allowed, please POST")
return redirect(url_for("index"))
flash("Picture printed !")
return redirect(url_for("index"))
# API routes # API routes
# The api has the following methods # The api has the following methods
@@ -168,9 +251,7 @@ def api_print_sms():
txt = request.form["txt"] txt = request.form["txt"]
except werkzeug.exceptions.BadRequestKeyError as e: except werkzeug.exceptions.BadRequestKeyError as e:
app.logger.error("Whoops, we are missing the txt input field. : %s ", str(e)) app.logger.error("Whoops, we are missing the txt input field. : %s ", str(e))
flash("Whoops, no forms submitted or missing signature : %s", str(e)) return str(e), 400
return redirect(url_for("index"))
try: try:
# comment: We try to get a signature # comment: We try to get a signature
sign = request.form["signature"] sign = request.form["signature"]
@@ -179,10 +260,13 @@ def api_print_sms():
"No signature found for this print, using default signature.", str(e) "No signature found for this print, using default signature.", str(e)
) )
sign = configuration_file["defaults"]["signature"] sign = configuration_file["defaults"]["signature"]
try:
# comment: We try to print the SMS
web.print_sms(txt, sign) web.print_sms(txt, sign)
return redirect(url_for("index")) except Exception as e:
return str(e), 500
# end try
return "OK", 200
@app.route("/api/print/img", methods=["POST"]) @app.route("/api/print/img", methods=["POST"])
@limiter.limit("6/minute", override_defaults=False) @limiter.limit("6/minute", override_defaults=False)
@@ -244,12 +328,14 @@ def camera_picture():
return jsonify({"message": "No camera present"}), 500 return jsonify({"message": "No camera present"}), 500
## Authentification
@app.route("/login") @app.route("/login")
@limiter.limit("1/second", override_defaults=False) @limiter.limit("1/second", override_defaults=False)
def login_page(): def login_page():
"""Unsued, logins""" """Unsued, logins"""
# web.login(username,password) # web.login(username,password)
return redirect(url_for("index")) return redirect(url_for("index")), 501
@app.route("/logout") @app.route("/logout")
@@ -257,7 +343,7 @@ def login_page():
def logout_page(): def logout_page():
"""Unused, logout""" """Unused, logout"""
# web.logout(username, password) # web.logout(username, password)
return redirect(url_for("index")) return redirect(url_for("index")), 501
@app.errorhandler(429) @app.errorhandler(429)