Code review and sanitation. More understanding of concepts
This commit is contained in:
+14
-12
@@ -2,7 +2,10 @@ import pymongo
|
|||||||
from flask import Flask, request, render_template, url_for, redirect
|
from flask import Flask, request, render_template, url_for, redirect
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
myclient = pymongo.MongoClient("mongodb://admin:pass@localhost:27017")
|
||||||
|
mydb = myclient["groclist_db"]
|
||||||
|
mycol_users = mydb["groclist_users"]
|
||||||
|
username=""
|
||||||
|
|
||||||
@app.route("/", methods=['GET', 'POST'])
|
@app.route("/", methods=['GET', 'POST'])
|
||||||
def groclist_home():
|
def groclist_home():
|
||||||
@@ -15,29 +18,28 @@ def groclist_home():
|
|||||||
|
|
||||||
return render_template('index.html',error=error)
|
return render_template('index.html',error=error)
|
||||||
|
|
||||||
@app.route("/welcome")
|
|
||||||
def groclist_welcome():
|
|
||||||
return render_template("welcome.html")
|
|
||||||
|
|
||||||
@app.route("/login", methods=['GET', 'POST'])
|
@app.route("/login", methods=['GET', 'POST'])
|
||||||
def groclist_login():
|
def groclist_login():
|
||||||
error = None
|
error = None
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
myclient = pymongo.MongoClient("mongodb://admin:pass@localhost:27017")
|
|
||||||
mydb = myclient["groclist_db"]
|
|
||||||
mycol = mydb["groclist_users"]
|
|
||||||
username = request.form['username']
|
username = request.form['username']
|
||||||
password = request.form['password']
|
password = request.form['password']
|
||||||
name = ""
|
if mycol_users.find_one({"username": username, "password": password}):
|
||||||
if mycol.find_one({"username": username, "password": password}):
|
|
||||||
# for user_info in mycol.find({}, {"name": 0}):
|
|
||||||
# print("Welkom", user_info)
|
|
||||||
return redirect(url_for('groclist_welcome'))
|
return redirect(url_for('groclist_welcome'))
|
||||||
else:
|
else:
|
||||||
error = 'Username of wachtwoord onbekend. Probeer opnieuw'
|
error = 'Username of wachtwoord onbekend. Probeer opnieuw'
|
||||||
|
|
||||||
return render_template("login.html", error=error)
|
return render_template("login.html", error=error)
|
||||||
|
|
||||||
|
@app.route("/welcome", methods=['GET', 'POST'])
|
||||||
|
def groclist_welcome():
|
||||||
|
if request.method == 'GET':
|
||||||
|
userid = mycol_users.find_one({"username": username}, {"_id": 1})
|
||||||
|
print(userid)
|
||||||
|
return render_template("welcome.html", userid=userid)
|
||||||
|
else:
|
||||||
|
error = "Userid is niet bekend. Database defect"
|
||||||
|
return render_template("index.html", error=error)
|
||||||
|
|
||||||
app.add_url_rule("/welcome", "welcome", groclist_welcome)
|
app.add_url_rule("/welcome", "welcome", groclist_welcome)
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
import pymongo
|
||||||
|
from flask import Flask, request, render_template, redirect, url_for
|
||||||
|
|
||||||
|
# initialize connection to database
|
||||||
|
app = Flask(__name__)
|
||||||
|
config = {
|
||||||
|
"username": "admin",
|
||||||
|
"password": "pass",
|
||||||
|
"server": "localhost:27017",
|
||||||
|
}
|
||||||
|
connector = "mongodb://{}:{}@{}".format(config["username"],config["password"], config["server"]
|
||||||
|
client = pymongo.MongoClient(connector)
|
||||||
|
db = client["groclist"]
|
||||||
|
|
||||||
|
@app.route("/")
|
||||||
|
def main():
|
||||||
|
return render_template("login.html")
|
||||||
|
|
||||||
|
@app.route("/login", methods=["POST"])
|
||||||
|
def login()
|
||||||
|
error = None
|
||||||
|
username = request.form["username"]
|
||||||
|
password = request.form["password"]
|
||||||
|
if db.cred.find_one({"username": username, "password": password})
|
||||||
|
auth_user_id = db.cred.find_one({"username": username},{"_id", 1})
|
||||||
|
return redirect(url_for("welcome", id=auth_user_id["_id"]))
|
||||||
|
else:
|
||||||
|
error = "Usernaam of wachtwoord onbekend, probeer opnieuw"
|
||||||
|
return render_template("login.html", error=error)
|
||||||
|
|
||||||
|
@app.route("/welcome")
|
||||||
|
def welcome(id)
|
||||||
|
|
||||||
@@ -6,8 +6,9 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<h1>Welcome to GrocList<h1>
|
<h2>Welcome to GrocList<h2>
|
||||||
<br>
|
<br>
|
||||||
|
Hello userid: {{ userid }}
|
||||||
<p>Click <a href="/">here</a> to go home. </p>
|
<p>Click <a href="/">here</a> to go home. </p>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
Reference in New Issue
Block a user