-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
62 lines (52 loc) · 1.93 KB
/
db.py
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
from pymongo import MongoClient # type: ignore
import hashlib
connection_string="mongodb+srv://hjgoat:[email protected]/"
client=MongoClient(connection_string)
db=client["amongus"]
userdata=db["userdata"]
quizdata=db["quizdata"]
def add_signup(useremail,password,name):
newpass=hashlib.sha256(password.encode()).hexdigest()
query={"useremail":useremail}
if userdata.find_one(query):
return False
userdata.insert_one({'useremail':useremail,'password':newpass,'name':name,'State':0})
return True
def check_login(useremail,password):
newpass=hashlib.sha256(password.encode()).hexdigest()
query={"useremail":useremail,"password":newpass}
if userdata.find_one(query):
return True
return False
def getName(useremail,password):
newpass=hashlib.sha256(password.encode()).hexdigest()
query={"useremail":useremail,"password":newpass}
projection={"name":1,"_id":0}
result=userdata.find_one(query,projection)
return result['name']
def getQuestion(state):
query={"State":state}
document=quizdata.find_one(query,{"_id":0})
return document
def getState(useremail):
query={"useremail":useremail}
condition={"_id":0,"State":1}
result=userdata.find_one(query,condition)
return result['State']
def IncrementState(email):
query = {"useremail":email}
userdata.update_one(query,{'$inc':{"State":1}})
return True
def DecrementState(email):
query = {"useremail": email}
current_data = userdata.find_one(query)
if current_data and current_data["State"] > 0:
userdata.update_one(query, {'$inc': {"State": -1}})
current_data = userdata.find_one(query)
print(current_data)
result = quizdata.find_one({"State":current_data["State"]})
print(result)
return result
else:
result = quizdata.find_one({"State":0})
return result