From 09ff3d0faf84ef02045dac330589c66bfb163346 Mon Sep 17 00:00:00 2001 From: gboeker Date: Thu, 29 Feb 2024 12:55:23 -0500 Subject: [PATCH 1/5] test add auth --- app.py | 23 ++++++++++++++++++----- templates/decks.html | 4 ++-- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/app.py b/app.py index 24f5437..f366065 100644 --- a/app.py +++ b/app.py @@ -39,10 +39,8 @@ def allDecks(username): else: if (not current_user.is_authenticated or current_user.id != username): return redirect('/login') - else: - user = db.users.find_one({'user_id': current_user.id}) - return render_template('decks.html', mainDecks = user['mainDecks'], personalDecks = user['personalDecks']) - + user = db.users.find_one({'user_id': current_user.id}) + return render_template('decks.html', mainDecks = user['mainDecks'], personalDecks = user['personalDecks']) @app.route("//") def displayDeck(username, deckTitle): @@ -53,7 +51,22 @@ def displayDeck(username, deckTitle): # shuffle deck random.shuffle(cardList) return render_template('card.html', deckTitle=deckTitle, username=username, cardList=cardList) - return "temp" + else: + if (not current_user.is_authenticated or current_user.id != username): + return redirect('/login') + + currentDeck = db.users.find_one({"title": deckTitle}) + # if the deck is not found in the users deck, look for in main + if not currentDeck: + currentDeck = db.decks.find_one({"title": deckTitle}) + + print(currentDeck) + + cardList = currentDeck['cards'] + # shuffle deck + random.shuffle(cardList) + return render_template('card.html', deckTitle=deckTitle, username=username, cardList=cardList) + @app.route("//create", methods=["POST"]) def createDeck(username): diff --git a/templates/decks.html b/templates/decks.html index 5e8735f..cdb8762 100644 --- a/templates/decks.html +++ b/templates/decks.html @@ -3,7 +3,7 @@ {% block container %} {% for deck in mainDecks %} - + @@ -11,7 +11,7 @@

{{ deck.title }}

{% endfor %} {% for deck in personalDecks %} -
+ From 008cd7ce8a16579413115431803cc776ee36da05 Mon Sep 17 00:00:00 2001 From: gboeker Date: Thu, 29 Feb 2024 14:30:53 -0500 Subject: [PATCH 2/5] show user personal deck --- app.py | 10 ++++++---- authentication.py | 2 +- templates/decks.html | 28 +++++++++++++++------------- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/app.py b/app.py index 721556e..898a75a 100644 --- a/app.py +++ b/app.py @@ -35,14 +35,14 @@ def signup(): def allDecks(username): if username == "guest": mainDecks = db.decks.find({}) - return render_template('decks.html', mainDecks=mainDecks) + return render_template('decks.html', mainDecks=mainDecks, isAuth=False) else: if (not current_user.is_authenticated or current_user.id != username): return redirect('/login') else: user = db.users.find_one({'user_id': current_user.id}) mainDecks = db.decks.find({}) - return render_template('decks.html', mainDecks = mainDecks, personalDecks = user['personalDecks']) + return render_template('decks.html', username=username, isAuth=True, mainDecks = mainDecks, personalDecks = user['personalDecks']) @app.route("//") def displayDeck(username, deckTitle): @@ -72,10 +72,12 @@ def displayDeck(username, deckTitle): @app.route("//create", methods=["POST"]) def createDeck(username): - # would need to first find user in db, but not set up yet + if (not current_user.is_authenticated or current_user.id != username): + return redirect('/login') + # now is authenticated title = request.form["title"] newDeck = {"title": title, "cards": []} - db.decks.insert_one(newDeck) + db.users.update_one({"user_id": username}, {"$push": {"personalDecks": newDeck}}) # would rendirect to template for Cards return "created deck" diff --git a/authentication.py b/authentication.py index 593f485..9dfbb59 100644 --- a/authentication.py +++ b/authentication.py @@ -63,7 +63,7 @@ def auth_signup(): return render_template('signup.html', username_taken=False, passwords_dont_match=True) else: user = User(user_id, generate_password_hash(password)) - db['users'].insert_one({"user_id": user.id, "password": user.password, 'personalDecks': []}) + db['users'].insert_one({"user_id": user.id, "password": user.password, "personalDecks": []}) login_user(user) # TODO: Redirect to the appropriate page for the logged in user return redirect('/' + user_id + '/decks') diff --git a/templates/decks.html b/templates/decks.html index 4f74286..5b3924d 100644 --- a/templates/decks.html +++ b/templates/decks.html @@ -18,20 +18,22 @@

