-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathview_message.py
207 lines (165 loc) · 7.24 KB
/
view_message.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
import json
# function to determine if there is a new message for a user
def at_least_1_new_message(user):
count = 0
with open("new_messages.json", 'r') as f:
messages = json.loads(f.read())
for x in messages:
if x["recipient"] == user.username:
count += 1
break
return count
# gets the user's new messages
def get_new_messages(user):
messages = []
# get the user's new messages
with open("new_messages.json", 'r') as f:
contents = json.loads(f.read())
for x in contents:
if x["recipient"] == user.username:
messages.append(x)
return messages
# gets the user's saved messages
def get_saved_messages(user):
messages = []
# get the user's saved messages
with open("saved_messages.json", 'r') as f:
contents = json.loads(f.read())
for x in contents:
if x["recipient"] == user.username:
messages.append(x)
return messages
def view_messages_ui(user, accounts):
while True:
# get the user's new messages
new_messages = get_new_messages(user)
# get the user's saved messages
saved_messages = get_saved_messages(user)
# if the user has either new or saved messages, print the view messages options
# otherwise, print you have no messages
if len(new_messages) > 0 or len(saved_messages) > 0:
print("\n ********* View Messages Options ********* \n")
# keep track of the number of options
count = 1
# if the user has new messages, print the new messages option
#if len(new_messages) > 0:
print(str(count) + ") View New Messages")
count += 1
# if the user has saved messages, print the saved messages option
#if len(saved_messages) > 0:
print(str(count) + ") View Saved Messages")
# count += 1
# print the quit option
print("q) Quit")
view_messages_selection = input("Select an Option: ")
# if the user selected to view new messages, go to the view new messages function
if view_messages_selection == "1":
view_new_messages(user, new_messages)
elif view_messages_selection == "2":
view_saved_messages(user, saved_messages)
elif view_messages_selection == "q":
return
else:
print("Unknown Selection, Try Again!")
else:
print("\n ** You have no messages, returning to messages menu **\n")
return
def view_new_messages(user, new_messages):
while True:
# if new messages length if greater than 0, print the new messages options
if len(new_messages) > 0:
# print the new messages
print("\n ********* New Messages ********* \n")
for num, message in enumerate(new_messages):
print("New message #"+str(num+1)+": ")
print("From: " + message["sender"] + "\n")
print("Message: " + message["message"] + "\n")
print("\n")
# print the view messages options
message_option = input("Enter a message number to save the message\nor q to quit and delete unsaved new messages:")
# try to convert the message option to an integer
try:
message_option = int(message_option)
# if the integer is in the range of the new messages, save the message
if message_option <= len(new_messages):
# save the message
save_message(user, new_messages[message_option-1])
# remove the message from the new messages list
new_messages.remove(new_messages[message_option-1])
else:
print("Unknown Selection, Try Again!")
except ValueError:
# if message option is q, quit and delete unsaved new messages
if message_option == "q":
print("\n ** Deleting unsaved new messages **\n")
delete_new_messages(user)
return
else:
print("Unknown Selection, Try Again!")
else:
print("\n ** You have no new messages, returning to messages menu **\n")
delete_new_messages(user)
return
def view_saved_messages(user, saved_messages):
while True:
# if new messages length if greater than 0, print the new messages options
# print(len(saved_messages))
if len(saved_messages) > 0:
# print the new messages
print("\n ********* Saved Messages ********* \n")
for num, message in enumerate(saved_messages):
print("New message #"+str(num+1)+": ")
print("From: " + message["sender"] + "\n")
print("Message: " + message["message"] + "\n")
print("\n")
# print the view messages options
message_option = input("Enter a message number to delete\n else q to quit")
# try to convert the message option to an integer
try:
message_option = int(message_option)
# if the integer is in the range of the new messages, save the message
if message_option <= len(saved_messages):
# remove the message from the saved messages list
saved_messages.remove(saved_messages[message_option-1])
delete_saved_message(user)
else:
print("Unknown Selection, Try Again!")
except ValueError:
# if message option is q, quit and delete unsaved new messages
if message_option == "q":
return
else:
print("Unknown Selection, Try Again!")
else:
print("\n ** You have no saved messages **\n")
return
# this function deletes all new messages for a user
def delete_new_messages(user):
with open("new_messages.json", "r") as fs:
new_messages = json.loads(fs.read())
new_messages = [i for i in new_messages if not (i['recipient'] == user.username)]
with open("new_messages.json", "w") as fs:
json.dump(new_messages, fs)
# this function deletes one single message for a user
def delete_saved_message(user):
with open("saved_messages.json", "r") as fs:
saved_messages = json.loads(fs.read())
# JSON File popping
for i in range(len(saved_messages)):
if saved_messages[i]['recipient'] == user.username:
saved_messages.pop(i)
break
with open("saved_messages.json", "w") as fs:
json.dump(saved_messages, fs)
# this function saves a message for a user by adding it to the saved messages file
def save_message(user, message):
with open("saved_messages.json", "r") as f:
messageInfo = json.loads(f.read())
savedMessage = {
"recipient": user.username,
"sender": message["sender"],
"message": message["message"]
}
messageInfo.append(savedMessage)
with open("saved_messages.json", "w") as f:
json.dump(messageInfo, f)