Skip to content

Commit

Permalink
add error handling for bad database conn
Browse files Browse the repository at this point in the history
  • Loading branch information
maggi373 committed Sep 5, 2024
1 parent fcc0cff commit 549327e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
7 changes: 4 additions & 3 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
__version__ = solderpy_version

app: Flask = Flask(__name__)
if not management_only:
if management_only == False or Database.is_setup() != 2:
app.register_blueprint(api)
if not api_only:
app.register_blueprint(alogin)
if migratetechnic is True or new_user is True or Database.is_setup() == 0:
if migratetechnic is True or new_user is True or Database.is_setup() == 0 or Database.is_setup() == 2:
app.register_blueprint(asetup)
if Database.is_setup() != 2:
# Note that asite must be after setup
app.register_blueprint(alogin)
app.register_blueprint(asite)

app.secret_key = secrets.token_hex()
Expand Down
34 changes: 20 additions & 14 deletions asetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@

asetup = Blueprint("asetup", __name__)

if migratetechnic is True or new_user is True or Database.is_setup() == 0:
Database.create_session_table()
if Database.is_setup() != 2:
if migratetechnic is True or new_user is True or Database.is_setup() == 0:
Database.create_session_table()

@asetup.route("/setup", methods=["GET"])
def setup():
if Database.is_setup() == 2:
flash("An error occurred whilst trying to check database connection", "error")
return render_template("setup.html")
if Database.is_setup() == 0:
Database.create_tables()
if new_user or not User.any_user_exists():
Expand All @@ -24,15 +28,17 @@ def setup():

@asetup.route("/setup", methods=["POST"])
def setup_creation():
if new_user or not User.any_user_exists():
if request.form["setupemail"] is None:
print("setup failed")
flash("setup failed due to missing email", "error")
return render_template("setup.html", failed=True)
if request.form["setuppassword"] is None:
print("setup failed")
flash("setup failed due to missing password", "error")
return render_template("setup.html", failed=True)
User.new(request.form["setupemail"], request.form["setupemail"],
request.form["setuppassword"], request.remote_addr, '1')
return redirect(url_for('asite.index'))
if Database.is_setup() != 2:
if new_user or not User.any_user_exists():
if request.form["setupemail"] is None:
print("setup failed")
flash("setup failed due to missing email", "error")
return render_template("setup.html")
if request.form["setuppassword"] is None:
print("setup failed")
flash("setup failed due to missing password", "error")
return render_template("setup.html")
User.new(request.form["setupemail"], request.form["setupemail"],
request.form["setuppassword"], request.remote_addr, '1')
return redirect(url_for('asite.index'))
return render_template("setup.html")
3 changes: 2 additions & 1 deletion asite.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@

asite = Blueprint("asite", __name__)

Session.start_session_loop()
if Database.is_setup() != 2:
Session.start_session_loop()

## Allowed extensions to be uploaded
ALLOWED_EXTENSIONS = {'zip', 'jar'}
Expand Down
6 changes: 4 additions & 2 deletions models/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ def get_connection() -> connector.connection:
)
except Exception as e:
print("Error connecting to database", e)
return None
return conn

@staticmethod
def is_setup() -> bool:
def is_setup() -> int:
if Database.get_connection() is None:
return 2
conn = Database.get_connection()
cur = conn.cursor(dictionary=True)
sql = "SHOW TABLES"
Expand All @@ -41,7 +44,6 @@ def is_setup() -> bool:
cur.execute(sql)
except Exception as e:
ErrorPrinter.message("An error occurred whilst trying to check database setup", e)
flash("Unable to connect to database" + e, "error")
return 2
table = cur.fetchone()
print(table)
Expand Down

0 comments on commit 549327e

Please sign in to comment.