-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmenu.py
More file actions
222 lines (191 loc) · 6.4 KB
/
menu.py
File metadata and controls
222 lines (191 loc) · 6.4 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
'''
Provides a basic frontend
'''
import sys
import logging
import datetime
import main
#pylint: disable=E0606
def load_users():
'''
Loads user accounts from a file
'''
logging.info("initializing load_user")
file = input('Enter filename of user file: ')
main.load_users(file, user_collection)
def load_status_updates():
'''
Loads status updates from a file
'''
logging.info("initializing load_status_updates")
file_name = input('Enter filename for status file: ')
result = main.load_status_updates(file_name, status_collection)
if result is True:
print("Statuses added to collection successfully.")
else:
print("Error occurred. Please enter a valid file name.")
def add_user():
'''
Adds a new user into the database
'''
logging.info("initializing add_user")
user_id = input('User ID: ')
email = input('User email: ')
user_name = input('User name: ')
user_last_name = input('User last name: ')
if not main.add_user(user_id,
email,
user_name,
user_last_name,
user_collection):
logging.warning("new user inputs not valid")
print("An error occurred while trying to add new user")
else:
logging.info("new user added successfully")
print("User was successfully added", )
def update_user():
'''
Updates information for an existing user
'''
logging.info("initializing update_user")
user_id = input('User ID: ')
email = input('User email: ')
user_name = input('User name: ')
user_last_name = input('User last name: ')
if not main.update_user(user_id, email, user_name, user_last_name, user_collection):
print("An error occurred while trying to update user")
else:
print("User was successfully updated")
def search_user():
'''
Searches a user in the database
'''
logging.info("initializing search_user")
user_id = input('Enter user ID to search: ')
result = main.search_user(user_id, user_collection)
if not result.user_id:
print("ERROR: User does not exist")
else:
print(f"User ID: {result.user_id}")
print(f"Email: {result.email}")
print(f"Name: {result.user_name}")
print(f"Last name: {result.user_last_name}")
def delete_user():
'''
Deletes user from the database
'''
user_id = input('User ID: ')
if not main.delete_user(user_id, user_collection):
print("An error occurred while trying to delete user")
else:
print("User was successfully deleted")
def save_users():
'''
Saves user database into a file
'''
file_name = input('Enter filename for users file: ')
main.save_users(file_name, user_collection)
def add_status():
'''
Adds a new status into the database
'''
user_id = input('User ID: ')
status_id = input('Status ID: ')
status_text = input('Status text: ')
if not main.add_status(user_id, status_id, status_text, status_collection):
print("An error occurred while trying to add new status")
else:
print("New status was successfully added")
def update_status():
'''
Updates information for an existing status
'''
user_id = input('User ID: ')
status_id = input('Status ID: ')
status_text = input('Status text: ')
if not main.update_status(status_id, user_id, status_text, status_collection):
print("An error occurred while trying to update status")
else:
print("Status was successfully updated")
def search_status():
'''
Searches a status in the database
'''
status_id = input('Enter status ID to search: ')
result = main.search_status(status_id, status_collection)
if not result.user_id:
print("ERROR: Status does not exist")
else:
print(f"User ID: {result.user_id}")
print(f"Status ID: {result.status_id}")
print(f"Status text: {result.status_text}")
def delete_status():
'''
Deletes status from the database
'''
status_id = input('Status ID: ')
if not main.delete_status(status_id, status_collection):
print("An error occurred while trying to delete status")
else:
print("Status was successfully deleted")
def save_status():
'''
Saves status database into a file
'''
file_name = input('Enter filename for status file: ')
main.save_status_updates(file_name, status_collection)
def quit_program():
'''
Quits program
'''
logging.info("user quit the program.")
sys.exit()
if __name__ == '__main__':
#pylint: disable=C0103
log_format = "%(asctime)s %(filename)s:%(lineno)-4d %(levelname)s %(message)s"
logger = logging.getLogger(__name__)
date = datetime.datetime.today().strftime('%m-%d-%Y')
filename = "log_" + date + ".log"
logging.basicConfig(level=logging.DEBUG,
filename= filename,
filemode='a',
format = log_format)
logging.info("starting up code.")
user_collection = main.init_user_collection()
status_collection = main.init_status_collection()
menu_options = {
'A': load_users,
'B': load_status_updates,
'C': add_user,
'D': update_user,
'E': search_user,
'F': delete_user,
'G': save_users,
'H': add_status,
'I': update_status,
'J': search_status,
'K': delete_status,
'L': save_status,
'Q': quit_program
}
while True:
user_selection = input("""
A: Load user database
B: Load status database
C: Add user
D: Update user
E: Search user
F: Delete user
G: Save user database to file
H: Add status
I: Update status
J: Search status
K: Delete status
L: Save status database to file
Q: Quit
Please enter your choice: """)
if user_selection.upper() in menu_options:
menu_options[user_selection.upper()]()
else:
print("Invalid option")
logging.info("user inputted invalid option")