From 950f26510757a12a94009d00934d3765b07d511b Mon Sep 17 00:00:00 2001 From: gboeker Date: Fri, 1 Mar 2024 17:18:41 -0500 Subject: [PATCH 1/7] change redirect urls to url_for --- app.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index 3b7cc42..1a0ad2b 100644 --- a/app.py +++ b/app.py @@ -76,7 +76,8 @@ def createDeck(username): title = request.form["title"] newDeck = {"title": title, "cards": []} db.users.update_one({"user_id": username}, {"$push": {"personalDecks": newDeck}}) - # would rendirect to template for Cards + # would rendirect to decks + return redirect(url_for('allDecks', username=username)) return "created deck" @app.route("///add", methods=["POST"]) From a593a73a2243a27b2ca25acac8ba460ca20ad52d Mon Sep 17 00:00:00 2001 From: gboeker Date: Fri, 1 Mar 2024 17:57:02 -0500 Subject: [PATCH 2/7] added redirects for user routes --- app.py | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/app.py b/app.py index 1a0ad2b..b67219c 100644 --- a/app.py +++ b/app.py @@ -15,11 +15,11 @@ def home(): if request.method == 'POST': if 'login' in request.form: - return redirect('/login') + return redirect(url_for('login')) elif 'sign-up' in request.form: - return redirect('/signup') + return redirect(url_for('signup')) elif 'play-as-guest' in request.form: - return redirect('/guest/decks') + return redirect(url_for('allDecks', username='guest')) return render_template('start.html') # Handle authentication related stuff in authentication.py file @@ -39,7 +39,7 @@ def allDecks(username): else: # authenticate user if (not current_user.is_authenticated or current_user.id != username): - return redirect('/login') + return redirect(url_for('login')) 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']) @@ -56,7 +56,7 @@ def displayDeck(username, deckTitle): else: # authenticate user if (not current_user.is_authenticated or current_user.id != username): - return redirect('/login') + return redirect(url_for('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 @@ -72,61 +72,65 @@ def displayDeck(username, deckTitle): def createDeck(username): # authenticate user if (not current_user.is_authenticated or current_user.id != username): - return redirect('/login') + return redirect(url_for('login')) title = request.form["title"] newDeck = {"title": title, "cards": []} db.users.update_one({"user_id": username}, {"$push": {"personalDecks": newDeck}}) # would rendirect to decks + # TODO: is there a way to not have to refresh the page and add the new deck return redirect(url_for('allDecks', username=username)) - return "created deck" @app.route("///add", methods=["POST"]) def addCard(username, deckTitle): # authenticate user if (not current_user.is_authenticated or current_user.id != username): - return redirect('/login') + return redirect(url_for('login')) # would need to first find user in db, but not set up yet deck = db.decks.find_one({"title": deckTitle}) newCard = request.form["question"] deck["cards"].append(newCard) db.decks.update_one({"title": deckTitle}, {"$set": deck}) # would redirect to template for Cards - return "added card" + # TODO: is there a way to not have to refresh the page and show the added card + return redirect(url_for('displayDeck', username=username, deckTitle=deckTitle)) @app.route("////edit", methods=["POST"]) def editCard(username, deckTitle, cardIndex): # authenticate user if (not current_user.is_authenticated or current_user.id != username): - return redirect('/login') + return redirect(url_for('login')) # would need to first find user in db, but not set up yet deck = db.decks.find_one({"title": deckTitle}) newCard = request.form["question"] deck["cards"][int(cardIndex)] = newCard db.decks.update_one({"title": deckTitle}, {"$set": deck}) # would redirect to template for Cards - return "edited card" + # TODO: is there a way to not have to refresh the page and show the edited card + return redirect(url_for('displayDeck', username=username, deckTitle=deckTitle)) @app.route("////delete") def deleteCard(username, deckTitle, cardIndex): # authenticate user if (not current_user.is_authenticated or current_user.id != username): - return redirect('/login') + return redirect(url_for('login')) # would need to first find user in db, but not set up yet deck = db.decks.find_one({"title": deckTitle}) deck["cards"].pop(int(cardIndex)) db.decks.update_one({"title": deckTitle}, {"$set": deck}) # would redirect to template for Cards - return "deleted card" + # TODO: is there a way to not have to refresh the page and show the previous card + return redirect(url_for('displayDeck', username=username, deckTitle=deckTitle)) @app.route("///delete") def deleteDeck(username, deckTitle): # authenticate user if (not current_user.is_authenticated or current_user.id != username): - return redirect('/login') + return redirect(url_for('login')) # would need to first find user in db, but not set up yet db.decks.delete_one({"title": deckTitle}) - # would redirect to template for Cards - return "deleted deck" + # would rendirect to decks + # TODO: is there a way to not have to refresh the page when deleting deck + return redirect(url_for('allDecks', username=username)) # run the app if __name__ == "__main__": From f957eb8d2c2ed0cad50c29759fcd4406737cf081 Mon Sep 17 00:00:00 2001 From: gboeker Date: Fri, 1 Mar 2024 18:16:55 -0500 Subject: [PATCH 3/7] add card to user db --- app.py | 13 ++++++++----- templates/card.html | 4 ++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/app.py b/app.py index b67219c..d548c7b 100644 --- a/app.py +++ b/app.py @@ -58,7 +58,10 @@ def displayDeck(username, deckTitle): if (not current_user.is_authenticated or current_user.id != username): return redirect(url_for('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( + {"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}) @@ -85,11 +88,11 @@ def addCard(username, deckTitle): # authenticate user if (not current_user.is_authenticated or current_user.id != username): return redirect(url_for('login')) - # would need to first find user in db, but not set up yet - deck = db.decks.find_one({"title": deckTitle}) newCard = request.form["question"] - deck["cards"].append(newCard) - db.decks.update_one({"title": deckTitle}, {"$set": deck}) + db.users.update_one( + {"user_id": username, "personalDecks.title": deckTitle}, + {"$push": {"personalDecks.$.cards": newCard}} + ) # would redirect to template for Cards # TODO: is there a way to not have to refresh the page and show the added card return redirect(url_for('displayDeck', username=username, deckTitle=deckTitle)) diff --git a/templates/card.html b/templates/card.html index 6a64d59..c85948a 100644 --- a/templates/card.html +++ b/templates/card.html @@ -10,10 +10,10 @@

