Change the return type to a JSON message for the API, add logging
This commit is contained in:
parent
866d89eb09
commit
abaf506d56
41
src/main.py
41
src/main.py
@ -116,15 +116,16 @@ def api_print_sms():
|
|||||||
txt = request.form["txt"]
|
txt = request.form["txt"]
|
||||||
sign = request.form["signature"]
|
sign = request.form["signature"]
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
flash(e,'error')
|
app.logger.error(str(e) + " - Whoops, no forms submitted or missing signature.")
|
||||||
redirect(url_for('index'))
|
return jsonify({'message': 'Error getting the information from the form :' + e}), 500
|
||||||
|
|
||||||
try:
|
try:
|
||||||
web.print_sms(txt,sign)
|
web.print_sms(txt,sign)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
pass
|
return jsonify({'message': 'Error printing the SMS:' + e}), 500
|
||||||
|
|
||||||
return redirect(url_for('index'))
|
|
||||||
|
return jsonify({'message': 'Message printed'}), 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)
|
||||||
@ -134,43 +135,41 @@ def api_print_image():
|
|||||||
try:
|
try:
|
||||||
sign = request.form["signature"]
|
sign = request.form["signature"]
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
flash(str(e),'error')
|
|
||||||
app.logger.error(str(e) + " - Whoops, no forms submitted or missing signature.")
|
app.logger.error(str(e) + " - Whoops, no forms submitted or missing signature.")
|
||||||
return redirect(url_for('index'))
|
return jsonify({'message': 'Error getting the information from the form :' + e}), 500
|
||||||
|
|
||||||
|
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
# check if the post request has the file part
|
# check if the post request has the file part
|
||||||
try:
|
try:
|
||||||
if 'img' not in request.files:
|
if 'img' not in request.files:
|
||||||
flash('No file found. Did you use the good form ?', 'error')
|
|
||||||
app.logger.error("No file found. Did you use the good form ?")
|
app.logger.error("No file found. Did you use the good form ?")
|
||||||
return redirect(url_for("index"))
|
return jsonify({'message': 'No file found. Did you use the good form ?'}), 500
|
||||||
else:
|
else:
|
||||||
file = request.files['img']
|
file = request.files['img']
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if sign is not None and photo is not None:
|
app.logger.error('Error getting the files :' + e)
|
||||||
pass
|
return jsonify({'message': 'Error getting the files :' + e}), 500
|
||||||
else:
|
|
||||||
flash(str(e), 'error')
|
|
||||||
app.logger.error("Couldn't get an image nor signature : " + str(e))
|
|
||||||
|
|
||||||
# If the user does not select a file, the browser submits an
|
# If the user does not select a file, the browser submits an
|
||||||
# empty file without a filename.
|
# empty file without a filename.
|
||||||
if file.filename == '':
|
if file.filename == '':
|
||||||
app.logger.error("Submitted file has no filename !")
|
app.logger.error("Submitted file has no filename !")
|
||||||
flash('No file submitted, please select a file','error')
|
return jsonify({'message': "Submitted file has no filename !" + e}), 500
|
||||||
return redirect(url_for("index"))
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
app.logger.debug("Sending the image to the printer.")
|
app.logger.debug("Sending the image to the printer.")
|
||||||
web.print_image(file, sign)
|
web.print_image(file, sign)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
pass
|
app.logger.error("The image could not be printed : " + e )
|
||||||
else:
|
return jsonify({'message': "The image could not be printed :" + e}), 500
|
||||||
flash('Cannot access to page with this method.','error')
|
|
||||||
app.logger.debug('Bad access type to this API.')
|
else:
|
||||||
|
return jsonify({'message': "Method Not Allowed, please POST"}), 403
|
||||||
|
app.logger.debug('Bad access type to this API, please POST')
|
||||||
|
|
||||||
|
return jsonify({'message': 'Message printed'}), 200
|
||||||
|
|
||||||
return redirect(url_for("index"))
|
|
||||||
|
|
||||||
@app.route('/login')
|
@app.route('/login')
|
||||||
@limiter.limit("1/second", override_defaults=False)
|
@limiter.limit("1/second", override_defaults=False)
|
||||||
@ -187,10 +186,12 @@ def logout_page():
|
|||||||
@app.errorhandler(429)
|
@app.errorhandler(429)
|
||||||
def ratelimit_handler(e):
|
def ratelimit_handler(e):
|
||||||
flash("Rate limit reached, please slow down :) ( Currently at "+ e.description + ")", 'error')
|
flash("Rate limit reached, please slow down :) ( Currently at "+ e.description + ")", 'error')
|
||||||
|
app.logger.debug('Rate limit reached ' + e)
|
||||||
return redirect(url_for("index"))
|
return redirect(url_for("index"))
|
||||||
|
|
||||||
@app.route("/ping")
|
@app.route("/ping")
|
||||||
@limiter.exempt
|
@limiter.exempt
|
||||||
def ping():
|
def ping():
|
||||||
flash("🏓 Pong !",'info')
|
flash("🏓 Pong !",'info')
|
||||||
|
app.logger.debug('🏓 Pong !')
|
||||||
return redirect(url_for("index"))
|
return redirect(url_for("index"))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user