mirror of
https://github.com/Adam-Ant/QuotesDB
synced 2024-12-20 11:34:35 +00:00
Refactor to use function to set page variables
This commit is contained in:
parent
f2e490475d
commit
1ba261f4b3
23
main.py
23
main.py
@ -28,6 +28,19 @@ def get_userdb():
|
|||||||
userdb = mysql_do("SELECT * FROM Users")
|
userdb = mysql_do("SELECT * FROM Users")
|
||||||
return userdb
|
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
|
# Load User Table into variable
|
||||||
def mysql_do(query):
|
def mysql_do(query):
|
||||||
@ -75,20 +88,18 @@ def do_user_login(user, password):
|
|||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def index():
|
def index():
|
||||||
if 'username' in session:
|
return gen_page("index.html")
|
||||||
return render_template("index.html", user=session["username"])
|
|
||||||
return render_template("index.html")
|
|
||||||
|
|
||||||
@app.route("/quotes")
|
@app.route("/quotes")
|
||||||
def quoutepage():
|
def quoutepage():
|
||||||
retdata = mysql_do("SELECT * FROM Quotes ORDER BY ID DESC")
|
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'])
|
@app.route("/login", methods=['GET', 'POST'])
|
||||||
def login():
|
def login():
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
return do_user_login(request.form['username'], request.form['pw'])
|
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'])
|
@app.route("/addquote", methods=['GET','POST'])
|
||||||
def addquote():
|
def addquote():
|
||||||
@ -149,7 +160,7 @@ def addquote():
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
flash("INFO: Please login first.","info")
|
flash("INFO: Please login first.","info")
|
||||||
return redirect(url_for("login"))
|
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")
|
@app.route("/logout")
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="user">User:</label>
|
<label for="user">User:</label>
|
||||||
<select class="form-control" id="user" name="user">
|
<select class="form-control" id="user" name="user">
|
||||||
{% for user in users %}
|
{% for user in data %}
|
||||||
<option value="{{ user[0] }}">{{ user[1] }}</option>
|
<option value="{{ user[0] }}">{{ user[1] }}</option>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</select>
|
</select>
|
||||||
|
Loading…
Reference in New Issue
Block a user