From 86da3f92fb28af57aa29b7862ea3f43fd5919584 Mon Sep 17 00:00:00 2001 From: Matt Lee Date: Thu, 8 Dec 2022 15:24:27 +0000 Subject: [PATCH] Final Commit - Code Documented --- main.py | 29 +++++++++++++++++++++++++++++ mongoDb.py | 10 ++++++++++ templates/base.html | 25 ++++++++++++++++--------- templates/games.html | 5 +++++ templates/gamesDetails.html | 26 +++++++++++++++++++++++++- 5 files changed, 85 insertions(+), 10 deletions(-) diff --git a/main.py b/main.py index 0cfb15a..604f510 100644 --- a/main.py +++ b/main.py @@ -29,6 +29,13 @@ # [START gae_python38_datastore_store_and_fetch_user_times] # [START gae_python3_datastore_store_and_fetch_user_times] + +""" + store_time() function to store the time the user logs in, to google cloud datastore. + :param email: The user's email (set by firebaseAuth) + :return: Returns a confirmation string +""" + def store_time(email, dt): entity = datastore.Entity(key=datastore_client.key('User', email, 'visit')) entity.update({ @@ -38,6 +45,12 @@ def store_time(email, dt): datastore_client.put(entity) +""" + fetch_times() function to get user's login times from google cloud datastore. + :param email: The user's email (set by firebaseAuth) + :return: Returns a confirmation string +""" + def fetch_times(email, limit): ancestor = datastore_client.key('User', email) query = datastore_client.query(kind='visit', ancestor=ancestor) @@ -47,6 +60,12 @@ def fetch_times(email, limit): return times +""" + add_user_details() function add the user's data to the google cloud datastore. + :param user_id: The user's user id (set by firebaseAuth) + :return: Returns a confirmation string +""" + def add_user_details(gamertag, platform, genre, game, uid): entity = datastore.Entity(key=datastore_client.key('UserId', uid, 'userDetails')) entity.update({ @@ -59,6 +78,12 @@ def add_user_details(gamertag, platform, genre, game, uid): datastore_client.put(entity) +""" + fetch_user_details() function gets all the user's data from the google cloud datastore. + :param user_id: The user's user id (set by firebaseAuth) + :return: Returns a confirmation string +""" + def fetch_user_details(uid, limit): ancestor = datastore_client.key('UserId', uid) query = datastore_client.query(kind='userDetails', ancestor=ancestor) @@ -87,6 +112,10 @@ def delete_user_details(user_id): data = query.fetch() datastore_client.delete_multi(data) +""" + The following functions define the routes for the site, passing in the correct values needed for the pages. + Accessing the mongoDB file if needed and recieving data from http requests made on the front end +""" @app.route('/') @app.route('/index') diff --git a/mongoDb.py b/mongoDb.py index 21bcc72..68281db 100644 --- a/mongoDb.py +++ b/mongoDb.py @@ -3,6 +3,11 @@ from pymongo import MongoClient from bson.json_util import dumps +""" + the following functions, up to the next comment, call the functions from the cloud and returns the correct data and/or + passes in the correct variables the cloud functions needs +""" + def get_games(): url = "https://europe-west2-ad-gamezone.cloudfunctions.net/get_mongodb_games" @@ -39,6 +44,11 @@ def add_game(new_game): uResponse return new_game +""" + The following functions access mongoDB with values passed from main.py. These funcions allow the pages + basket and myOrders to run correctly and remain persistent +""" + def get_cartbyID(userID, total): cluster=MongoClient( "mongodb+srv://dpUser:dpUserPassword@adcoursework.9ybhyss.mongodb.net/?retryWrites=true&w=majority") db=cluster["Games"] diff --git a/templates/base.html b/templates/base.html index bcdabe5..7449b54 100644 --- a/templates/base.html +++ b/templates/base.html @@ -93,11 +93,19 @@ document.cookie = "token=" + token; }); } else { + // Directs the user to the sign in page if they try to manually enter the website url console.log("No user signed in") window.location.replace("/"); } }); + /** + * + * This function gets decides whether the user is logged in and then works out which user_id is linked + * to that account. Then displays the crud options if the user has signed in with an authorised account + * + */ + function addEditBtns() { firebase.auth().onAuthStateChanged(function(user) { if (user) { @@ -115,6 +123,14 @@ } }) } + + /** + * + * This function gets decides whether the user is logged in and then works out which user_id is linked + * to that account. Then displays the delete a game option if the user has signed in with an authorised account + * + */ + function deleteBtn() { firebase.auth().onAuthStateChanged(function(user) { if (user) { @@ -128,15 +144,6 @@ } }) } - function displayData() { - firebase.auth().onAuthStateChanged(function(user) { - if (user) { - document.getElementById("account-name").innerHTML=user.uid; - } else { - console.log("No user signed in") - } - }) - }