-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
49 lines (39 loc) · 1.35 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
import datetime
class DataBase:
def __init__(self, filename):
self.filename = filename
self.users = None
self.file = None
self.load()
def load(self):
self.file = open(self.filename, "r")
self.users = {}
for line in self.file:
email, password, name, created = line.strip().split(";")
self.users[email] = (password, name, created)
self.file.close()
def get_user(self, email):
if email in self.users:
return self.users[email]
else:
return -1
def add_user(self, email, password, name):
if email.strip() not in self.users:
self.users[email.strip()] = (password.strip(), name.strip(), DataBase.get_date())
self.save()
return 1
else:
print("Email exists already")
return -1
def validate(self, email, password):
if self.get_user(email) != -1:
return self.users[email][0] == password
else:
return False
def save(self):
with open(self.filename, "w") as f:
for user in self.users:
f.write(user + ";" + self.users[user][0] + ";" + self.users[user][1] + ";" + self.users[user][2] + "\n")
@staticmethod
def get_date():
return str(datetime.datetime.now()).split(" ")[0]