Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
762 lines (640 loc) · 28.4 KB
/
Copy pathmain.py
File metadata and controls
762 lines (640 loc) · 28.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
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
#Written by Leah Gibbons, with contributions and testing from Bea Sauve
from profile_manager import ProfileManager
PROFILES_DATA_FILE_PATH = "data/profiles.csv"
def main():
"""Displays welcome banner and login menu."""
manager = ProfileManager() #ProfileManager and its data will persist across users
close_program = False
print("*-----*-----*-----*-----*-----*-----*-----*-----*")
print("* Welcome to the Glubs Social Media Network! *")
print("*-----*-----*-----*-----*-----*-----*-----*-----*")
#Basic loop concept for this menu and other menus in this program:
#The menu will display information.
#Then the user will be prompted for input which will cause the program to take actions, display more information, and re-prompt for further input.
#This process will loop forever until a special user input is received which causes the function to return.
while True:
if close_program:
print("Goodbye!")
return
current_user = None #user identity cleared between logins
print("How would you like to login?")
print("1. Login as user")
print("2. Login as admin")
print("99. Exit the program.")
user_input = input(
"Enter your selection: "
).strip().lower()
if user_input == "1":
current_user = login_user(manager)
if current_user != "":
close_program = display_user_menu(manager, current_user)
else:
print("Login failed. Please try again.")
elif user_input == "2":
current_user = login_user(manager)
if current_user != "":
is_admin = admin_verification() #verify every time someone tries to login as admin
if is_admin:
close_program = display_admin_menu(manager, current_user)
else:
print("Unable to verify admin credentials. Please login again.")
else:
print("Login failed. Please try again.")
elif user_input == "99":
close_program = True
else:
print("Sorry, I didn't recognize that input. Please try again: ")
def login_user(manager):
"""Prompts the user to enter their name. New users will be required to create a profile.
:type ProfileManager: the session's ProfileManager
:rtype string: the user's name"""
username = input(
"Please enter your name: "
).strip().title()
if username == "99":
print("Cheeky!")
username = "Ninety-Nine"
if manager.contains_profile(username):
print(f"Welcome back, {username}!")
return username
else:
print(f"Welcome, {username}!")
profile_created = create_profile(manager, username)
if profile_created:
return username
else:
return ""
def admin_verification():
"""Password verification for admin logins.
:rtype boolean: True if admin credentials were successfully verified, False otherwise.
"""
while True:
print("Administrators are required to enter a PASSWORD.")
print("(The password is uncrackable)")
user_input = input(
"Enter the password, or enter 99 to return to the login menu: "
).strip().lower()
if user_input == "uncrackable":
return True
elif user_input == "99":
return False
else:
print("Incorrect password. Please try again: ")
def display_user_menu(manager, current_user):
"""Displays menu options for regular users.
:type ProfileManager: the session's ProfileManager
:type string: current_user, the user who is logged in.
:rtype boolean: True if the user chooses to quit the program, False to send them back to the login menu.
"""
user_was_deleted = False
user_active_status(manager, current_user, True)
while True:
#If the user deleted their profile, force logout.
if user_was_deleted:
print("Logging out...")
return False
print(f"User Menu: Logged in as {current_user}")
print("1. Modify your profile")
print("2. View a profile")
print("3. Add a friend")
print("4. View your friend list")
print("5. View a friend's friend list")
print("6. Delete your profile.")
print("7. Switch the current user.")
print("8. Create graph of current user's network.")
print("9. Logout (end program)")
user_input = input(
"Select your menu option: "
).strip().lower()
if user_input == "1":
# This option can change the current user's name, so we need to always check for that.
current_user = modify_profile(manager, current_user)
elif user_input == "2":
display_user_profile(manager, current_user)
elif user_input == "3":
add_friend(manager, current_user)
elif user_input == "4":
view_friends(manager, current_user)
elif user_input == "5":
view_friends_of_friends(manager, current_user)
elif user_input == "6":
user_was_deleted = delete_profile(manager, current_user)
elif user_input == "7":
user_active_status(manager,current_user, False)
print("Logging out as current user...")
return False
elif user_input == "8":
create_user_network_graph(manager, current_user)
elif user_input == "9":
return True
else:
print("Sorry, I didn't recognize that input. Please try again: ")
def display_admin_menu(manager, current_user):
"""Displays menu options for users with admin persmissions.
:type ProfileManager: the session's ProfileManager
:type string: current_user, the user who is logged in.
:rtype boolean: True if the user chooses to quit the program, False to send them back to the login menu.
"""
user_was_deleted = False
#Set user activity status
user_active_status(manager, current_user, True)
while True:
#If the user deleted their profile, force logout.
if user_was_deleted:
print("Logging out...")
return False
print(f"Admin Menu: Logged in as {current_user}")
print("1. Create a profile")
print("2. Modify profile")
print("3. View a user's profile information")
print("4. View all profiles")
print("5. Add a friend")
print("6. View your friend list")
print("7. View anyone's friend list")
print("8. Delete a profile")
print("9. Switch the current user")
print("10. Read profiles from CSV")
print("11. Create graph of current user's network")
print("12. Logout (end program)")
user_input = input("Select your menu option: ").strip().lower()
if user_input == "1":
create_profile(manager, current_user, True)
elif user_input == "2":
#This option can change the current user's name, so we need to always check for that.
current_user = modify_profile(manager, current_user, True)
elif user_input == "3":
display_user_profile(manager, current_user)
elif user_input == "4":
view_all_profiles(manager)
elif user_input == "5":
add_friend(manager, current_user)
elif user_input == "6":
view_friends(manager, current_user)
elif user_input == "7":
view_friends_of_friends(manager, current_user, True)
elif user_input == "8":
delete_profile(manager, current_user, True)
elif user_input == "9":
user_active_status(manager,current_user,False)
print("Logging out as current user...")
return False
elif user_input == "10":
read_profiles_from_csv(manager)
elif user_input == "11":
create_user_network_graph(manager, current_user)
elif user_input == "12":
return True
else:
print("Sorry, I didn't recognize that input. Please try again: ")
# ****************
# | MENU OPTIONS |
# ****************
def create_profile(manager, current_user, is_admin=False):
"""Creates a new user profile with several prompts for user input.
:type ProfileManager: the session's ProfileManager
:type string: current_user, the user who is logged in.
:type boolean: True if the user has admin permissions, False otherwise.
:rtype boolean: True if a user successfully created their own profile, False otherwise.
"""
#Regular users can only access this method when creating their own profile.
if not is_admin and manager.contains_profile(current_user):
print("A profile already exists under this name.")
print("Returning to main menu...")
return False
#Admins can create profiles on behalf of others.
elif manager.contains_profile(current_user):
print("Admin: You are creating a new profile for someone else.")
print("Let's get their information!")
new_user = input(
"What is their name? "
).strip().title()
location = input(
"What is their location? "
).strip().title()
relationship_status = validate_relationship_input()
if relationship_status == "":
print("Skipping adding relationship status.")
age = validate_integer_input("What is their age? ")
occupation = input(
"What is their occupation? "
).strip().title()
astrological_sign = input(
"What is their astrological sign? "
).strip().title()
status = input(
"What are they up to right now? "
).strip()
if new_user == "99":
new_user = "Ninety-Nine"
manager.add_profile(new_user, location, relationship_status, age, occupation, astrological_sign, status)
print(f"Success! A new profile has been created under the name {new_user}")
while True:
print("Would you like to add a profile picture?")
user_input = input(
"Enter 1 for yes or 2 for no: "
)
if user_input == "1":
new_profile = manager.get_profile(current_user)
photo = validate_jpg()
if photo != "":
new_profile.add_photo(photo)
print("Success! Their profile picture has been added")
return False
elif user_input == "2":
print("Skipping adding profile picture...")
return False
else:
print("Sorry, I didn't understand that. Please try again.")
#Initial profile creation for the current user.
else:
print("Wonderful! You would like to create a new profile.")
print("Let's get some information about you!")
location = input(
"What is your location? "
).strip().title()
relationship_status = validate_relationship_input()
if relationship_status == "":
print("Skipping adding relationship status.")
age = validate_integer_input("What is your age? ")
occupation = input(
"What is your occupation? "
).strip().title()
astrological_sign = input(
"What is your astrological sign? "
).strip().title()
status = input(
"What are you up to right now? "
).strip()
manager.add_profile(current_user, location, relationship_status, age, occupation, astrological_sign, status)
print(f"Success! Your profile has been created under the name {current_user}")
while True:
print("Would you like to add a profile picture?")
user_input = input(
"Enter 1 for yes or 2 for no: "
)
if user_input == "1":
new_profile = manager.get_profile(current_user)
photo = validate_jpg()
if photo != "":
new_profile.add_photo(photo)
print("Success! Your profile picture has been added")
return True
elif user_input == "2":
print("Skipping adding profile picture...")
return True
else:
print("Sorry, I didn't understand that. Please try again.")
def modify_profile(manager, current_user, is_admin=False):
"""Allows the user to modify their profile, or for administrators, to modify any profile.
:type ProfileManager: the session's ProfileManager
:type string: current_user, the user who is logged in.
:type boolean: True if the user has admin permissions, False otherwise.
:rtype string: the current user's name
"""
#Admins are asked whether they want to modify their own or someone else's profile.
if is_admin:
user_to_modify = admin_user_selection(manager, current_user, "Whose profile would you like to modify?")
if user_to_modify == "":
return current_user
else:
user_to_modify = current_user
while True:
print(f"You are modifying {user_to_modify}'s profile. What would you like to modify?")
print("1. Name")
print("2. Relationship status")
print("3. Status")
print("99. Return to main menu.")
user_input = input(
"Enter your choice: "
).strip().lower()
if user_input == "99":
return current_user
elif user_input == "1":
new_name = input(
"Enter the new name: "
).strip().title()
success = manager.change_name(user_to_modify, new_name)
if not success:
print("I'm sorry, we weren't able to make that name change.")
print("This may be because the old name wasn't found in our network,")
print("or because the new name already does exist in our network.")
#If the user has changed their own name, update their name in this method and in the return.
elif current_user == user_to_modify:
user_to_modify = new_name
current_user = new_name
elif user_input == "2":
relationship_status = validate_relationship_input()
if relationship_status != "":
manager.get_profile(user_to_modify).set_relationship(relationship_status)
elif user_input == "3":
new_status = input(
"Enter the new status: "
).strip().lower()
manager.get_profile(user_to_modify).set_status(new_status)
else:
print("Sorry, I didn't understand that. Please try again.")
def view_all_profiles(manager):
"""Allows the user to view a list of all profiles using either a breadth-first or a depth-first search.
:type ProfileManager: The session's ProfileManager
"""
while True:
print("What search method would you like to use to view all profiles in the network?")
print("1. Breadth-first search")
print("2. Depth-first search")
print("99. Return to main menu.")
user_input = input(
"Enter your selection: "
).strip().lower()
if user_input == "99":
return
elif user_input == "1":
print(manager.display_profiles("bfs"))
elif user_input == "2":
print(manager.display_profiles("dfs"))
else:
print("I'm sorry, I didn't understand that. Please try again.")
def display_user_profile(manager, current_user):
'''Gives user the option to display personal profile details or another users details
:type ProfileManager: the session's profile manager
:type string: current_user, the user who is logged in.
'''
user_to_view = admin_user_selection(manager, current_user, "Whose profile would you like to view?")
manager.display_profile_details(user_to_view)
def add_friend(manager, current_user):
"""Allows the user to add their friends.
:type ProfileManager: the session's ProfileManager
:type string: current_user, the user who is logged in.
"""
while True:
friend_to_add = input(
"Enter the name of the friend you would like to add, or enter 99 to return to the main menu: "
).strip().title()
#User may exit to main menu
if friend_to_add == "99":
return
#Disallow adding self as friend
elif friend_to_add == current_user:
print("You cannot add yourself as a friend! Would you like to try again?")
#Disallow adding friends not in the network
elif not manager.contains_profile(friend_to_add):
print("Sorry, this friend hasn't joined the Glubs Social Media Network yet!")
print("Would you like to enter the name of a different friend?")
#Disallow adding users who are already friends
elif friend_to_add in manager.get_friends_of_friends(current_user, "bfs"):
print("This user is already your friend! Would you like to add a different friend?")
#Add friend
else:
successful = manager.connect_profiles(current_user, friend_to_add)
if successful:
print(f"{friend_to_add} was successfully added as your friend!")
print("Would you like to add another friend?")
else:
print("Sorry, something went wrong! Would you like to try again?")
def view_friends(manager, current_user):
"""Allows the user to view their own friends using either BFS or DFS.
:type ProfileManager: the session's ProfileManager
:type string: current_user, the user who is logged in.
"""
while True:
print(f"What search method would you like to use to view your friends?")
print("1. Breadth-first search")
print("2. Depth-first search")
print("99. Return to main menu.")
user_input = input(
"Enter your selection: "
).strip().lower()
if user_input == "99":
return
elif user_input == "1":
print(manager.get_friends_of_friends(current_user, "bfs"))
elif user_input == "2":
print(manager.get_friends_of_friends(current_user, "dfs"))
else:
print("I'm sorry, I didn't understand that. Please try again.")
def view_friends_of_friends(manager, current_user, is_admin=False):
"""Allows the user to view a friend's friend list, or for admins, to view anyone's friend list.
:type ProfileManager: the session's ProfileManager
:type string: current_user, the user who is logged in.
:type boolean: True if the user has admin permissions, False otherwise.
"""
#Admins may choose to view their own or anyone else's friends.
if is_admin:
user_to_get_friends = admin_user_selection(manager, current_user, "Whose friends would you like to view? ")
if user_to_get_friends == "":
return
#Regular users may only view their friends' friend lists.
else:
user_to_get_friends = verify_user_is_friend(manager, current_user, "Whose friends would you like to view? ")
if user_to_get_friends == "":
return
while True:
print(f"What search method would you like to use to view the friends of {user_to_get_friends}?")
print("1. Breadth-first search")
print("2. Depth-first search")
print("99. Return to main menu.")
user_input = input(
"Enter your selection: "
).strip()
if user_input == "99":
return
elif user_input == "1":
print(manager.get_friends_of_friends(user_to_get_friends, "bfs"))
elif user_input == "2":
print(manager.get_friends_of_friends(user_to_get_friends, "dfs"))
else:
print("I'm sorry, I didn't understand that. Please try again.")
def delete_profile(manager, current_user, is_admin=False):
"""Allows the user to delete their profile, or for administrators, to delete any profile.
:type ProfileManager: the session's ProfileManager
:type string: current_user, the user who is logged in.
:rtype boolean: True if the user successfully deletes their own profile, False otherwise.
"""
#Admins may choose to delete their own or someone else's profile.
user_to_delete = current_user
if is_admin:
user_to_delete = admin_user_selection(manager, current_user, "Whose account would you like to delete?")
if user_to_delete == "99":
return False
while True:
#Give user a chance to change their mind before deleting the profile.
print(f"Really delete {user_to_delete}'s profile?")
print("1. Yes")
print("2. No (return to main menu)")
user_input = input(
"Enter your choice: "
).strip().lower()
#The user will be returned to the main menu after the profile is deleted or the deletion failed.
#If they deleted their own profile, they will be logged out as well.
if user_input == "1":
success = manager.remove_profile(user_to_delete)
if success:
print(f"{user_to_delete}'s profile has been deleted.")
return user_to_delete == current_user
else:
print("Sorry, something went wrong and the deletion was unsuccessful. Returning to main menu.")
return False
elif user_input == "2":
return False
else:
print("Sorry, I didn't understand that. Please try again.")
def read_profiles_from_csv(manager):
"""Builds the social network from the provided csv file.
:type ProfileManager: the session's ProfileManager
:type string: current_user, the user who is logged in.
"""
print("Building profiles from data file...")
try:
manager.read_profiles_from_csv(PROFILES_DATA_FILE_PATH)
print("|=============================================================|")
print("Profiles successfully loaded from data file!")
print("Returning to main menu...")
print("|=============================================================|")
return
except FileNotFoundError:
print("Error: The file was not found. Unable to build profiles from csv.")
print("Returning to main menu...")
return
except Exception as e:
print(f"An unexpected error occurred: {e}. Unable to build profiles from csv.")
print("Returning to main menu...")
return
def create_user_network_graph(manager, current_user):
"""Creates a graph of the current user's network (self, friends, and friends of friends).
:type ProfileManager: the session's ProfileManager
:type string: current_user, the user who is logged in.
"""
while True:
print(f'1. Display your network')
print(f'2. Display the entire network')
print("99. Return to main menu.")
user_input = input(
"Enter your selection: ")
if user_input == "99":
return
elif user_input == '1':
manager.create_user_graph(current_user, 1)
elif user_input == '2':
manager.create_user_graph(current_user, None)
else:
print("Sorry, I didn't understand that. Please try again.")
def user_active_status(manager, current_user, active = bool):
'''Changes a users status to either online or offline
:type ProfileManager: the session's ProfileManager
:type string: current_user, the user who is logged in.
:type boolean: active, boolean indicates whether the user in online or offline
'''
user_profile = manager.get_profile(current_user)
user_profile.set_activity(active)
# **********************
# * INPUT VALIDATION *
# **********************
def validate_relationship_input():
"""Validates relationship status input.
:rtype string: The valid, recognized relationship status, or an empty string if the user exits without entering a valid input.
"""
#Permitted inputs and their mappings
relationship_dictionary = {
"1": "Single",
"single": "Single",
"2": "In a Relationship",
"in a relationship": "In a Relationship",
"3": "Married",
"married": "Married",
}
while True:
print("Current relationship status?")
print("1. Single")
print("2. In a relationship")
print("3. Married")
print("99. Return to previous menu without entering relationship status.")
user_input = input(
"Enter your selection: "
).strip().lower()
if user_input == "99":
return ""
elif user_input not in relationship_dictionary:
print("Sorry, I didn't understand that. Please try again.")
else:
return relationship_dictionary[user_input]
def validate_jpg():
"""Validates that a string is a jpg file name.
:rtype string: the valid jpg file name, or an empty string if the user exits without entering a valid input.
"""
while True:
user_input = input(
"Please enter the name of your jpg file, or enter 99 to return to the main menu. "
).strip()
if user_input.lower().endswith(".jpg"):
return user_input
elif user_input == "99":
return ""
else:
print("Photo must be a jpg file. Please try again.")
def validate_existing_username(manager):
"""Prompts the user for a valid username that exists in the current network.
:type ProfileManager: manager, the session's ProfileManager.
:rtype string: a valid username connected to an existing profile, or an empty string if the user exits.
"""
while True:
user_input = input(
"Please enter the username: "
).strip().title()
if user_input == "99":
return ""
elif manager.contains_profile(user_input):
return user_input
else: print("Sorry, that username does not exist on our network. Please try again, or enter 99 to return.")
def validate_integer_input(prompt):
"""Prompts the user for a valid integer input.
:type string: prompt, the specific prompt to be used
:rtype int: A valid integer
"""
while True:
user_input = input(prompt).strip()
try:
return int(user_input)
except ValueError:
print("Please enter a valid integer.")
def admin_user_selection(manager, current_user, prompt):
"""Allows an admin to choose whether they wish to perform an operation on themselves or on a different user in the network.
:type ProfileManager: the session's ProfileManager
:type string: current_user, the user who is logged in.
:type string: prompt, the specific prompt to be used
:rtype string: A valid username of someone on the network
"""
while True:
print(prompt)
print("1. Yours")
print("2. Someone else's")
print("99. Return to main menu.")
user_input = input("Enter your selection: ")
if user_input == "1":
return current_user
elif user_input == "2":
return(validate_existing_username(manager))
elif user_input == "99":
return ""
else:
print("Sorry, I didn't understand that. Please try again.")
def verify_user_is_friend(manager, current_user, prompt):
"""Prompts a user to choose a valid friend from their friends list.
:type ProfileManager: manager, the session's ProfileManager
:type string: current_user, the user who is logged in.
:type string: prompt, the specific prompt to be used.
:rtype string: A valid friend of the user, or an empty string if the user exits the menu.
"""
#Get the user's friend list
friend_list = manager.get_friends_of_friends(current_user, "bfs")
while True:
print(prompt)
friend_name = input("Enter your friend's name: ").strip().title()
if friend_name == "99":
return ""
elif friend_name in friend_list:
return friend_name
else:
print("Sorry, we couldn't find that person in your friends list.")
print("Please try again, or enter 99 to return.")
# Execute
if __name__ == "__main__":
main()