def accept_invitation(invitee_id, docid):
    '''This function removes the invitation from invitations db'''
    invitations_db = pd.read_csv(path_to_invitations_db)
    index = get_invitation_index(invitee_id, docid)
    invitations_db.drop(index=index, inplace=True)
    invitations_db.to_csv(path_to_invitations_db, index=False)
    DocumentsManager.add_contributor(invitee_id, docid)
def send_invitation(invitee_id, docid):
    '''This function records an invitation in db'''
    df = pd.DataFrame({
        'inviter_id': [DocumentsManager.get_doc_info(docid)['owner_id']],
        'invitee_id': [invitee_id],
        'doc_id': [docid],
        'time': [DocumentsManager.create_time_object()],
        'accepted': [False]
    })
    with open(path_to_invitations_db, 'a') as invitations_db:
        df.to_csv(invitations_db, index=False, header=False)
 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 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 open_doc(self):
     if self.selected_docid:
         self.controller.opened_docid = self.selected_docid
         DocumentsManager.inc_views_count(self.selected_docid)
         if self.controller.get_usertype() == 'Guest':
             self.controller.create_doc_viewer_page()
         else:
             if DocumentsManager.is_owner(self.userid, self.selected_docid):
                 self.controller.create_doc_owner_page()
             elif DocumentsManager.is_contributor(self.userid,
                                                  self.selected_docid,
                                                  self.controller.is_su()):
                 self.controller.create_doc_editor_page()
             elif DocumentsManager.is_viewer(self.selected_docid):
                 self.controller.create_doc_viewer_page()
             else:
                 tk.messagebox.showerror(
                     "", "Sorry, you don't have access to this document!")
     else:
         tk.messagebox.showerror("", "Please select a document!")
 def is_on_warning_list(self):
     bad_doc = AccountsManager.is_warned(self.__userid)
     if bad_doc:
         self.bad_docid = bad_doc['bad_docid']
         self.bad_doc_title = bad_doc['bad_doc_title']
         self.opened_docid = self.bad_docid
         self.is_warned = True
         if DocumentsManager.is_owner(self.__userid, self.bad_docid):
             self.create_doc_owner_page()
             self.show_frame("DocumentOwnerPage")
         else:
             self.create_doc_editor_page()
             self.show_frame("DocumentEditorPage")
         return True
 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 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 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_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!")
    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 __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 fetch_docs_for_home_page(self):
     if self.controller.get_usertype() == 'Guest':
         docs = DocumentsManager.get_docs_for_gu()
     else:
         docs = DocumentsManager.get_docs_for_ou(self.userid)
     self.refresh_doc_section(docs)