/
app.py
584 lines (553 loc) · 16.9 KB
/
app.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
import os
import sqlite3 as sql
import user, db, seenit, post, comment
import postUpvote as pu
import postDownvote as pd
import commentUpvote as cu
import commentDownvote as cd
import getpass
import logging
logging.basicConfig(filename='seenit.log', level=logging.INFO,
format='%(asctime)s:%(levelname)s:%(message)s')
new_u_id = new_s_id = 5
new_p_id = new_c_id = new_pu_id = new_cu_id = 8
new_pd_id = new_cd_id = 1
this_u_id = 0
admin = False
# run this when database is not created yet
def build_database():
logging.info("Build database\n")
db.create_tables()
db.insert_users()
db.insert_seenits()
db.insert_posts()
db.insert_comments()
db.insert_post_upvote()
db.insert_comment_upvote()
# db.show_seenits()
# db.show_posts()
# db.show_comments()
# db.show_post_upvotes()
def register():
global new_u_id, this_u_id, admin
logging.info("Register request user response\n")
print("1 - Register")
x = input("Username: ")
z = getpass.getpass('Password:')
y = input("Email: ")
w = input("Are you an administrator? (y/n)")
logging.info("Response: ")
logging.info("user name: ")
logging.info(x)
logging.info(" password: ")
logging.info(z)
logging.info(" email: ")
logging.info(y)
logging.info("Are you administrator? ")
logging.info(w)
logging.info("\n")
user.insert(new_u_id, x, y, z)
this_u_id = new_u_id
# print ("this u id is ", this_u_id)
new_u_id += 1
if w == 'y':
admin = True
main_menu()
def login():
global this_u_id, admin
logging.info("Print login menu - Request user response\n")
print("-" * 40)
print (" Login Menu")
print("-" * 40)
login = input('''
1 = Register
2 = Already A Member
3 = Main Menu
4 = Exit
''')
print("-" * 40)
# print("\n")
if login == '1':
logging.info("Response: 1 - Register\n")
register()
elif login == '2':
logging.info("Response: 2 - Already A Member\n")
logging.info("Print login menu - Request user response\n")
print("2 - Login")
x = input("Username:")
y = getpass.getpass('Password:')
w = input("Are you an administrator? (y/n)")
this_u_id = user.login(x, y)
logging.info("Username: ")
logging.info(x)
logging.info(" password: ")
logging.info(y)
logging.info(" Are you an administrator? ")
logging.info(w)
logging.info("\n")
if w == 'y':
admin = True
main_menu()
elif login == '3':
logging.info("Response: 3 - main menu\n")
main_menu()
else:
logging.info("Response: 4 - Exit\n")
exit()
def insert_vote(table, vote, id):
global new_pu_id, new_pd_id, new_cu_id, new_cd_id
if table == 'post':
if vote == 'up':
pu.insert(new_pu_id, id, this_u_id)
new_pu_id += 1
else:
pd.insert(new_pd_id, id, this_u_id)
new_pd_id += 1
show_votes('post', id)
else:
if vote == 'up':
cu.insert(new_cu_id, id, this_u_id)
new_cu_id += 1
else:
cd.insert(new_cd_id, id, this_u_id)
new_cd_id += 1
show_comment(id)
def delete_vote(table, vote, id):
logging.info("delete_vote request user response: Please input id of the one you want to delete\n")
v_id = input("Please input id of the one you want to delete:")
logging.info("Response: ")
logging.info(v_id)
logging.info("\n")
if table == 'post':
if vote == 'up':
pu.delete(v_id)
else:
pd.delete(v_id)
show_votes('post', id)
else:
if vote == 'up':
cu.delete(v_id)
else:
cd.delete(v_id)
show_comment(id)
def show_votes(table, id):
print ("【Upvotes】:")
if table == 'comment':
cu.read_all(id)
else:
pu.read_all(id)
print ("【Downvotes】:")
if table == 'comment':
cd.read_all(id)
else:
pd.read_all(id)
print("-" * 40)
print (" Vote Menu")
print("-" * 40)
logging.info("Print show_votes menu - Request user's response\n")
if admin == True:
method = input('''
1 = Up
2 = Down
3 = Delete Up
4 = Delete Down
5 = Main Menu
6 = Exit
''')
else:
method = input('''
1 = Up
2 = Down
5 = Main Menu
6 = Exit
''')
if method == '1':
logging.info("Response: 1 - Up\n")
insert_vote(table, 'up', id)
elif method == '2':
logging.info("Response: 2 - Down\n")
insert_vote(table, 'down', id)
elif method == '3':
logging.info("Response: 3 - Delete Up\n")
delete_vote(table, 'up', id)
elif method == '4':
logging.info("Response: 4 - Delete Down\n")
delete_vote(table, 'down', id)
elif method == '6':
logging.info("Response: 6 - Exit")
exit()
else:
logging.info("Response: 5 - Main Menu")
main_menu()
def insert_comment(p_id):
global new_c_id
logging.info("insert_comment request user response - Please input your comment\n")
content = input("Please input your comment:")
logging.info("Response: ")
logging.info(content)
logging.info("\n")
comment.insert(new_c_id,content,p_id,this_u_id)
new_c_id += 1
show_comments(p_id)
def show_comment(c_id):
comment.read_one(c_id)
show_votes('comment', c_id)
def delete_comment(p_id):
logging.info("delete_comment request user response - Please input id of the one you choose\n")
c_id = input("Please input id of the one you choose:")
logging.info("Response: ")
logging.info(c_id)
logging.info("\n")
comment.delete(c_id)
show_comments(p_id)
def update_comment(p_id):
logging.info("update_comment request user response - Please input the id of the one you choose\n")
c_id = input("Please input id of the one you choose:")
logging.info("Response: ")
logging.info(c_id)
logging.info("\n")
logging.info("update_comment request user response - Please input your comment\n")
content = input("Please input your comment:")
logging.info("Response: ")
logging.info(content)
logging.info("\n")
comment.update(c_id,content)
show_comments(p_id)
def show_comments(p_id):
print ("Comments:")
comment.read_all(p_id)
# print (comments)
print("-" * 40)
print (" Comment Menu")
print("-" * 40)
logging.info("Print show_comments menu - Request user response\n")
if admin == True:
method = input('''
1 = Create One
2 = Choose One
3 = Delete One
4 = Update One
5 = Main Menu
6 = Exit
''')
else:
method = input('''
1 = Create One
2 = Choose One
5 = Main Menu
6 = Exit
''')
if method == '1':
logging.info("Response: 1 - Create One\n")
insert_comment(p_id)
elif method == '2':
logging.info("Response: 2 - Choose One\n")
c_id = input("Please input id of the one you choose:")
show_comment(c_id)
elif method == '3':
logging.info("Response: 3 - Delete One\n")
delete_comment(p_id)
elif method == '4':
logging.info("Response: 4 - Update One\n")
update_comment(p_id);
elif method == '6':
logging.info("Response: 6 - Exit\n")
exit()
else:
logging.info("Response: 5 - Main Menu\n")
main_menu()
def insert_post(s_id):
global new_p_id
logging.info("insert_post request user response - Please input your post\n")
content = input("Please input your post:")
logging.info("Response: ")
logging.info(content)
logging.info("\n")
post.insert(new_p_id,content,s_id,this_u_id)
new_p_id += 1
show_posts(s_id)
def show_post():
logging.info("show_post request user response - Please input id of the one you choose\n")
p_id = input("Please input id of the one you choose:")
logging.info("Response: ")
logging.info(p_id)
logging.info("\n")
_post= post.read_one(p_id)
print (_post)
comment_and_vote_menu(p_id)
def comment_and_vote_menu(p_id):
print("-" * 40)
print(" Comment/Vote Menu")
print("-" * 40)
logging.info("Print comment_and_vote_menu menu - Request user response\n")
method = input('''
1 = Show Comments
2 = Show Votes
3 = Main Menu
4 = Exit
''')
if method == '1':
logging.info("Response: 1 - Show Comments\n")
show_comments(p_id)
elif method == '2':
logging.info("Response: 2 - Show votes\n")
show_votes('post', p_id)
elif method == '4':
logging.info("Response: 4 - Exit\n")
exit()
else:
logging.info("Response: 3 - Main Menu\n")
main_menu()
def delete_post(s_id):
p_id = input("Please input id of the one you choose:")
post.delete(p_id)
show_posts(s_id)
def update_post(s_id):
logging.info("update_post request user response - Please input id of the one you choose\n")
p_id = input("Please input id of the one you choose:")
logging.info("Response: ")
logging.info(p_id)
logging.info("\n")
logging.info("update_post request usr response - Please input your post\n")
content = input("Please input your post:")
logging.info("Reponse: ")
logging.info(content)
logging.info("\n")
post.update(p_id,content)
show_posts(s_id)
def show_posts(s_id):
print("Posts:")
post.read_all(s_id)
# print(posts)
print("-" * 40)
print(" Post Menu")
print("-" * 40)
logging.info("Print show_posts menu - Request user response\n")
if admin == True:
method = input('''
1 = Create One
2 = Choose One
3 = Delete One
4 = Update One
5 = Main Menu
6 = Exit
''')
else:
method = input('''
1 = Create One
2 = Choose One
5 = Main Menu
6 = Exit
''')
if method == '1':
logging.info("Response: 1 - Create One\n")
insert_post(s_id)
elif method == '2':
logging.info("Response: 2 - Choose One\n")
show_post()
elif method == '3':
logging.info("Response: 3 - Delete One\n")
delete_post(s_id)
elif method == '4':
logging.info("Response: 4 - Update One\n")
update_post(s_id);
elif method == '6':
logging.info("Response: 6 - Exit\n")
exit()
else:
logging.info("Response: 5 - Main Menu\n")
main_menu()
def insert_seenit():
global new_s_id
logging.info("insert_seenit request user response - Please describe your seenit\n")
category = input("Please describe your seenit:")
logging.info("Response: ")
logging.info(category)
logging.info("\n")
seenit.insert(new_s_id, category, this_u_id)
new_s_id += 1
show_seenits()
def show_seenit():
logging.info("show_seenit request user reponse - Please input id of the one you choose\n")
s_id = input("Please input id of the one you choose:")
logging.info("Response: ")
logging.info(s_id)
logging.info("\n")
_seenit = seenit.read_one(s_id)
print(_seenit)
show_posts(s_id)
def delete_seenit():
logging.info("delete_seenit request user reponse - Please input id of the one you choose\n")
s_id = input("Please input id of the one you choose:")
logging.info("Response: ")
logging.info(s_id)
logging.info("\n")
seenit.delete(s_id)
show_seenits()
def update_seenit():
logging.info("update_seenit request user response - Please input id of the one you choose\n")
s_id = input("Please input id of the one you choose:")
logging.info("Response: ")
logging.info(s_id)
logging.info("\n")
logging.info("update_seenit request user response - Please decribe your seenit\n")
category = input("Please describe your seenit:")
logging.info("Response: ")
logging.info(category)
logging.info("\n")
seenit.update(s_id, category)
show_seenits()
def show_seenits():
seenit.read_all()
print("-" * 40)
print(" Seenit Menu")
print("-" * 40)
logging.info("Print show_seenits menu - Request user reponse\n")
if admin == True:
method = input('''
1 = Create One
2 = Choose One
3 = Delete One
4 = Update One
5 = Main Menu
6 = Exit
''')
else:
method = input('''
1 = Create One
2 = Choose One
5 = Main Menu
6 = Exit
''')
if method == '1':
logging.info("Response: 1 - Create One\n")
insert_seenit()
elif method == '2':
logging.info("Response: 2 - Choose One\n")
show_seenit()
elif method == '3':
logging.info("Response: 3 - Delete One\n")
delete_seenit()
elif method == '4':
logging.info("Response: 4 - Update One\n")
update_seenit();
elif method == '6':
logging.info("Response: 6 - Exit\n")
exit()
else:
logging.info("Response: 5 - Main Menu\n")
main_menu()
def account_profile():
global this_u_id
user_info = user.read_one(this_u_id)
print("My Account Information:")
print(user_info)
print("-" * 40)
print(" Account Menu")
print("-" * 40)
logging.info("Print account_profile menu - Request user response\n")
if admin == True:
method = input('''
1 = Update My Account Info
2 = Delete My Account
3 = Show All Accounts
4 = Main Menu
5 = Exit
''')
else:
method = input('''
1 = Update My Account Info
2 = Delete My Account
4 = Main Menu
5 = Exit
''')
if method == '1':
logging.info("Response: 1 - Update Account Info\n")
logging.info("Response 1 request user response\n")
x = input("user name: ")
z = getpass.getpass('Password:')
y = input("email: ")
logging.info("user name: ")
logging.info(x)
logging.info(" password: ")
logging.info(z)
logging.info(" email: ")
logging.info(y)
logging.info("\n")
user.update(this_u_id, x, z, y)
account_profile()
elif method == '2':
logging.info("Response: 2 - Delete My Account\n")
user.delete(this_u_id)
this_u_id = 0
main_menu()
elif method == '3':
logging.info("Response: 3 - Show All Accounts\n")
user.read_all()
# print(all_users)
account_profile()
elif method == '5':
logging.info("Response: 5 - Exit\n")
exit()
else:
logging.info("Response: 4 - Main Menu\n")
main_menu()
def logout():
global this_u_id, admin
this_u_id = 0
admin = False
main_menu()
def print_main_menu():
# MENU DESIGN!
print (28 * "-", "MAIN MENU", 33 * "-")
print ('|', 25 * " ", "0. Account Profile", 23 * " ", "|")
print ('|', 25 * " ", "2. Show Seenits", 26 * " ", "|")
print ('|', 25 * " ", "3. Logout", 32 * " ", "|")
print ('|', 25 * " ", "4. Exit", 34 * " ", "|")
print (72 * "-")
def print_Welcome_menu():
# MENU DESIGN!
print (33 * "~", "Welcome", 33 * "~")
print ('|', 27 * " ", "User ID is", this_u_id, 29 * " ", "|")
print ('|', 2 * " ", "(0 means not logged in yet, other number means logged in already)", 1 * " ", "|")
print (72 * "~")
def print_login_menu():
# MENU DESIGN!
print (28 * "-", "MAIN MENU", 33 * "-")
print ('|', 25 * " ", "1. Login", 33 * " ", "|")
print ('|', 25 * " ", "4. exit", 34 * " ", "|")
print (72 * "-")
def print_register_menu():
# MENU DESIGN!
print (27 * "-", "LOGIN MENU", 33 * "-")
print ('|', 25 * " ", "1. Register", 30 * " ", "|")
print ('|', 25 * " ", "2. Already A Member", 22 * " ", "|")
print ('|', 25 * " ", "3. Main Menu", 29 * " ", "|")
print ('|', 25 * " ", "4. Exit", 34 * " ", "|")
print (72 * "-")
def main_menu():
logging.info("Printing main menu - Request user response\n")
print_Welcome_menu()
if this_u_id:
print_main_menu()
method = input()
else:
print_login_menu()
method = input()
if method == '1':
logging.info("Response: 1 - Login\n")
login()
elif method == '2':
logging.info("Response: 2 - Show Seenits\n")
show_seenits()
elif method == '0':
logging.info("Response: 0 - Account Profile\n")
account_profile()
elif method == '3':
logging.info("Response: 3 - Logout\n")
logout()
else:
logging.info("Response: 4 - Exit\n")
exit()
# build_database()
main_menu()