-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDocumentViewerPage.py
256 lines (223 loc) · 11.7 KB
/
DocumentViewerPage.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
import tkinter as tk
import pandas as pd
import DocumentsManager
import AccountsManager
import InvitationsManager
import ComplaintsManager
from tkinter import simpledialog
import getpass
import os
class DocumentViewerPage(tk.Frame):
def __init__(self, parent, controller):
self.username = controller.get_username()
self.userid = controller.get_userid()
self.docid = controller.opened_docid
self.controller = controller
self.doc_info = DocumentsManager.get_doc_info(self.docid)
self.doc_versions = DocumentsManager.get_doc_old_versions(self.docid)
self.owner_name = AccountsManager.get_username(self.doc_info['owner_id'])
tk.Frame.__init__(self, parent)
self.lock_status_var = tk.StringVar(self)
self.title_var = tk.StringVar(self)
self.content_var = tk.StringVar(self)
self.modified_time_var = tk.StringVar(self)
self.modified_user_var = tk.StringVar(self)
self.last_modified_var = tk.StringVar(self)
self.scope_var = tk.StringVar(self)
lock_status_label = tk.Label(self, textvariable=self.lock_status_var, fg="grey")
last_modified_label = tk.Label(self, textvariable=self.last_modified_var, fg="grey")
scope_label = tk.Label(self, textvariable=self.scope_var, fg="grey")
owner_label = tk.Label(self, text="Owned by {}".format(self.owner_name), fg="green")
label_type = tk.Label(self, text="® FourofUS 2018", fg="gray", font=controller.footer_font)
label1 = tk.Label(self, text="ShareWithME")
label1.config(font=("Courier", 35, 'bold'))
label_title = tk.Label(self, text="Title")
label_content = tk.Label(self, text="Content")
self.title = tk.Text(self, height=1, width=30, highlightbackground="black", highlightcolor="black",
highlightthickness=1,
font=("Times New Roman", 18))
self.content = tk.Text(self, height=25, width=60, highlightbackground="black", highlightcolor="black",
highlightthickness=1, )
# For version history drop down
self.version_var = tk.StringVar(self)
self.selected_version = ''
self.doc_versions_list = []
self.versions_drop_down = tk.OptionMenu(self, self.version_var, None)
complain_button = tk.Button(self, text="Complain", command=lambda: controller.show_warning() if controller.is_warned else self.complain_doc())
back_button = tk.Button(self, text="Back", command=lambda: controller.show_warning() if controller.is_warned else self.destroy())
download_button = tk.Button(self, text="Download", command=lambda: controller.show_warning() if controller.is_warned else self.download_file())
n = 150
m = 50
label_type.pack(side=tk.BOTTOM)
label1.pack(side=tk.TOP, ipady=20)
label_title.place(x=n - 120, y=m + 10)
self.title.place(x=n - 120, y=m + 30)
label_content.place(x=n - 120, y=m + 70)
self.content.place(x=n - 120, y=m + 90)
self.versions_drop_down.place(x=n + 325, y=m + 20)
download_button.place(x=n + 325, y=m * 9)
complain_button.place(x=n + 325, y=m * 7)
back_button.place(x=n + 325, y=m * 8)
lock_status_label.place(x=n + 70, y=m * 10 + 20)
last_modified_label.place(x=n + 70, y=m * 10 + 40)
scope_label.place(x=n - 40, y=m + 70)
owner_label.place(x=n + 160, y=m + 70)
# display doc info
self.refresh_content()
def download_file(self):
content = self.content.get(1.0, tk.END)
title = self.title.get(1.0, tk.END)
title = title.strip()
os_username = getpass.getuser()
path = "/users/" + os_username + "/downloads/" + title
if os.path.isfile(path+".txt"):
for i in range(1, 9999):
new_path = path + " ({})".format(i)
if os.path.isfile(new_path+".txt"):
print("there is the file")
continue
else:
temp_file = open(new_path+".txt", "w")
break
else:
temp_file = open(path+".txt", "w")
print(content, file=temp_file)
temp_file.close()
tk.messagebox.showinfo("","File successfully downloaded!")
def fetch_title_and_content(self):
# delete old content and insert new content
self.doc_info = DocumentsManager.get_doc_info(self.docid)
if self.doc_info['current_seq_id'] != '-':
self.title.delete(1.0, tk.END)
self.title.insert(tk.INSERT, self.filter_taboo_words(str(self.doc_info['title']), ' '))
self.content.delete(1.0, tk.END)
self.content.insert(tk.INSERT, self.filter_taboo_words(str(self.doc_info['content']), '\n'))
else:
# do not filter the initial title upon creating new doc
self.title.delete(1.0, tk.END)
self.title.insert(tk.INSERT, self.doc_info['title'])
def fetch_status(self):
self.doc_info = DocumentsManager.get_doc_info(self.docid)
# update lock status
if self.doc_info['is_locked'] == False:
self.lock_status_var.set("Document is unlocked")
else:
locker = DocumentsManager.get_locker(self.docid)
self.lock_status_var.set("Document is currently locked by {}".format(locker['name']))
# update scope
self.scope_var.set("This is a {} document".format(self.doc_info['scope']))
# update last modifier and time
self.last_modified_var.set(
"Last modified by {} at {}".format(AccountsManager.get_username(int(self.doc_info['modified_by'])),
self.doc_info['modified_at'])
)
def fetch_old_versions(self):
self.doc_versions = DocumentsManager.get_doc_old_versions(self.docid)
if self.doc_info['current_seq_id'] != '-':
current_seq_id = self.doc_info['current_seq_id']
self.doc_versions_list = ['Version {}'.format(current_seq_id.split('-')[1])]
self.selected_version = self.doc_versions_list[0]
self.version_var.set(self.doc_versions_list[0]) # initial selected version is current version
if not self.doc_versions.empty:
print(self.doc_versions)
for seq_id, row in self.doc_versions.iterrows():
self.doc_versions_list.append('Version {}'.format(seq_id.split('-')[1]))
print(self.doc_versions_list)
self.versions_drop_down['menu'].delete(0, tk.END)
for version in self.doc_versions_list:
self.versions_drop_down['menu'].add_command(label=version,
command=lambda value=version: self.version_selected(value))
def refresh_content(self):
# Can use this function to refresh content
self.fetch_title_and_content()
self.fetch_status()
self.fetch_old_versions()
def version_selected(self, value):
# retrieve selected version:
self.version_var.set(value)
self.selected_version = value
selected_seq_id = '{}-{}'.format(self.docid, value.split()[1])
self.content.delete(1.0, tk.END)
# if selected version is current version then display current content
if selected_seq_id == self.doc_info['current_seq_id']:
self.content.insert(tk.INSERT, self.filter_taboo_words(self.doc_info['content'], '\n'))
self.fetch_status()
else:
old_version_content = DocumentsManager.retrieve_old_version(selected_seq_id)
self.content.insert(tk.INSERT, self.filter_taboo_words(old_version_content, '\n'))
old_version_info = DocumentsManager.get_old_version_info(selected_seq_id)
modifier = AccountsManager.get_username(old_version_info['modified_by'])
modified_time = old_version_info['modified_at']
self.last_modified_var.set(
"Last modified by {} at {}".format(modifier, modified_time)
)
def filter_taboo_words(self, content, separator):
'''separator is blank space if content is doc title
separator is newline if content is doc content
This function returns a filtered content without appearance of taboo words'''
words_used = content.split(separator)
taboo_words = self.get_taboo_words()
filtered_content = []
for word in words_used:
if word in taboo_words:
filtered_content.append('UNK')
else:
filtered_content.append(word)
if separator == ' ':
return ' '.join(filtered_content)
else:
return '\n'.join(filtered_content)
def get_taboo_words(self):
taboo_db = pd.read_csv("database/TabooWords.csv")
taboo_list = taboo_db.loc[taboo_db['approved'] == True]['word'].tolist()
return taboo_list
def complain_doc(self):
if self.doc_info['scope'] == 'Private' or DocumentsManager.is_owner(self.userid, self.docid):
tk.messagebox.showerror("", "You cannot complain your own document.")
elif self.controller.is_guest() or self.doc_info['scope'] == 'Public':
self.complain_to_su()
else:
answer = tk.messagebox.askyesno("", "Do you want to complain about this version to the owner?")
if answer:
self.complain_to_owner()
else:
answer = tk.messagebox.askyesno("", "Then do you want to complain about the owner to the Super User?")
if answer:
self.complain_to_su()
def complain_to_owner(self):
if self.doc_info['current_seq_id'] == '-':
tk.messagebox.showerror("", "This is a new document. You cannot complain if there is no update.")
return
selected_seq_id = '{}-{}'.format(self.docid, self.selected_version.split()[1])
if self.doc_info['current_seq_id'] == selected_seq_id:
updater = self.doc_info['modified_by']
else:
updater = DocumentsManager.get_old_version_info(selected_seq_id)['modified_by']
complaint = tk.simpledialog.askstring("Complain to Owner", "Please enter your complaint about this version.\n"
"If this is not the version you want to complain about,\ngo back to the"
"document page and select the correct version.")
if complaint is not None:
complaint_info = {
'receiver_id': self.doc_info['owner_id'],
'complaint_type': 'to_owner',
'complainee_id': updater,
'seq_id': selected_seq_id,
'content': complaint
}
ComplaintsManager.add_complaint(self.userid, complaint_info)
tk.messagebox.showinfo("", "Your complaint has been sent to the owner.")
def complain_to_su(self):
selected_seq_id = '{}-{}'.format(self.docid, self.selected_version.split()[1])
complaint = tk.simpledialog.askstring("Complain to Super User", "Please enter your complaint about this document: ")
if complaint is not None:
super_users = AccountsManager.get_all_super_users()
for su in super_users:
complaint_info = {
'receiver_id': su,
'complaint_type': 'to_su',
'complainee_id': DocumentsManager.get_doc_info(self.docid)['owner_id'],
'seq_id': selected_seq_id,
'content': complaint,
}
ComplaintsManager.add_complaint(self.userid, complaint_info)
tk.messagebox.showinfo("", "Your complaint has been recorded!")