diff --git a/src/main.py b/src/main.py index 6b3cf57..c3f409b 100644 --- a/src/main.py +++ b/src/main.py @@ -116,15 +116,16 @@ def api_print_sms(): txt = request.form["txt"] sign = request.form["signature"] except Exception as e: - flash(e,'error') - redirect(url_for('index')) + app.logger.error(str(e) + " - Whoops, no forms submitted or missing signature.") + return jsonify({'message': 'Error getting the information from the form :' + e}), 500 try: web.print_sms(txt,sign) 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']) @limiter.limit("6/minute", override_defaults=False) @@ -134,43 +135,41 @@ def api_print_image(): try: sign = request.form["signature"] except Exception as e: - flash(str(e),'error') 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': # check if the post request has the file part try: 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 ?") - return redirect(url_for("index")) + return jsonify({'message': 'No file found. Did you use the good form ?'}), 500 else: file = request.files['img'] except Exception as e: - if sign is not None and photo is not None: - pass - else: - flash(str(e), 'error') - app.logger.error("Couldn't get an image nor signature : " + str(e)) + app.logger.error('Error getting the files :' + e) + return jsonify({'message': 'Error getting the files :' + e}), 500 # 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('No file submitted, please select a file','error') - return redirect(url_for("index")) + return jsonify({'message': "Submitted file has no filename !" + e}), 500 try: app.logger.debug("Sending the image to the printer.") web.print_image(file, sign) except Exception as e: - pass - else: - flash('Cannot access to page with this method.','error') - app.logger.debug('Bad access type to this API.') + app.logger.error("The image could not be printed : " + e ) + return jsonify({'message': "The image could not be printed :" + e}), 500 + + 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') @limiter.limit("1/second", override_defaults=False) @@ -187,10 +186,12 @@ def logout_page(): @app.errorhandler(429) def ratelimit_handler(e): flash("Rate limit reached, please slow down :) ( Currently at "+ e.description + ")", 'error') + app.logger.debug('Rate limit reached ' + e) return redirect(url_for("index")) @app.route("/ping") @limiter.exempt def ping(): flash("🏓 Pong !",'info') + app.logger.debug('🏓 Pong !') return redirect(url_for("index"))