Fix error flashing and transmission
This commit is contained in:
115
src/main.py
115
src/main.py
@@ -85,7 +85,7 @@ except PermissionError:
|
||||
exit(77)
|
||||
|
||||
# Output the config file
|
||||
if os.getenv("LIPY_DEBUG") is True:
|
||||
if os.getenv("FLASK_DEBUG"):
|
||||
pprint.pprint(configuration_file)
|
||||
|
||||
# We define the app module used by Flask
|
||||
@@ -154,7 +154,7 @@ def web_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))
|
||||
flash("Whoops, no forms submitted or missing signature : " + str(e), 'error')
|
||||
return redirect(url_for("index"))
|
||||
|
||||
try:
|
||||
@@ -170,15 +170,15 @@ def web_print_sms():
|
||||
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))
|
||||
flash("Whoops, we could not print an SMS because :" + str(e), 'error')
|
||||
return redirect(url_for("index"))
|
||||
|
||||
# end try
|
||||
flash("The SMS has been printed !")
|
||||
flash("The SMS has been printed !", 'info')
|
||||
return redirect(url_for("index"))
|
||||
|
||||
|
||||
@app.route("/web/print/img")
|
||||
@app.route("/web/print/img", methods=["POST"])
|
||||
@limiter.limit("1/second", override_defaults=False)
|
||||
def web_print_img():
|
||||
"""Prints an image on a printer"""
|
||||
@@ -193,36 +193,30 @@ def web_print_img():
|
||||
)
|
||||
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")
|
||||
# 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 : " + str(e), 'error')
|
||||
return redirect(url_for("index"))
|
||||
|
||||
flash("Picture printed !")
|
||||
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 !", 'error')
|
||||
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 : " + str(e), 'error')
|
||||
return redirect(url_for("index"))
|
||||
|
||||
flash("Picture printed !", 'info')
|
||||
return redirect(url_for("index"))
|
||||
|
||||
# API routes
|
||||
@@ -246,7 +240,7 @@ def api_index():
|
||||
@limiter.limit("6/minute", override_defaults=False)
|
||||
def api_print_sms():
|
||||
"""Prints a short message on a printer"""
|
||||
app.logger.debug("Printing an sms")
|
||||
app.logger.debug("Printing an sms via API")
|
||||
try:
|
||||
txt = request.form["txt"]
|
||||
except werkzeug.exceptions.BadRequestKeyError as e:
|
||||
@@ -257,7 +251,7 @@ def api_print_sms():
|
||||
sign = request.form["signature"]
|
||||
except werkzeug.exceptions.BadRequestKeyError as e:
|
||||
app.logger.warning(
|
||||
"No signature found for this print, using default signature.", str(e)
|
||||
"No signature found for this print, using default signature. : %s", str(e)
|
||||
)
|
||||
sign = configuration_file["defaults"]["signature"]
|
||||
try:
|
||||
@@ -272,51 +266,38 @@ def api_print_sms():
|
||||
@limiter.limit("6/minute", override_defaults=False)
|
||||
def api_print_image():
|
||||
"""Prints an image on a printer"""
|
||||
app.logger.debug("Printing an image")
|
||||
app.logger.debug("Printing an image via API")
|
||||
|
||||
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)
|
||||
"No signature found for this print, using default signature. %s", 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"))
|
||||
# check if the post request has the file part
|
||||
if "img" not in request.files:
|
||||
app.logger.error("Whoops, no images submitted.")
|
||||
return "No image submitted", 400
|
||||
|
||||
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"))
|
||||
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 !")
|
||||
return "Submitted file has no filename !", 400
|
||||
|
||||
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"))
|
||||
try:
|
||||
app.logger.debug("Sending the image to the printer.")
|
||||
web.print_image(file, sign)
|
||||
except Exception as e:
|
||||
return str(e), 500
|
||||
|
||||
else:
|
||||
app.logger.error("Method not allowed")
|
||||
flash("Method not allowed")
|
||||
return redirect(url_for("index"))
|
||||
return "OK", 200
|
||||
|
||||
flash("Picture printed ! ")
|
||||
return redirect(url_for("index"))
|
||||
|
||||
|
||||
@app.route("/api/camera/picture")
|
||||
@app.route("/api/camera/picture", methods=["GET"])
|
||||
def camera_picture():
|
||||
"""Returns a picture taken by the camera"""
|
||||
if RASPBERRY_PI_CONNECTED:
|
||||
|
||||
Reference in New Issue
Block a user