forked from xyluz/pythonclass
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
156 lines (91 loc) · 3.07 KB
/
database.py
File metadata and controls
156 lines (91 loc) · 3.07 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
143
144
145
146
147
148
149
150
151
152
153
154
# create record
# update record
# read record
# delete record
# CRUD
# find user
import os
import validation
user_db_path = "data/user_record/"
def create(user_account_number, first_name, last_name, email, password):
# create a file
# name of the file would be account_number.txt
# add the user details to the file
# return true
# if saving to file fails, then deleted created file
user_data = first_name + "," + last_name + "," + email + "," + password + "," + str(0)
if does_account_number_exist(user_account_number):
return False
if does_email_exist(email):
print("User already exists")
return False
completion_state = False
try:
f = open(user_db_path + str(user_account_number) + ".txt", "x")
except FileExistsError:
does_file_contain_data = read(user_db_path + str(user_account_number) + ".txt")
if not does_file_contain_data:
delete(user_account_number)
else:
f.write(str(user_data));
completion_state = True
finally:
f.close();
return completion_state
def read(user_account_number):
# find user with account number
# fetch content of the file
is_valid_account_number = validation.account_number_validation(user_account_number)
try:
if is_valid_account_number:
f = open(user_db_path + str(user_account_number) + ".txt", "r")
else:
f = open(user_db_path + user_account_number, "r")
except FileNotFoundError:
print("User not found")
except FileExistsError:
print("User doesn't exist")
except TypeError:
print("Invalid account number format")
else:
return f.readline()
return False
def update(user_account_number):
print("update user record")
# find user with account number
# fetch the content of the file
# update the content of the file
# save the file
# return true
def delete(user_account_number):
# find user with account number
# delete the user record (file)
# return true
is_delete_successful = False
if os.path.exists(user_db_path + str(user_account_number) + ".txt"):
try:
os.remove(user_db_path + str(user_account_number) + ".txt")
is_delete_successful = True
except FileNotFoundError:
print("User not found")
finally:
return is_delete_successful
def does_email_exist(email):
all_users = os.listdir(user_db_path)
for user in all_users:
user_list = str.split(read(user), ',')
if email in user_list:
return True
return False
def does_account_number_exist(account_number):
all_users = os.listdir(user_db_path)
for user in all_users:
if user == str(account_number) + ".txt":
return True
return False
def authenticated_user(account_number, password):
if does_account_number_exist(account_number):
user = str.split(read(account_number), ',')
if password == user[3]:
return user
return False