Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flask Debug Toolbar #120

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is my first question, @GersonMontesinos. This secret key shouldn't be written here even if it's open source, for security reasons. One alternative would be to do this:

Suggested change
SECRET_KEY = "my-secret-key" # TODO: Replace with a more secure key, and don't push it to Github
SECRET_KEY = os.environ.get("SECRET_KEY")

But then in your production environment you would need to set this environment variable. How would you like to proceed?

29 changes: 29 additions & 0 deletions project/__init__.py
Original file line number Diff line number Diff line change
@@ -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(
"<html><body><pre>{{ response }}</pre></body></html>",
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")
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion project/main/business/qhawax.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Copy link
Author

@Javiercerna Javiercerna Oct 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is my second question, @GersonMontesinos. If I keep these endpoints as they are, they return a str, which will then become a HTML without any <body> tags. The toolbar can only be used for HTML responses with a <body> tag or with JSON responses (now that I added support for them).

There are 3 options here:

  1. Do nothing, and you will change this later at some point. The toolbar won't work for these endpoints until you fix it.
  2. Change it to a JSON string, with the code that I wrote here.
  3. Change it to a "valid" HTML, by including the missing tags. It would be something like this:
Suggested change
make_response(jsonify(same_helper.getQhawaxStatus(name)))
<html><body><pre>{}</pre></body></html>'.format(
same_helper.getQhawaxStatus(name)
)

For both options 2 and 3, I would have to change it for all your non-JSON responses, which could take some time. And, more important, your API customers (even the frontend) might need to modify their code in case they are expecting a str() instead of a JSON str or a "valid" HTML (with the extra tags).

What would you like to do? 😄

if (same_helper.getQhawaxStatus(name) != None)
else make_response(
{"Warning": "qHAWAX name has not been found"}, 400
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down