Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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)
Expand All @@ -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({
Expand All @@ -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)
Expand Down Expand Up @@ -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')
Expand Down
10 changes: 10 additions & 0 deletions mongoDb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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:[email protected]/?retryWrites=true&w=majority")
db=cluster["Games"]
Expand Down
25 changes: 16 additions & 9 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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")
}
})
}
</script>
<script>
function signOut(){
Expand Down
5 changes: 5 additions & 0 deletions templates/games.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ <h1>© 2022 Matt Lee</h1>
</footer>
</div>
<script>
/**
*
* Calling functions from the 'base.html' file
*
*/
function addGameform() {
document.getElementById("form-popup").style.display = "block";
}
Expand Down
26 changes: 25 additions & 1 deletion templates/gamesDetails.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
{% set active_page = "games" %}
{% block body %}
<div class="conentDiv-games">
<!-- <iframe id="vid" src="https://www.youtube.com/embed/Wqsg2vWHZBM?autoplay=1&mute=1&showinfo=0?modestbranding=0&controls=0" frameborder="0" allow="autoplay"></iframe> -->
<div class="mainDiv-game">
{% for game in data %}
<div>
Expand Down Expand Up @@ -42,6 +41,16 @@ <h3 id="price">{{game.price}}</h3>
</div>
</div>
<script>
/**
*
* This function gets the innerHTML of the element, with id slug.
*
* This function then creates a DELETE API request to the python flask back-end server with the API path
* '/deleteGame/'.
*
* @param slug The Games's slug
*
*/
function deleteGame(slug){
slug = document.getElementById("slug").innerHTML;
console.log(slug)
Expand All @@ -58,6 +67,17 @@ <h3 id="price">{{game.price}}</h3>
}
xhr.send();
}
/**
*
* This function gets the user id of the user that is currently signed in with firebase authentication and
* also gets name, price and image from the elements cointaining i'ds gameName, price and image resepectively
*
* This function then creates a POST API request to the python flask back-end server with the API path
* '/addToCart/' + the logged in user's user id.
*
* @param uid The user's userID
*
*/
function addToCart(){
name = document.getElementById("gameName").innerHTML;
userID = document.getElementById("user_id").innerHTML;
Expand All @@ -80,6 +100,10 @@ <h3 id="price">{{game.price}}</h3>
}
xhr.send();
}
/**
*
* Everytime 'newline' is found on the page it will remove it and replace it with a line break
*/
window.onload = function replace () {
document.body.innerHTML
= document.body.innerHTML
Expand Down