From 9ac49baf951ffb362ec566c3674fde3caf2697ac Mon Sep 17 00:00:00 2001 From: Javiercerna Date: Wed, 6 Oct 2021 20:58:03 +0200 Subject: [PATCH 1/2] Set up JSONSupportedToolbar (modified Flask-DebugToolbar) --- config.py | 14 +++++++++----- project/__init__.py | 29 +++++++++++++++++++++++++++++ requirements.txt | 1 + 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/config.py b/config.py index 663d9a9..41cb3bd 100644 --- a/config.py +++ b/config.py @@ -5,12 +5,16 @@ env = "prod" if env == "local": - print("Using release configuration...") + print("Using local configuration...") SQLALCHEMY_DATABASE_URI = os.environ.get("SQLALCHEMY_DATABASE_URI_OPEN") SQLALCHEMY_TRACK_MODIFICATIONS = False - BASE_URL = 'http://0.0.0.0:5000/' -elif env == 'prod': - print('Using release configuration...') - SQLALCHEMY_DATABASE_URI = os.environ.get('SQLALCHEMY_DATABASE_URI_OPEN') + BASE_URL = "http://0.0.0.0:5000/" + DEBUG = True + SECRET_KEY = "my-secret-key" +elif env == "prod": + print("Using release configuration...") + SQLALCHEMY_DATABASE_URI = os.environ.get("SQLALCHEMY_DATABASE_URI_OPEN") SQLALCHEMY_TRACK_MODIFICATIONS = False BASE_URL = "https://openqairamapnapi.qairadrones.com/" + DEBUG = False + SECRET_KEY = "my-secret-key" # TODO: Replace with a more secure key, and don't push it to Github diff --git a/project/__init__.py b/project/__init__.py index 9c39ba9..5d4374d 100644 --- a/project/__init__.py +++ b/project/__init__.py @@ -1,8 +1,35 @@ +import flask from flask import Flask from flask_cors import CORS, cross_origin +from flask_debugtoolbar import DebugToolbarExtension from flask_socketio import SocketIO from flask_sqlalchemy import SQLAlchemy + +class JSONSupportedToolbar: + def __init__(self, app): + @app.after_request + def after_request(response): + should_modify_response = ( + response.mimetype == "application/json" + and flask.request.args.get("debug") == "true" + ) + + if not should_modify_response: + return response + + html_content = flask.render_template_string( + "
{{ response }}
", + response=response.data.decode("utf-8"), + ) + + return app.process_response( + flask.make_response(html_content, response.status_code) + ) + + DebugToolbarExtension(app) + + # Config app = Flask(__name__) app.config.from_object("config") @@ -11,6 +38,8 @@ # Extensions db = SQLAlchemy(app) +if app.debug: + toolbar = JSONSupportedToolbar(app) import project.database.models as models from project.database.models import (AirQualityMeasurement, Company, diff --git a/requirements.txt b/requirements.txt index 95059d0..1a64605 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,6 +7,7 @@ chardet==3.0.4 click==7.1.2 Flask==1.1.2 Flask-Cors==3.0.8 +Flask-DebugToolbar==0.11.0 Flask-SocketIO==4.3.0 Flask-SQLAlchemy==2.4.3 future==0.18.2 From f69004ed6d5624cdd81794adc5461d1a8fc4f57d Mon Sep 17 00:00:00 2001 From: Javiercerna Date: Wed, 6 Oct 2021 21:16:30 +0200 Subject: [PATCH 2/2] WIP: Switch string API responses to JSON --- project/main/business/qhawax.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/main/business/qhawax.py b/project/main/business/qhawax.py index d25e92c..08238be 100644 --- a/project/main/business/qhawax.py +++ b/project/main/business/qhawax.py @@ -75,7 +75,7 @@ def getQhawaxStatus(): name = request.args.get("name") try: return ( - str(same_helper.getQhawaxStatus(name)) + make_response(jsonify(same_helper.getQhawaxStatus(name))) if (same_helper.getQhawaxStatus(name) != None) else make_response( {"Warning": "qHAWAX name has not been found"}, 400