def read_data(self): """method to display notes of a particular user""" # get 2 instances of the note, 1 to be returned, the second to be modified for easy viewing for user note1 = Note.find_all(self.username) note2 = Note.find_all(self.username) if len(note2) != 0: # modifying note for ease of referencing by adding a s/n attribute num = 0 for item in note2: num += 1 item["S/N"] = num return_note = note2 # modifying note for easy reading by viewer by changing id to serial number and shortening date num = 0 for item in note1: num += 1 item["_id"] = num item["created_date"] = str(item["created_date"]).split(" ")[0] print(tabulate(tabular_data=note1, headers="keys", tablefmt="rst")) return return_note else: print("Sorry, No Notes Found")
def delete_note(self, note_id): # delete note try: self.note = Note().get(note_id) self.note.delete() except Exception as e: logger.error(e) raise Exception(Message.DELETE_NOTE_FAILED)
def write_data(self): """method to create data for user""" title = input("Title: ") content = input("Content: ") author = self.username note = Note(title, content, author) note.save_to_mongo() print("\nEntry added to database")
def add_note(): title = request.form['title'] description = request.form['content'] note = Note(title=title, description=description) note.save_to_mongo() return redirect(url_for('index'))
class NoteService: """NoteService for creating/editing/getting/deleting note. """ def __init__(self): super().__init__() def create_note(self, user_id, title, content, note_type): # creating note for user_id, with informations: title, content and note_type try: self.note = Note( id=str(uuid.uuid4()), user_id=user_id, title=title, content=content, note_type=note_type, created_by=user_id ) return self.note.add() except Exception as e: logger.error(e) raise Exception(Message.CREATE_NOTE_FAILED) def edit_note(self, note_id, title, content, note_type, user_id): # editing note with changing informations: title, content, note_type, user_id try: self.note = Note().get(note_id) self.note.title = title self.note.content = content self.note.note_type = note_type self.note.updated_by = user_id self.note.updated_at = datetime.now() self.note.update() except Exception as e: logger.error(e) raise Exception(Message.EDIT_NOTE_FAILED) def delete_note(self, note_id): # delete note try: self.note = Note().get(note_id) self.note.delete() except Exception as e: logger.error(e) raise Exception(Message.DELETE_NOTE_FAILED) def get_user_notes(self, user_id): # get all note of user try: user_notes = Note().get_user_notes(user_id) return user_notes except Exception as e: logger.error(e) raise Exception(Message.GET_USER_NOTES_FAILED)
def edit_note(self, note_id, title, content, note_type, user_id): # editing note with changing informations: title, content, note_type, user_id try: self.note = Note().get(note_id) self.note.title = title self.note.content = content self.note.note_type = note_type self.note.updated_by = user_id self.note.updated_at = datetime.now() self.note.update() except Exception as e: logger.error(e) raise Exception(Message.EDIT_NOTE_FAILED)
def create_note(self, user_id, title, content, note_type): # creating note for user_id, with informations: title, content and note_type try: self.note = Note( id=str(uuid.uuid4()), user_id=user_id, title=title, content=content, note_type=note_type, created_by=user_id ) return self.note.add() except Exception as e: logger.error(e) raise Exception(Message.CREATE_NOTE_FAILED)
def get_user_notes(self, user_id): # get all note of user try: user_notes = Note().get_user_notes(user_id) return user_notes except Exception as e: logger.error(e) raise Exception(Message.GET_USER_NOTES_FAILED)
def delete_data(self): """method to delete note from database""" note_list = self.read_data() if note_list is not None: try: # prompt user for choice num = int(input("Enter Id of Note to Delete: ")) # get id for note for note in note_list: if note["S/N"] == num: id = note["_id"] Note.delete(id) print("Entry has been deleted") except: print("Error occurred during delete, check Id and try again")
def edit_data(self): """method to edit title or content of a note""" note_list = self.read_data() if note_list is not None: try: num = int(input("Enter ID of Note to Edit: ")) # get id from serial number for note in note_list: if note["S/N"] == num: id = note["_id"] # get note using id response = Note.find_one(id) note = Note(**response) # get key match that will be used for edit current_title = note.title current_content = note.content # prompt user to edit title or content ans = input("Edit\n1. Title\n2. Content\n") ans = ans.strip() # edit title or content based on user choice if ans == "1": title = input("Enter new title: ") note.title = title note.update_mongo(match={"title": current_title}) print("Title successfully edited") elif ans == "2": content = input("Enter new content: ") note.content = content note.update_mongo(match={"content": current_content}) print("Content successfully edited") else: print("Invalid option selected") except: print("Error occurred during edit, check Id and try again")
def index(): notes = Note.all() return render_template('index.html', notes=notes)