From 1ba261f4b3d96ed3d7fc4965ce183395237eff76 Mon Sep 17 00:00:00 2001 From: Adam Dodman Date: Thu, 5 Oct 2017 02:08:32 +0100 Subject: [PATCH] Refactor to use function to set page variables --- main.py | 23 +++++++++++++++++------ templates/add_quote.html | 2 +- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/main.py b/main.py index 20c3aca..dab29fc 100644 --- a/main.py +++ b/main.py @@ -28,6 +28,19 @@ def get_userdb(): userdb = mysql_do("SELECT * FROM Users") return userdb +def gen_page(template, data=None): + # This fire if we need to pass something into templating + if data: + print("DATA") + pp.pprint(data) + if 'username' in session: + return render_template(template, user=session["username"], data=data) + else: + return render_template(template, data=data) + + if 'username' in session: + return render_template(template, user=session["username"]) + return render_template(template) # Load User Table into variable def mysql_do(query): @@ -75,20 +88,18 @@ def do_user_login(user, password): @app.route("/") def index(): - if 'username' in session: - return render_template("index.html", user=session["username"]) - return render_template("index.html") + return gen_page("index.html") @app.route("/quotes") def quoutepage(): retdata = mysql_do("SELECT * FROM Quotes ORDER BY ID DESC") - return render_template("quote_view.html", data=retdata) + return gen_page("quote_view.html", retdata) @app.route("/login", methods=['GET', 'POST']) def login(): if request.method == 'POST': return do_user_login(request.form['username'], request.form['pw']) - return render_template("login.html") + return gen_page("login.html") @app.route("/addquote", methods=['GET','POST']) def addquote(): @@ -149,7 +160,7 @@ def addquote(): except KeyError: flash("INFO: Please login first.","info") return redirect(url_for("login")) - return render_template("add_quote.html", users=get_userdb()) + return gen_page("add_quote.html", get_userdb()) @app.route("/logout") diff --git a/templates/add_quote.html b/templates/add_quote.html index bbc00cb..ba7bfef 100644 --- a/templates/add_quote.html +++ b/templates/add_quote.html @@ -14,7 +14,7 @@