From bde4f9c9925b44e0dc1fc3dc6db338ded4eeb608 Mon Sep 17 00:00:00 2001 From: iltenahmet Date: Sat, 24 Feb 2024 18:42:27 -0500 Subject: [PATCH 1/5] Display error message if .env is not set up. Update pipfile to include dotenv --- Pipfile | 1 + Pipfile.lock | 19 ++++++++++++++++++- app.py | 8 +++++++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/Pipfile b/Pipfile index 4e871b1..03c6b68 100644 --- a/Pipfile +++ b/Pipfile @@ -6,6 +6,7 @@ name = "pypi" [packages] pymongo = "*" flask = "*" +python-dotenv = "*" [dev-packages] diff --git a/Pipfile.lock b/Pipfile.lock index a917fba..616cf0f 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "9b7939a21a860b0c3f14a6a5fd131e51d50b87d5de8b6a7772d1b7c6555eb81b" + "sha256": "2845750caf44933171e5c0fa0a3d3693e3cbe3cb10b25811beee31ba0c5373a8" }, "pipfile-spec": 6, "requires": { @@ -32,6 +32,14 @@ "markers": "python_version >= '3.7'", "version": "==8.1.7" }, + "colorama": { + "hashes": [ + "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44", + "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6" + ], + "markers": "platform_system == 'Windows'", + "version": "==0.4.6" + }, "dnspython": { "hashes": [ "sha256:5ef3b9680161f6fa89daf8ad451b5f1a33b18ae8a1c6778cdf4b43f08c0a6e50", @@ -220,6 +228,15 @@ "markers": "python_version >= '3.7'", "version": "==4.6.2" }, + "python-dotenv": { + "hashes": [ + "sha256:e324ee90a023d808f1959c46bcbc04446a10ced277783dc6ee09987c37ec10ca", + "sha256:f7b63ef50f1b690dddf550d03497b66d609393b40b564ed0d674909a68ebf16a" + ], + "index": "pypi", + "markers": "python_version >= '3.8'", + "version": "==1.0.1" + }, "werkzeug": { "hashes": [ "sha256:507e811ecea72b18a404947aded4b3390e1db8f826b494d76550ef45bb3b1dcc", diff --git a/app.py b/app.py index 99ed3fa..6892cfd 100644 --- a/app.py +++ b/app.py @@ -11,6 +11,12 @@ uri = os.getenv("MONGO_URI") db_name = os.getenv("MONGO_DBNAME") +# Display error message if mongoDB environment is not set up +if not uri or not db_name: + error_message = "MongoDB environment is not set up. Please check your environment variables." + app.logger.error(error_message) + raise EnvironmentError(error_message) + # Create a new client and connect to the server client = MongoClient(uri) db = client[db_name] @@ -54,4 +60,4 @@ def addCard(username, deckTitle): # run the app if __name__ == "__main__": FLASK_PORT = os.getenv("FLASK_PORT", "5000") - app.run(port=FLASK_PORT) \ No newline at end of file + app.run(port=FLASK_PORT) From b1c7963b01be6cf8a0cafca4064519e76aa0708c Mon Sep 17 00:00:00 2001 From: iltenahmet Date: Sat, 24 Feb 2024 18:42:27 -0500 Subject: [PATCH 2/5] Display error message if .env is not set up. Update pipfile to include dotenv --- Pipfile | 1 + Pipfile.lock | 19 ++++++++++++++++++- app.py | 8 +++++++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/Pipfile b/Pipfile index 4e871b1..03c6b68 100644 --- a/Pipfile +++ b/Pipfile @@ -6,6 +6,7 @@ name = "pypi" [packages] pymongo = "*" flask = "*" +python-dotenv = "*" [dev-packages] diff --git a/Pipfile.lock b/Pipfile.lock index a917fba..616cf0f 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "9b7939a21a860b0c3f14a6a5fd131e51d50b87d5de8b6a7772d1b7c6555eb81b" + "sha256": "2845750caf44933171e5c0fa0a3d3693e3cbe3cb10b25811beee31ba0c5373a8" }, "pipfile-spec": 6, "requires": { @@ -32,6 +32,14 @@ "markers": "python_version >= '3.7'", "version": "==8.1.7" }, + "colorama": { + "hashes": [ + "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44", + "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6" + ], + "markers": "platform_system == 'Windows'", + "version": "==0.4.6" + }, "dnspython": { "hashes": [ "sha256:5ef3b9680161f6fa89daf8ad451b5f1a33b18ae8a1c6778cdf4b43f08c0a6e50", @@ -220,6 +228,15 @@ "markers": "python_version >= '3.7'", "version": "==4.6.2" }, + "python-dotenv": { + "hashes": [ + "sha256:e324ee90a023d808f1959c46bcbc04446a10ced277783dc6ee09987c37ec10ca", + "sha256:f7b63ef50f1b690dddf550d03497b66d609393b40b564ed0d674909a68ebf16a" + ], + "index": "pypi", + "markers": "python_version >= '3.8'", + "version": "==1.0.1" + }, "werkzeug": { "hashes": [ "sha256:507e811ecea72b18a404947aded4b3390e1db8f826b494d76550ef45bb3b1dcc", diff --git a/app.py b/app.py index d8532ab..cea23ba 100644 --- a/app.py +++ b/app.py @@ -11,6 +11,12 @@ uri = os.getenv("MONGO_URI") db_name = os.getenv("MONGO_DBNAME") +# Display error message if mongoDB environment is not set up +if not uri or not db_name: + error_message = "MongoDB environment is not set up. Please check your environment variables." + app.logger.error(error_message) + raise EnvironmentError(error_message) + # Create a new client and connect to the server client = MongoClient(uri) db = client[db_name] @@ -78,4 +84,4 @@ def editCard(username, deckTitle, cardIndex): # run the app if __name__ == "__main__": FLASK_PORT = os.getenv("FLASK_PORT", "5000") - app.run(port=FLASK_PORT) \ No newline at end of file + app.run(port=FLASK_PORT) From cf101a60475c22cd98c2de6f0129fcb60c441ea4 Mon Sep 17 00:00:00 2001 From: iltenahmet Date: Sun, 25 Feb 2024 09:19:47 -0500 Subject: [PATCH 3/5] Handle routes for login and signup pages, rename signin.html to signup.html --- app.py | 8 ++++++++ templates/signup.html | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index cea23ba..b51fbf7 100644 --- a/app.py +++ b/app.py @@ -46,6 +46,14 @@ def signup(): #would redirect to template for login return render_template('signup.html') +@app.route('/login', methods=['GET']) +def login(): + return render_template('login.html') + +@app.route('/signup', methods=['GET']) +def signup(): + return render_template('signup.html') + @app.route("//decks") def allDecks(username): # would need to first find user in db, but not set up yet diff --git a/templates/signup.html b/templates/signup.html index 3ed9425..03c05d4 100644 --- a/templates/signup.html +++ b/templates/signup.html @@ -11,4 +11,4 @@ -{% endblock %} \ No newline at end of file +{% endblock %} From 46b723f354d09ed1b60371f8e9733e3a964de74a Mon Sep 17 00:00:00 2001 From: iltenahmet Date: Sun, 25 Feb 2024 09:48:09 -0500 Subject: [PATCH 4/5] Move authentication to it's own file --- app.py | 10 +++------- authentication.py | 9 +++++++++ 2 files changed, 12 insertions(+), 7 deletions(-) create mode 100644 authentication.py diff --git a/app.py b/app.py index b51fbf7..e786452 100644 --- a/app.py +++ b/app.py @@ -3,6 +3,7 @@ import os import pymongo from pymongo import MongoClient +import authentication load_dotenv() @@ -46,13 +47,8 @@ def signup(): #would redirect to template for login return render_template('signup.html') -@app.route('/login', methods=['GET']) -def login(): - return render_template('login.html') - -@app.route('/signup', methods=['GET']) -def signup(): - return render_template('signup.html') +app.add_url_rule('/login', view_func=authentication.login) +app.add_url_rule('/signup', view_func=authentication.signup) @app.route("//decks") def allDecks(username): diff --git a/authentication.py b/authentication.py new file mode 100644 index 0000000..80b9283 --- /dev/null +++ b/authentication.py @@ -0,0 +1,9 @@ +from flask import render_template + +def login(): + return render_template('login.html') + +def signup(): + return render_template('signup.html') + + From 75d19ad6ecdb7b5b29c715bfe1b424a9f65bc040 Mon Sep 17 00:00:00 2001 From: iltenahmet Date: Sun, 25 Feb 2024 10:01:18 -0500 Subject: [PATCH 5/5] Fix errors after rebasing this branch --- app.py | 20 +++++--------------- authentication.py | 2 +- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/app.py b/app.py index e786452..2a497ca 100644 --- a/app.py +++ b/app.py @@ -28,27 +28,17 @@ print("Connected to MongoDB!") except Exception as e: print("MongoDB connection error:", e) - + @app.route("/") def home(): + # Note: (Ahmet) I will do these # TODO: redirect to login # TODO: redirect to sign up return render_template('start.html') -@app.route("/login", methods=["GET", "POST"]) -def login(): - # would get form fields - # would check if correct username and password - return render_template('login.html') - -@app.route("/signup", methods=["GET", "POST"]) -def signup(): - #would add user to db - #would redirect to template for login - return render_template('signup.html') - -app.add_url_rule('/login', view_func=authentication.login) -app.add_url_rule('/signup', view_func=authentication.signup) +# Handle authentication related stuff in authentication.py file +app.add_url_rule('/login', methods=["GET", "POST"], view_func=authentication.login) +app.add_url_rule('/signup', methods=["GET", "POST"], view_func=authentication.signup) @app.route("//decks") def allDecks(username): diff --git a/authentication.py b/authentication.py index 80b9283..db3ad35 100644 --- a/authentication.py +++ b/authentication.py @@ -6,4 +6,4 @@ def login(): def signup(): return render_template('signup.html') - +# TODO: Handle autehentication and session management with flask-login