def create(person_id, note): """ This function creates a new note related to the passed in person id. :param person_id: Id of the person the note is related to :param note: The JSON containing the note data :return: 201 on success """ # get the parent person person = Person.query.filter(Person.person_id == person_id).one_or_none() # Was a person found? if person is None: abort(404, f"Person not found for Id: {person_id}") # Create a note schema instance schema = NoteSchema() new_note = schema.load(note, session=db.session).data # Add the note to the person and database person.notes.append(new_note) db.session.commit() # Serialize and return the newly created note in the response data = schema.dump(new_note).data return data, 201
def update(person_id, note_id, note): """ This function updates an existing note related to the passed in person id. :param person_id: Id of the person the note is related to :param note_id: Id of the note to update :param note: The JSON containing the note data :return: 200 on success """ update_note = (Note.query.filter(Person.person_id == person_id).filter( Note.note_id == note_id).one_or_none()) # Did we find an existing note? if update_note is not None: # turn the passed in note into a db object schema = NoteSchema() updated = schema.load(note, session=db.session) # Set the id's to the note we want to update updated.person_id = update_note.person_id updated.note_id = update_note.note_id # merge the new object into the old and commit it to the db db.session.merge(updated) db.session.commit() # return updated note in the response data = schema.dump(update_note) return data, 200 # Otherwise, nope, didn't find that note else: abort(404, f"Note not found for Id: {note_id}")
def read_all(): notes = Note.query.order_by(db.desc(Note.timestamp)).all() note_schema = NoteSchema(many=True) data = note_schema.dump(notes) return data
def read_one(person_id, note_id): """ This function responds to a request for /api/people/{person_id}/notes/{note_id} with one matching note for the associated person :param person_id: Id of person the note is related to :param note_id: Id of the note :return: json string of note contents """ # Query the database for the note note = ( Note.query.join(Person, Person.person_id == Note.person_id) .filter(Person.person_id == person_id) .filter(Note.note_id == note_id) .one_or_none() ) # Was a note found? if note is not None: note_schema = NoteSchema() data = note_schema.dump(note).data return data # Otherwise, nope, didn't find that note else: abort(404, f"Note not found for Id: {note_id}")
def create(note): content = note.get('content', None) title = note.get('title', None) remind = note.get('remind', None) if not title: title = get_first_n_symbols_content(SYMBOLS_COUNT, content) note['title'] = title existing_note = Note.query.filter(Note.title == title)\ .filter(Note.content == content)\ .filter(Note.remind == remind)\ .one_or_none() if existing_note is None: schema = NoteSchema() print(note) new_note = schema.load(note, session=db.session) db.session.add(new_note) db.session.commit() return schema.dump(new_note), 201 else: abort(406, 'Note already exists')
def update(note_id, note): update_note = Note.query.filter(Note.note_id == note_id).one_or_none() content = note.get('content', None) title = note.get('title', None) remind = note.get('remind', None) if not title: title = get_first_n_symbols_content(SYMBOLS_COUNT, content) note['title'] = title existing_note = Note.query.filter(Note.title == title)\ .filter(Note.content == content)\ .filter(Note.remind == Note.remind)\ .one_or_none() if update_note is None: abort(404, 'Note with not found') elif (existing_note is not None and existing_note.note_id != note_id): abort(406, 'Note already exists') else: schema = NoteSchema() update = schema.load(note, session=db.session) update.note_id = update_note.note_id db.session.merge(update) db.session.commit() data = schema.dump(update_note) return data, 200
def update(person_id, note_id, note): """ This function updates an existing note related to the passed in person id. :param person_id: Id of the person the note is related to :param note_id: Id of the note to update :param content: The JSON containing the note data :return: 200 on success """ update_note = (Note.query.filter(Person.person_id == person_id).filter( Note.note_id == note_id).one_or_none()) if update_note is not None: schema = NoteSchema() update = schema.load(note, session=db.session) update.person_id = update_note.person_id update.note_id = update_note.note_id db.session.merge(update) db.session.commit() data = schema.dump(update_note) return data, 200 else: abort(404, f"Note not found for Id: {note_id}")
def read_one(note_id): note = Note.query.filter(Note.note_id == note_id).one_or_none() if note is not None: note_schema = NoteSchema() return note_schema.dump(note) else: abort(400, f'Note with id - {note_id} not found')
def read_one(person_id, note_id): ''' This function responds to a request for /api/people/{person_id}/notes/{note_id} with one matching note for the associated person Parameters ---------- person_id : int Id of person the note is related to note_id : int Id of the note Returns ------- str JSON string of note contents ''' # Query the database for the note note = (Note.query.join(Person, Person.person_id == Note.person_id).filter( Person.person_id == person_id).filter( Note.note_id == note_id).one_or_none()) # Was a note found? if note is not None: note_schema = NoteSchema() data = note_schema.dump(note) return data # Otherwise, didn't find the note else: abort(404, f'Note with Id {note_id} not found.')
def create(person_id, note): ''' This function creates a new note related to the passed person_id Parameters ---------- person_id : int Id of the person the note is related to note : Note The JSON containing the note data Returns ------- 201 On successful creation ''' # Get the parent person person = Person.query.filter(Person.person_id == person_id).one_or_none() # Was a person found? if person is None: abort(404, f'Person with Id {person_id} not found') # Create a note schema instance schema = NoteSchema() new_note = schema.load(note, session=db.session).data # Add the note to the person and database person.notes.append(new_note) db.session.commit() # Serialize and return the newly created note in the response data = schema.dump(new_note) return data, 201
def update(person_id, note_id, note): """ This function updates an existing note related to the passed in person id. :param person_id: Id of the person the note is related to :param note_id: Id of the note to update :param content: The JSON containing the note data :return: 200 on success """ update_note = (Note.query.filter(Person.person_id == person_id).filter( Note.note_id == note_id).one_or_none()) # Did we find an existing note? if update_note is not None: # Set the id's to the note we want to update update_note.content = note.get("content") db.session.commit() # return updated note in the response schema = NoteSchema() data = schema.dump(update_note) return data, 200 # Otherwise, nope, didn't find that note else: abort(404, f"Note not found for Id: {note_id}")
def update(person_id, note_id, note): """ update an existing note related to the passed in person_id :param person_id: :param note_id: :param note: :return: 200 on success """ update_note = (Note.query.filter(Person.person_id == person_id).filter( Note.note_id == note_id).one_or_none()) # find a note? if update_note is not None: # turn note into a db object schema = NoteSchema() update = schema.load(note, session=db.session) # set the IDs to the note we want to update update.person_id = update_note.person_id update.note_id = update_note.note_id # merge the new object, commit to db db.session.merge(update) db.session.commit() # return updated note data = schema.dump(update_note) return data, 200 else: abort(404, f'Note not found for ID: {note_id}')
def create(person_id, note): """ creates a new note related to the passed person_id :param person_id: :param note: :return: 201 on success """ # get the person person = Person.query.filter(Person.person_id == person_id).one_or_none() # person? if person is None: abort(404, f'Person not found for: {person_id}') schema = NoteSchema() new_note = schema.load(note, session=db.session) # add note to the db person.notes.append(new_note) db.session.commit() # serialize and return the newly created note data = schema.dump(new_note) return data, 201
def read_one(noteid): note = Note.query.filter(Note.noteid == noteid).one_or_none() if note is not None: note_schema = NoteSchema() data = note_schema.dump(note) return data, 200 else: abort(404, "Note Record not found for {}".format(noteid))
def read_all(): """ This function responds to a request for /api/people/notes with the complete list of notes, sorted by note timestamp :return: json list of all notes for all people """ notes = Note.query.order_by(db.desc(Note.timestamp)).all() note_schema = NoteSchema(many=True, exclude=["person.notes"]) data = note_schema.dump(notes) return data
def read_all(query=None): notes = None if query: notes = Note.query.filter((Note.title.like(f'%{query}%')) | (Note.title.like(f'%{query}%'))).order_by( Note.title) else: notes = Note.query.order_by(Note.title) note_schema = NoteSchema(many=True) return note_schema.dump(notes.all())
def read_all(): """ This function responds to a request for /api/people/notes with the complete list of notes, sorted by note timestamp :return: json list of all notes for all people """ # Query the database for all the notes notes = Note.query.order_by(db.desc(Note.timestamp)).all() # Serialize the list of notes from our data note_schema = NoteSchema(many=True) data = note_schema.dump(notes) return data
def read_all(): """ refers to /api/people/notes :return: json list of all notes for all people """ # query for the notes notes = Note.query.order_by(db.desc(Note.timestamp)).all() # serialize the list note_schema = NoteSchema(many=True, exclude=["person.notes"]) data = note_schema.dump(notes) return data
def read_one(person_id, note_id): note = (Note.query.join(Person, Person.person_id == Note.person_id).filter( Person.person_id == person_id).filter( Note.note_id == note_id).one_or_none()) if note is not None: note_schema = NoteSchema() data = note_schema.dump(note) return data else: abort(404, f"Note not found for Id: {note_id}")
def read_all(): note = Note.query.order_by(Note.title).all() if note is not None: # Serialize the data for the response note_schema = NoteSchema(many=True) data = note_schema.dump(note) return data else: abort( 404, "Notes not found ", )
def create(person_id, note): person = Person.query.filter(Person.person_id == person_id).one_or_none() if person is None: abort(404, f"Person not found for Id: {person_id}") schema = NoteSchema() new_note = schema.load(note, session=db.session) person.notes.append(new_note) db.session.commit() data = schema.dump(new_note) return data, 201
def read_one(note_id): note = Note.query.filter(Note.note_id == note_id).one_or_none() if note is not None: # Serialize the data for the response note_schema = NoteSchema() data = note_schema.dump(note) return data else: abort( 404, "Note not found for Id: {note_id}".format(note_id=note_id), )
def update(note_id, note): update_note = Note.query.filter(Note.note_id == note_id).one_or_none() # Try to find an existing note with the same title as the update title = note.get("title") content = note.get("content") existing_note = (Note.query.filter(Note.title == title).filter( Note.content == content).one_or_none()) if update_note is None: abort( 404, "Note not found for Id: {note_id}".format(note_id=note_id), ) elif (existing_note is not None and existing_note.note_id != note_id): abort( 404, "Note {title} {content} exists already".format(title=title, content=content), ) else: # turn the passed in note into a db object schema = NoteSchema() update = schema.load(note, session=db.session) # Set the id to the note we want to update update.note_id = update_note.note_id # merge the new object into the old and commit it to the db db.session.merge(update) db.session.commit() note_add = Note.query.filter(Note.note_id == note_id).one_or_none() print(note_add) arch_n = Arch_Note(note_id=note_add.note_id, title=note_add.title, content=note_add.content, comment='updated') db.session.commit() data = schema.dump(update_note) return data, 200
def create(note_name): existing_note = Note.query.filter( Note.note_name == note_name).one_or_none() if existing_note is None: new_note = Note(note_name) db.session.add(new_note) db.session.commit() schema = NoteSchema() data = schema.dump(new_note) return data, 201 else: abort(409, "Note {} already exists.".format(note_name))
def update(person_id, note_id, note): update_note = (Note.query.filter(Person.person_id == person_id).filter( Note.note_id == note_id).one_or_none()) if update_note is not None: schema = NoteSchema() update = schema.load(note, session=db.session) update.person_id = update_note.person_id update.note_id = update_note.note_id db.session.merge(update) db.session.commit() data = schema.dump(update_note) return data, 200 else: abort(404, f"Note not found for Id: {note_id}")
def read_all(): ''' This function responds to a request for /api/people/notes with the complete list of notes, sorted by note timestamp Returns ------- list A JSON list of all notes for all people ''' # Query the database for all the notes notes = Note.query.order_by(db.desc(Note.timestamp)).all() # Serialize the list of notes from our data note_schema = NoteSchema(many=True, exclude=['person.notes']) data = note_schema.dump(notes) return data
def create(note): global count title = note.get("title") content = note.get("content") existing_note = (Note.query.filter(Note.title == title).filter( Note.content == content).one_or_none()) if existing_note is None: note_all = Note.query.all() schema = NoteSchema() if len(note_all) == 0: note['note_id'] = count else: print(note_all) if (count <= note_all[-1].note_id): count = note_all[-1].note_id + 1 new_note = schema.load(note, session=db.session) # Add the note to the database db.session.add(new_note) db.session.commit() note_all = Note.query.all() arch_n = Arch_Note(note_id=note_all[-1].note_id, title=note_all[-1].title, content=note_all[-1].content, comment='created') db.session.add(arch_n) db.session.commit() # Serialize and return the newly created note in the response data = schema.dump(new_note) return data, 201 else: abort( 404, "Note {title} {content} exists already".format(title=title, content=content), )
def create(person_id, note): """ This function creates a new note related to the passed in person id. :param person_id: Id of the person the note is related to :param note: The JSON containing the note data :return: 201 on success """ person = Person.query.filter(Person.person_id == person_id).one_or_none() # Was a person found? if person is None: abort(404, f"Person not found for Id: {person_id}") schema = NoteSchema() new_note = schema.load(note, session=db.session) person.notes.append(new_note) db.session.commit() data = schema.dump(new_note) return data, 201
def search(): q = request.args.get('q') download = request.args.get('download') is not None if q: notes = Note.query.filter_by(owner=current_user).filter(or_(Note.title.like(f'%{q}%'), Note.content.like(f'%{q}%'))).all() if notes and download: return Response(json.dumps(NoteSchema(many=True).dump(notes)), headers={'Content-disposition': 'attachment;filename=result.json'}) else: return redirect(url_for('index')) return render_template('index.html', notes=notes, is_search=True)
def update(person_id, note_id, note): ''' This function updates an existing note related to the passed in person_id Parameters ---------- person_id : int Id of the person the note is related to note_id : int Id of the note to update content : str The JSON containing the note data Returns ------- 200 On success ''' update_note = (Note.query.filter(Person.person_id == person_id).filter( Note.note_id == note_id).one_or_none()) # Did we find an existing note? if update_note is not None: # Turn the passed in note into a db object schema = NoteSchema() update = schema.load(note, session=db.session).data # Set the id's to the note we want to update update.person_id = update_note.person_id update.note_id = update_note.note_id # Merge the new object into the old and commit it to the db db.session.merge(update) db.session.commit() # Return updated note in the response data = schema.dump(update_note) return data, 200 # Otherwise, we didn't find the note else: abort(404, f'Note with Id {note_id} not found')