From 26c2de12b6891bd77a4172c3cb5b09e72fa8a062 Mon Sep 17 00:00:00 2001 From: n07070 Date: Wed, 20 May 2026 13:29:23 +0200 Subject: [PATCH] Add new web route, restructure API route --- src/main.py | 104 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 95 insertions(+), 9 deletions(-) diff --git a/src/main.py b/src/main.py index d7695e4..7578262 100644 --- a/src/main.py +++ b/src/main.py @@ -98,6 +98,9 @@ app.config["TEMPLATES_AUTO_RELOAD"] = True # Printer connection # Uses the class defined in the printer.py file printer = Printer(app, 0x04B8, 0x0E28) +# printers = Printer(app) +# printers.discover_printers() +# printers.init() printer.init_printer() # Find out if we are running on a Raspberry Pi @@ -141,6 +144,86 @@ def webcam(): app.logger.debug("Loading webcam interface") 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 # The api has the following methods @@ -168,9 +251,7 @@ def api_print_sms(): 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")) - + return str(e), 400 try: # comment: We try to get a signature sign = request.form["signature"] @@ -179,10 +260,13 @@ def api_print_sms(): "No signature found for this print, using default signature.", str(e) ) sign = configuration_file["defaults"]["signature"] - - web.print_sms(txt, sign) - return redirect(url_for("index")) - + try: + # comment: We try to print the SMS + web.print_sms(txt, sign) + except Exception as e: + return str(e), 500 + # end try + return "OK", 200 @app.route("/api/print/img", methods=["POST"]) @limiter.limit("6/minute", override_defaults=False) @@ -244,12 +328,14 @@ def camera_picture(): return jsonify({"message": "No camera present"}), 500 +## Authentification + @app.route("/login") @limiter.limit("1/second", override_defaults=False) def login_page(): """Unsued, logins""" # web.login(username,password) - return redirect(url_for("index")) + return redirect(url_for("index")), 501 @app.route("/logout") @@ -257,7 +343,7 @@ def login_page(): def logout_page(): """Unused, logout""" # web.logout(username, password) - return redirect(url_for("index")) + return redirect(url_for("index")), 501 @app.errorhandler(429)