-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
142 lines (104 loc) · 4.64 KB
/
db.py
File metadata and controls
142 lines (104 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
uri = 'mongodb+srv://dcode:Tat1pet@cluster0.l5w5iei.mongodb.net/?retryWrites=true&w=majority'
# Create a new client and connect to the server
client = MongoClient(uri, server_api=ServerApi('1'))
db = client.get_database('Cluster0')
records = db.users
dcQuiz = db.dc_quiz
marvelQuiz = db.marvel_quiz
def existsByEmail(email):
"""
Check if a user with the given email address exists in the database.
This function queries the database to check if a user record with the specified
email address exists. If a user with the provided email is found in the database,
it returns True; otherwise, it returns False.
Args:
email (str): The email address to check for existence in the database.
Returns:
bool: True if a user with the provided email exists in the database, False otherwise.
"""
emailFound = records.find_one({"email": email})
if emailFound:
return True
return False
def registerUser(user):
"""
Register a new user by adding their information to the database.
This function inserts a new user record into the database using the provided 'user'
dictionary. It then checks if the user has been successfully added to the database
by querying for a user with the same email address. If a user with the provided email
is found in the database, it returns True, indicating successful registration; otherwise,
it returns False.
Args:
user (dict): A dictionary containing user information, including first_name, last_name,
email, and hashed password.
Returns:
bool: True if the user was successfully registered in the database, False otherwise.
"""
records.insert_one(user)
user = records.find_one({"email": user['email']})
if user:
return True
return False
def getUserByEmail(email):
"""
Retrieve a user's information from the database by their email address.
This function queries the database to retrieve user information for the user
associated with the provided email address.
Args:
email (str): The email address of the user whose information is to be retrieved.
Returns:
dict or None: A dictionary containing the user's information (if found) or None
if no user with the provided email address exists in the database.
"""
return records.find_one({"email": email})
def getUserScores(email):
"""
Retrieve a user's quiz scores from the database by their email address.
This function queries the 'scores' collection in the database to retrieve quiz scores
associated with the user specified by their email address.
Args:
email (str): The email address of the user whose quiz scores are to be retrieved.
Returns:
pymongo.cursor.Cursor: A cursor containing quiz scores for the user specified by
their email address. The cursor can be iterated to access individual score records.
"""
return db.scores.find({"email": email})
def fetchQuestions(universe):
"""
Retrieve quiz questions from the database based on the specified universe.
This function queries the database to retrieve quiz questions based on the specified 'universe.'
If 'universe' is 'dc,' it fetches questions related to the DC Comics universe; otherwise, it fetches
questions related to the Marvel Comics universe.
Args:
universe (str): The universe for which to retrieve quiz questions ('dc' or 'marvel').
Returns:
pymongo.cursor.Cursor: A cursor containing quiz questions for the specified universe.
The cursor can be iterated to access individual question records.
"""
if universe == 'dc':
return dcQuiz.find()
else:
return marvelQuiz.find()
def storeUserScore(email, universe, score, quizDateTime):
"""
Store a user's quiz score in the database.
This function inserts a user's quiz score information into the 'scores' collection in the database.
Args:
email (str): The email address of the user who took the quiz.
universe (str): The universe for which the quiz score was obtained ('dc' or 'marvel').
score (int): The user's quiz score.
quizDateTime (datetime): The date and time when the quiz was taken.
Returns:
bool: True if the user's quiz score information was successfully stored in the database,
False otherwise.
"""
user_score = {
"email": email,
"universe": universe,
"score": score,
"quizDateTime": quizDateTime
}
db.scores.insert_one(user_score)
return True