{{ deckTitle }}

START

- + - + From 18ab9aa2f84bad3d7c072a75676eb0e3603ec70a Mon Sep 17 00:00:00 2001 From: gboeker Date: Fri, 1 Mar 2024 19:10:56 -0500 Subject: [PATCH 4/7] edit card for user db --- app.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/app.py b/app.py index d548c7b..e3a662f 100644 --- a/app.py +++ b/app.py @@ -102,11 +102,12 @@ def editCard(username, deckTitle, cardIndex): # authenticate user if (not current_user.is_authenticated or current_user.id != username): return redirect(url_for('login')) - # would need to first find user in db, but not set up yet - deck = db.decks.find_one({"title": deckTitle}) newCard = request.form["question"] - deck["cards"][int(cardIndex)] = newCard - db.decks.update_one({"title": deckTitle}, {"$set": deck}) + db.users.update_one( + {"user_id": username, "personalDecks.title": deckTitle}, + {"$set": {"personalDecks.$[deck].cards.{0}".format(cardIndex): newCard}}, + array_filters=[{"deck.title": deckTitle}] + ) # would redirect to template for Cards # TODO: is there a way to not have to refresh the page and show the edited card return redirect(url_for('displayDeck', username=username, deckTitle=deckTitle)) From 66f26074cf6148af564758ebc477a340e3e69f72 Mon Sep 17 00:00:00 2001 From: gboeker Date: Fri, 1 Mar 2024 19:31:44 -0500 Subject: [PATCH 5/7] delete card in user db --- app.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/app.py b/app.py index e3a662f..5afec74 100644 --- a/app.py +++ b/app.py @@ -117,10 +117,20 @@ def deleteCard(username, deckTitle, cardIndex): # authenticate user if (not current_user.is_authenticated or current_user.id != username): return redirect(url_for('login')) - # would need to first find user in db, but not set up yet - deck = db.decks.find_one({"title": deckTitle}) - deck["cards"].pop(int(cardIndex)) - db.decks.update_one({"title": deckTitle}, {"$set": deck}) + + db.users.update_one( + {"user_id": username, "personalDecks.title": deckTitle}, + {"$pull": {"personalDecks.$.cards": "question"}} + ) + +# db.users.update_one( +# {"user_id": username, "personalDecks.title": deckTitle}, +# {"$pull": {"personalDecks.$.cards": question}} +# ) +# # would need to first find user in db, but not set up yet +# deck = db.decks.find_one({"title": deckTitle}) +# deck["cards"].pop(int(cardIndex)) +# db.decks.update_one({"title": deckTitle}, {"$set": deck}) # would redirect to template for Cards # TODO: is there a way to not have to refresh the page and show the previous card return redirect(url_for('displayDeck', username=username, deckTitle=deckTitle)) From 898eaf9bd5135baa79732890085c0b95e5b40359 Mon Sep 17 00:00:00 2001 From: gboeker Date: Fri, 1 Mar 2024 19:38:55 -0500 Subject: [PATCH 6/7] change edit card to search by question not index --- app.py | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/app.py b/app.py index 5afec74..77821fb 100644 --- a/app.py +++ b/app.py @@ -117,20 +117,11 @@ def deleteCard(username, deckTitle, cardIndex): # authenticate user if (not current_user.is_authenticated or current_user.id != username): return redirect(url_for('login')) - + newCard = request.form["question"] db.users.update_one( {"user_id": username, "personalDecks.title": deckTitle}, - {"$pull": {"personalDecks.$.cards": "question"}} + {"$pull": {"personalDecks.$.cards": newCard}} ) - -# db.users.update_one( -# {"user_id": username, "personalDecks.title": deckTitle}, -# {"$pull": {"personalDecks.$.cards": question}} -# ) -# # would need to first find user in db, but not set up yet -# deck = db.decks.find_one({"title": deckTitle}) -# deck["cards"].pop(int(cardIndex)) -# db.decks.update_one({"title": deckTitle}, {"$set": deck}) # would redirect to template for Cards # TODO: is there a way to not have to refresh the page and show the previous card return redirect(url_for('displayDeck', username=username, deckTitle=deckTitle)) From 8328de617979c09b80c229836ecdff509bb296b4 Mon Sep 17 00:00:00 2001 From: gboeker Date: Fri, 1 Mar 2024 19:42:53 -0500 Subject: [PATCH 7/7] delete deck for user in db --- app.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/app.py b/app.py index 77821fb..4bf0cd9 100644 --- a/app.py +++ b/app.py @@ -105,8 +105,8 @@ def editCard(username, deckTitle, cardIndex): newCard = request.form["question"] db.users.update_one( {"user_id": username, "personalDecks.title": deckTitle}, - {"$set": {"personalDecks.$[deck].cards.{0}".format(cardIndex): newCard}}, - array_filters=[{"deck.title": deckTitle}] + {"$set": {"personalDecks.$[deck].cards.$[card]": newCard}}, + array_filters=[{"deck.title": deckTitle}, {"card": newCard}] ) # would redirect to template for Cards # TODO: is there a way to not have to refresh the page and show the edited card @@ -131,8 +131,7 @@ def deleteDeck(username, deckTitle): # authenticate user if (not current_user.is_authenticated or current_user.id != username): return redirect(url_for('login')) - # would need to first find user in db, but not set up yet - db.decks.delete_one({"title": deckTitle}) + db.users.update_one({"user_id": username}, {"$pull": {"personalDecks": {"title": deckTitle}}}) # would rendirect to decks # TODO: is there a way to not have to refresh the page when deleting deck return redirect(url_for('allDecks', username=username))