コード例 #1
0
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
コード例 #2
0
ファイル: notes.py プロジェクト: free4m/realpython
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}')
コード例 #3
0
ファイル: notes.py プロジェクト: SouthernYoda/People_API
def update(person_id, note_id, content):
    """
    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:

        # turn the passed in note into a db object
        schema = NoteSchema()
        update = schema.load({"content": content}, session=db.session)

        # 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, nope, didn't find that note
    else:
        abort(404, f"Note not found for Id: {note_id}")
コード例 #4
0
ファイル: notes.py プロジェクト: rtelles64/people_api
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
コード例 #5
0
ファイル: notes.py プロジェクト: free4m/realpython
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
コード例 #6
0
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
コード例 #7
0
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')
コード例 #8
0
ファイル: notes.py プロジェクト: Sneh1999/Peoples-database
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}")
コード例 #9
0
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
コード例 #10
0
ファイル: note.py プロジェクト: Kadet1990/REST_API_EXAMPLE
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
コード例 #11
0
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}")
コード例 #12
0
ファイル: notes.py プロジェクト: Sneh1999/Peoples-database
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
コード例 #13
0
ファイル: note.py プロジェクト: Kadet1990/REST_API_EXAMPLE
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),
        )
コード例 #14
0
ファイル: notes.py プロジェクト: rtelles64/people_api
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')