diff --git a/backend/add_user.py b/backend/add_user.py index 4a66feb..255cc81 100644 --- a/backend/add_user.py +++ b/backend/add_user.py @@ -2,21 +2,32 @@ import pymongo import getpass import uuid -myclient = pymongo.MongoClient("mongodb://admin:pass@localhost:27017/") +# initialize connection to database +config = { + "username": "admin", + "password": "pass", + "server": "localhost:27017", +} +connector = "mongodb://{}:{}@{}".format(config["username"],config["password"], config["server"]) +client = pymongo.MongoClient(connector) +db = client["groclist"] + username = input("Username: ") password = getpass.getpass("Password: ") uuid = str(uuid.uuid4()) email = input("Email-adres: ") full_name = input("Volledige naam: ") +list_id = "" -mydb = myclient["groclist_db"] -mycol = mydb["groclist_users"] +user_cred = {"username": username, "password": password, "uuid": uuid} +user_info = {"uuid":uuid, "email": email, "name": full_name ,"listid": list_id} -mydict = {"username": username, "password": password, "groclist_uuid": uuid, "email": email, "name": full_name} -if mycol.find_one({"username": username}): + +if db.user_cred.find_one({"username": username}): print("Username already exists\n") exit() else: - x = mycol.insert_one(mydict) + cred_record = db.user_cred.insert_one(user_cred) + info_record = db.user_info.insert_one(user_info) print("User created") diff --git a/backend/get_doc.py b/backend/get_doc.py new file mode 100644 index 0000000..b3c7eb7 --- /dev/null +++ b/backend/get_doc.py @@ -0,0 +1,21 @@ +import uuid +import pymongo +import getpass + +config = { + "username": "admin", + "password": "pass", + "server": "localhost:27017", +} +connector = "mongodb://{}:{}@{}".format(config["username"],config["password"], config["server"]) +client = pymongo.MongoClient(connector) +db = client["groclist"] + + +username = input("Username :") +#password = getpass.getpass("Password: ") + +userid = db.user_cred.find_one({"username": username}) +email = db.user_info.find_one({"uuid": userid{"uuid": 1} ,"name": 1, "email": 1}) + +print (userid, email) diff --git a/frontend/app.py b/frontend/app.py index 0f4b93c..cad0262 100644 --- a/frontend/app.py +++ b/frontend/app.py @@ -2,7 +2,10 @@ import pymongo from flask import Flask, request, render_template, url_for, redirect 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']) def groclist_home(): @@ -15,26 +18,28 @@ def groclist_home(): return render_template('index.html',error=error) -@app.route("/welcome") -def groclist_welcome(): - return render_template("welcome.html") - @app.route("/login", methods=['GET', 'POST']) def groclist_login(): error = None if request.method == 'POST': - myclient = pymongo.MongoClient("mongodb://admin:pass@localhost:27017") - mydb = myclient["groclist_db"] - mycol = mydb["groclist_users"] username = request.form['username'] password = request.form['password'] - if mycol.find_one({"username": username, "password": password}): + if mycol_users.find_one({"username": username, "password": password}): return redirect(url_for('groclist_welcome')) else: error = 'Username of wachtwoord onbekend. Probeer opnieuw' 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) diff --git a/frontend/groclist.py b/frontend/groclist.py new file mode 100644 index 0000000..71ab2b0 --- /dev/null +++ b/frontend/groclist.py @@ -0,0 +1,47 @@ +import pymongo +from flask import Flask, request, render_template, redirect, url_for, abort + +# 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("index.html") + +@app.route("/login", methods=["GET","POST"]) +def login(): + error = None + if request.method == "POST": + username = request.form["username"] + password = request.form["password"] + if db.user_cred.find_one({"username": username, "password": password}): + auth_user_id = db.user_cred.find_one({"username": username},{"uuid": 1}) + if auth_user_id: + app.logger.info(auth_user_id) + return render_template("welcome.html", id=auth_user_id["uuid"]) + else: + return render_template("404.html") + else: + error = "Usernaam of wachtwoord onbekend, probeer opnieuw" + return render_template("login.html", error=error) + +@app.route("/welcome/", methods=['GET']) +def welcome(id): + print("Hello: ", id) + +@app.errorhandler(404) +def not_found(error): + app.logger.info(error) + return render_template("404.html"), 404 + +# start server with run method +if __name__ == "__main__": + app.run(debug=True) diff --git a/frontend/templates/404.html b/frontend/templates/404.html new file mode 100644 index 0000000..8fca942 --- /dev/null +++ b/frontend/templates/404.html @@ -0,0 +1,4 @@ +{% extends "base.html" %} +{% block content %} +

not found

+{% endblock %} \ No newline at end of file diff --git a/frontend/templates/index.html b/frontend/templates/index.html index e64d639..41a68be 100644 --- a/frontend/templates/index.html +++ b/frontend/templates/index.html @@ -11,7 +11,7 @@

Welkom bij Groclist

-
- + +
{% endblock %} diff --git a/frontend/templates/login.html b/frontend/templates/login.html index 3ada399..03938fe 100644 --- a/frontend/templates/login.html +++ b/frontend/templates/login.html @@ -2,11 +2,11 @@ Groclist - login page - +
-

Please login

+

Please login


-

Welcome to GrocList

+

Welcome to GrocList


+ Hello userid: {{ userid }}

Click here to go home.