{{ deck.title }}

{% endfor %} - + {% if isAuth %} + - + + {% endif %} + {% endif %} + {% endblock %} From 326a86bd8a4be5f3f1ddc99d14e8c32575241092 Mon Sep 17 00:00:00 2001 From: gboeker Date: Thu, 29 Feb 2024 19:50:20 -0500 Subject: [PATCH 4/5] show cards from guest --- app.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/app.py b/app.py index a68c163..aa45a9c 100644 --- a/app.py +++ b/app.py @@ -37,12 +37,12 @@ def allDecks(username): mainDecks = db.decks.find({}) return render_template('decks.html', mainDecks=mainDecks, isAuth=False) else: + # authenticate user if (not current_user.is_authenticated or current_user.id != username): return redirect('/login') - else: - user = db.users.find_one({'user_id': current_user.id}) - mainDecks = db.decks.find({}) - return render_template('decks.html', username=username, isAuth=True, mainDecks=mainDecks, personalDecks=user['personalDecks']) + user = db.users.find_one({'user_id': current_user.id}) + mainDecks = db.decks.find({}) + return render_template('decks.html', username=username, isAuth=True, mainDecks=mainDecks, personalDecks=user['personalDecks']) @app.route("//") def displayDeck(username, deckTitle): @@ -54,22 +54,26 @@ def displayDeck(username, deckTitle): random.shuffle(cardList) return render_template('card.html', deckTitle=deckTitle, username=username, cardList=cardList) else: + # authenticate user if (not current_user.is_authenticated or current_user.id != username): return redirect('/login') + # TODO: id is not being generated, some problem + currentDeck = db.users.find_one({"user_id": username, "personalDecks.title": deckTitle}, {"personalDecks.$": 1}).get("personalDecks")[0] - currentDeck = db.users.find_one({"title": deckTitle}) + # if the deck is not found in the users deck, look for in main if not currentDeck: currentDeck = db.decks.find_one({"title": deckTitle}) - print(currentDeck) + print("current deck", currentDeck) + print("better deck", db.decks.find_one({"title": "lalalala"})) cardList = currentDeck['cards'] # shuffle deck random.shuffle(cardList) return render_template('card.html', deckTitle=deckTitle, username=username, cardList=cardList) - +# TODO: change createDeck to addDeck for naming consistency @app.route("//create", methods=["POST"]) def createDeck(username): # authenticate user @@ -77,7 +81,10 @@ def createDeck(username): return redirect('/login') title = request.form["title"] newDeck = {"title": title, "cards": []} + db.users.update_one({"user_id": username}, {"$push": {"personalDecks": newDeck}}) + # db.users.update_one({"user_id": username}, {"$set": {"personalDecks." + title: []}}) #attempt at changing personalDeck to a set of dictionaries + # would rendirect to template for Cards return "created deck" From 410369c2494c03d9ff6dfc474fbe535efb01d784 Mon Sep 17 00:00:00 2001 From: gboeker Date: Thu, 29 Feb 2024 20:16:05 -0500 Subject: [PATCH 5/5] cleanup --- app.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/app.py b/app.py index aa45a9c..3b7cc42 100644 --- a/app.py +++ b/app.py @@ -59,15 +59,9 @@ def displayDeck(username, deckTitle): return redirect('/login') # TODO: id is not being generated, some problem currentDeck = db.users.find_one({"user_id": username, "personalDecks.title": deckTitle}, {"personalDecks.$": 1}).get("personalDecks")[0] - - # if the deck is not found in the users deck, look for in main if not currentDeck: currentDeck = db.decks.find_one({"title": deckTitle}) - - print("current deck", currentDeck) - print("better deck", db.decks.find_one({"title": "lalalala"})) - cardList = currentDeck['cards'] # shuffle deck random.shuffle(cardList) @@ -81,10 +75,7 @@ def createDeck(username): return redirect('/login') title = request.form["title"] newDeck = {"title": title, "cards": []} - db.users.update_one({"user_id": username}, {"$push": {"personalDecks": newDeck}}) - # db.users.update_one({"user_id": username}, {"$set": {"personalDecks." + title: []}}) #attempt at changing personalDeck to a set of dictionaries - # would rendirect to template for Cards return "created deck"