-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
172 lines (130 loc) · 4.26 KB
/
database.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import json
import uuid
def addUser(email:str, password:str):
"""Adds a user to the database and generates a unique UserID for them
Args:
email (str): The email of the new user
password (str): The password of the new user
"""
user = {
"id": str(uuid.uuid4()),
"email": email,
"password": password
}
with open("./database/database.json", "r") as f:
fileCont = f.read()
database = json.loads(fileCont)
database["users"].append(user)
json_database = json.dumps(database)
with open("./database/database.json", "w") as f:
f.write(json_database)
def getUser(id:str):
"""Gets User
Args:
id (uid): A uid: b3d19da8-c681-47da-8736-25cbb97f3512
Returns:
{
"id": "b3d19da8-c681-47da-8736-25cbb97f3512",
"email": "[email protected]",
"password": "asjdkflaslkdf"
}
"""
with open("./database/database.json", "r") as f:
fileCont = f.read()
database = json.loads(fileCont)
for user in database["users"]:
if user["id"] == id:
return user
return None
def getSession(email:str, password:str):
"""Gets the userID from an username and password
Args:
email (str): The username you're trying to find
password (str): The password you're trying to find
Returns:
(str / None): Returns the userID or nothing if it isn't found
"""
with open("./database/database.json", "r") as f:
fileCont = f.read()
database = json.loads(fileCont)
for user in database["users"]:
if user["email"] == email:
if user["password"] == password:
return user["id"]
return None
def addRecievedEmail(from_email:str, uid:str, to_email:str, subject:str, contents:str):
"""Adds a recieved email to the database
Args:
from_email (str): The emailaddress the email came from
uid (str): The userID of the user
to_email (str): The emailaddress the email was for
subject (str): The subject of the email
contents (str): The contents of the email
"""
email = {
"from_email": from_email,
"uid": uid,
"to_email": to_email,
"subject": subject,
"contents": contents
}
with open("./database/database.json", "r") as f:
fileCont = f.read()
database = json.loads(fileCont)
database["recieved_emails"].append(email)
json_database = json.dumps(database)
with open("./database/database.json", "w") as f:
f.write(json_database)
def addSendEmail(from_email:str, uid:str, to_email:str, subject:str, contents:str):
"""Adds a send email to the database
Args:
from_email (str): The emailaddress the email came from
uid (str): The userID of the user
to_email (str): The emailaddress the email was for
subject (str): The subject of the email
contents (str): The contents of the email
"""
email = {
"from_email": from_email,
"uid": uid,
"to_email": to_email,
"subject": subject,
"contents": contents
}
with open("./database/database.json", "r") as f:
fileCont = f.read()
database = json.loads(fileCont)
database["send_emails"].append(email)
json_database = json.dumps(database)
with open("./database/database.json", "w") as f:
f.write(json_database)
def getMail(id:str):
"""Gets all emails recieved by an user
Args:
id (str): userID
Returns:
list: list of emails
"""
all_mails = []
with open("./database/database.json", "r") as f:
fileCont = f.read()
database = json.loads(fileCont)
for email in database["recieved_emails"]:
if email["uid"] == id:
all_mails.append(email)
return all_mails
def getSendMail(id:str):
"""Gets all emails send by an user
Args:
id (str): userID
Returns:
list: list of emails
"""
all_mails = []
with open("./database/database.json", "r") as f:
fileCont = f.read()
database = json.loads(fileCont)
for email in database["send_emails"]:
if email["uid"] == id:
all_mails.append(email)
return all_mails