예제 #1
0
def route_two_people():
    try:
        schema = PersonSchema()
        person1 = request.args.get("person1")
        validated_person1 = schema.load(person1)
        person2 = request.args.get("person2")
        person1_details = MONGO.db.people.find_one({"name": validated_person1})
        validated_person2 = schema.load(person2)
        person2_details = MONGO.db.people.find_one({"name": validated_person2})
        person1_response = {
            "username": person1_details["name"],
            "age": person1_details["age"],
            "address": person1_details["address"],
            "phone": person1_details["phone"],
        }
        person2_response = {
            "username": person2_details["name"],
            "age": person2_details["age"],
            "address": person2_details["address"],
            "phone": person2_details["phone"],
        }
        common_friends = find_common_friends(person1_details, person2_details)
        response = {
            "person1": person1_response,
            "person2": person2_response,
            "common_friends": common_friends,
        }
        return jsonify(response)
    except:
        error_response = {
            "status": "Error",
            "message": "An error ocurred while processing this request",
        }
        return jsonify(error_response["message"])
예제 #2
0
    def post(self):
        json_data = request.get_json()
        if not json_data:
            api.abort(404, 'No input data provided')

        schema = PersonSchema()

        try:
            new_person = schema.load(json_data, session=db.session)
        except ValidationError as err:
            api.abort(400, str(err))
            raise

        first_name, last_name = new_person.fname, new_person.lname

        existing_person = Person.query \
            .filter(Person.fname == first_name) \
            .filter(Person.lname == last_name) \
            .one_or_none()

        if existing_person:
            api.abort(
                409, 'Person {} {} already exists in the database'.format(
                    first_name, last_name))

        db.session.add(new_person)
        db.session.commit()

        return schema.dump(new_person), 201
예제 #3
0
    def put(self, person_id):
        """
        Defines a unique URL to update an existing order

        :param person_id:
        :return: person object
        """
        json_data = request.get_json()
        if not json_data:
            api.abort(404, 'No input data provided')

        schema = PersonSchema()

        try:
            new_person = schema.load(json_data, session=db.session)
        except ValidationError as err:
            api.abort(400, str(err))
            raise

        existing_person = Person.query \
            .filter(Person.person_id == person_id) \
            .one_or_none()

        if not existing_person:
            self.id_not_found(person_id)

        new_person.person_id = existing_person.person_id

        db.session.merge(new_person)
        db.session.commit()

        return schema.dump(existing_person), 201
예제 #4
0
def create(person):
    """
    This function creates a new person in the users structure
    based on the passed in person data
    :param person:  person to create in users structure
    :return:        201 on success, 406 on person exists
    """
    lname = person.get("lname", None)
    fname = person.get("fname", None)
    age = person.get("age", None)
    job = person.get("job", None)

    existing_person = Person.query.filter(Person.fname == fname,
                                          Person.lname == lname,
                                          Person.age == age,
                                          Person.job == job).one_or_none()

    if not existing_person:
        schema = PersonSchema()
        new_person = schema.load(person, session=db.session).data

        db.session.add(new_person)
        db.session.commit()

        return schema.dump(new_person).data, 201

    else:
        abort(
            406, "Person {lname} {fname} exists already".format(lname=lname,
                                                                fname=fname))
예제 #5
0
def create(person):
    """
    Function responding to requests for creating a new person

    :param person:  person to create in people's structure
    :return: 201 on success, 406 if person already exists
    """
    lname = person.get("lname", None)
    fname = person.get("fname", None)

    existing_person = (Person.query.filter(Person.fname == fname).filter(
        Person.lname == lname).one_or_none())

    if existing_person is None:
        # Create a person instance using schema and passed in person
        schema = PersonSchema()
        new_person = schema.load(person, session=db.session).data

        # Add person to the session and commit to database
        db.session.add(new_person)
        db.session.commit()

        # Serialize and return newly created person in response
        data = schema.dump(new_person).data

        return data, 201
    else:
        abort(
            409,
            "Person {fname} {lname} exists already".format(fname=fname,
                                                           lname=lname),
        )
예제 #6
0
def update(person_id, person):
    """
    This function updates an existing person in the people structure
    :param person_id:   Id of the person to update in the people structure
    :param person:      person to update
    :return:            updated person structure
    """
    # Get the person requested from the db into session
    update_person = Person.query.filter(
        Person.person_id == person_id).one_or_none()

    # Did we find an existing person?
    if update_person is not None:

        # turn the passed in person into a db object
        schema = PersonSchema()
        update = schema.load(person, session=db.session).data

        # Set the id to the person we want to update
        update.person_id = update_person.person_id

        # merge the new object into the old and commit it to the db
        db.session.merge(update)
        db.session.commit()

        # return updated person in the response
        data = schema.dump(update_person).data

        return data, 200

    # Otherwise, nope, didn't find that person
    else:
        abort(404, f"Person not found for Id: {person_id}")
예제 #7
0
def route_person(person_name):
    """ Return details of a single person """
    try:
        schema = PersonSchema()
        validated_person_name = schema.load(person_name)
        user_detail = {}
        person_details = MONGO.db.people.find_one(
            {"name": validated_person_name})
        user_detail.update({"username": person_details["name"]})
        user_detail.update({"age": person_details["age"]})
        favFood = person_details["favouriteFood"]
        favFruit = []
        favVeggie = []
        for each_fooditem in favFood:
            if each_fooditem in fruits:
                favFruit.append(each_fooditem)
            elif each_fooditem in vegetables:
                favVeggie.append(each_fooditem)
        user_detail.update({"fruits": favFruit})
        user_detail.update({"vegetables": favVeggie})
        return jsonify(user_detail)
    except:
        error_response = {
            "status": "Error",
            "message": "An error ocurred while processing this request",
        }
        return jsonify(error_response["message"])
예제 #8
0
def create(person):
    """
    This function creates a new person in the people structure
    based on the passed in person data
    :param person:  person to create in people structure
    :return:        201 on success, 406 on person exists
    """
    lname = person.get("lname", None)
    fname = person.get("fname", None)

    # Does the person already exist?
    existing_person = (
        Person.query.filter(Person.fname == fname)
        .filter(Person.lname == lname)
        .one_or_none()
    )

    # Can we insert this person
    if existing_person is None:

        # Create a person instance using the schema and the passed in person
        schema = PersonSchema()
        new_person = schema.load(person, session=db.session)

        # Add the person to the database
        db.session.add(new_person)
        db.session.commit()

        # Serialize and return the newly created person in the response
        return schema.dump(new_person), 201
예제 #9
0
def update(person_id, person):
    """
    This function updates an existing person in the persons database
    :param person_id:   ID of person to update in the persons database
    :param person:  person data to update
    :return:        updated person structure
    """
    # Get the person from the db
    update_person = Person.query.filter(
        Person.id == person_id
    ).one_or_none()

    # Abort if we can't find the person
    if update_person is None:
        abort(
            404,
            f"Person not found for Id: {person_id}",
        )

    # turn the passed in person into a db object
    person_schema = PersonSchema()
    updater = person_schema.load(person, session=db.session)

    # Set the id to the person we want to update
    updater.id = update_person.id

    # merge the new object into the old and commit it to the db
    db.session.merge(updater)
    db.session.commit()

    # return updated person in the response
    data = person_schema.dump(update_person)

    return data, 200
예제 #10
0
def update(person_id, person):
    # fname = person.get("fname", None)

    # if lname in PEOPLE and lname is not None:
    #     PEOPLE[lname]["fname"] = person.get("fname", None)
    #     PEOPLE[lname]["timestamp"] = get_timestamp()
    #     return make_response(
    #         "Successfully updated person {lname}".format(lname=lname)
    #     )
    # else:
    #     abort(
    #         404, "Person with {lname} does not exist".format(lname=lname)
    #     )

    fname = person.get("fname", None)
    lname = person.get("lname", None)
    # updated_person = Person.query.filter(Person.person_id == person_id).update(
    #     {Person.fname: fname, Person.lname: lname}
    # )

    schema = PersonSchema()
    update = schema.load(person)
    update.person_id = person_id

    db.session.merge(update)
    db.session.commit()

    return schema.dump(update)
예제 #11
0
def update(person_id, person):
    """
    Function for updating existing person in people's structure

    :param person_id: Id of the person to update in people structure
    :param person: person to update
    :return: updated person structure
    """
    update_person = Person.query.filter(
        Person.person_id == person_id).one_or_none()

    # If person exists
    if update_person is not None:
        # create person from data passed
        schema = PersonSchema()
        update = schema.load(person, session=db.session).data

        # Set the id to the new person
        update.id = update_person.person_id

        # merge the new object in old and commit it to db
        db.session.merge(update)
        db.session.commit()

        # return updated person
        data = schema.dump(update_person).data

        return data, 200

    # Person does not exist
    else:
        abort(
            404,
            "Person not found for Id: {person_id}".format(person_id=person_id),
        )
예제 #12
0
def create(person):
    """
    This function creates a new person in the people structure
    based on the passed-in person data

    :param person:  person to create in people structure
    :return:        201 on success, 406 on person exists
    """
    lname = person.get("lname")
    fname = person.get("fname")

    # Does the person exist already?
    existing_person = Person.query.filter(Person.fname == fname).filter(
        Person.lname == lname).one_or_none()

    if existing_person is None:

        # Create a person instance using the schema and the passed in person
        schema = PersonSchema()
        new_person = schema.load(person, session=db.session)

        # Add the person to the database
        db.session.add(new_person)
        db.session.commit()

        # Serialize and return the newly created person in the response
        data = schema.dump(new_person)

        return data, 201

    else:
        abort(
            409, 'Person {fname} {lname} exists already'.format(fname=fname,
                                                                lname=lname))
예제 #13
0
def update(person_id, person):
    """
    This function updates an existing person in the people structure
    Throws an error if a person with the name we want to update to
    already exists in the database.
    :param person_id:   Id of the person to update in the people structure
    :param person:      person to update
    :return:            updated person structure
    """
    # Get the person requested from the db into session
    update_person = Person.query.filter(
        Person.person_id == person_id
    ).one_or_none()

    # Try to find an existing person with the same name as the update
    fname = person.get("fname")
    lname = person.get("lname")

    existing_person = (
        Person.query.filter(Person.fname == fname)
            .filter(Person.lname == lname)
            .one_or_none()
    )

    # Are we trying to find a person that does not exist?
    if update_person is None:
        abort(
            404,
            "Person not found for Id: {person_id}".format(person_id=person_id),
        )

    # Would our update create a duplicate of another person already existing?
    elif (
            existing_person is not None and existing_person.person_id != person_id
    ):
        abort(
            409,
            "Person {fname} {lname} exists already".format(
                fname=fname, lname=lname
            ),
        )

    # Otherwise go ahead and update!
    else:

        # turn the passed in person into a db object
        schema = PersonSchema()
        update = schema.load(person, session=db.session)

        # Set the id to the person we want to update
        update.person_id = update_person.person_id

        # merge the new object into the old and commit it to the db
        db.session.merge(update)
        db.session.commit()

        # return updated person in the response
        data = schema.dump(update_person)

        return data, 200
예제 #14
0
def create(person):
    """
    This function creates a new person in the people structure
    based on the passed in person data

    :param person:  person to create in people structure
    :return:        201 on success, 406 on person exists
    """
    lname = person.get('lname')
    fname = person.get('fname')

    existing_person = Person.query \
        .filter(Person.fname == fname) \
        .filter(Person.lname == lname) \
        .one_or_none()

    if existing_person is None:

        schema = PersonSchema()
        new_person = schema.load(person, session=db.session).data

        db.session.add(new_person)
        db.session.commit()

        return schema.dump(new_person).data, 201

    else:
        abort(409, f'Person {fname} {lname} exists already')
예제 #15
0
def update(person_id, person):
    """
    This function updates an existing person in the people structure

    :param person_id:   Id of the person to update in the people structure
    :param person:  person to update
    :return:        updated person structure
    """
    update_person = Person.query \
        .filter(Person.person_id == person_id) \
        .one_or_none()

    if update_person is not None:

        schema = PersonSchema()
        update = schema.load(person, session=db.session).data

        update.id = update_person.id

        db.session.merge(update)
        db.session.commit()

        data = schema.dump(update_person).data

        return data, 200

    else:
        abort(
            404,
            'Person not found for Id: {person_id}'.format(person_id=person_id))
예제 #16
0
def update(person_id, person):
    """
    Updates person with person_id with passed in person 
    """
    # Get the person requested for update from the db into session
    update_person = Person.query.filter(
        Person.person_id == person_id).one_or_none()

    # Exit if person to update is not present in db
    if update_person is None:
        abort(404, f"Person with id: {person_id} not found")

    # Try to find after updating existing record, would we get a person that
    # already exists in the db
    fname = person.get("fname")
    lname = person.get("lname")

    existing_person = (Person.query.filter(Person.fname == fname).filter(
        Person.lname == lname).one_or_none())

    if existing_person is not None and existing_person.person_id != person_id:
        abort(409, f"Person with {fname} {lname} already exists")

    # Go ahead and update person
    # Turn passed-in person into a db object
    schema = PersonSchema()
    update = schema.load(person, session=db.session)

    update.person_id = update_person.person_id
    # Use .merge() to update record
    db.session.merge(update)
    db.session.commit()

    data = schema.dump(update_person)
    return data, 200
예제 #17
0
def create(person):
    """
    This function creates a new person in the people structure
    based on the passed in person data
    :param person:  person to create in people structure
    :return:        201 on success, 406 on person exists
    """
    fname = person.get("fname")
    lname = person.get("lname")

    existing_person = (Person.query.filter(Person.fname == fname).filter(
        Person.lname == lname).one_or_none())

    # Can we insert this person?
    if existing_person is None:

        # Create a person instance using the schema and the passed in person
        schema = PersonSchema()
        new_person = schema.load(person, session=db.session).data

        # Add the person to the database
        db.session.add(new_person)
        db.session.commit()

        # Serialize and return the newly created person in the response
        data = schema.dump(new_person).data

        return data, 201

    # Otherwise, nope, person exists already
    else:
        abort(409, f"Person {fname} {lname} exists already")
예제 #18
0
def create(person):
    """
    This function creates a new person in the persons database
    based on the passed in person data
    :param person:  person to create in persons structure
    :return:        201 on success, 409 on person already exists
    """
    first_name = person.get('first_name')
    last_name = person.get('last_name')
    email = person.get('email')
    age = person.get('age')

    try:
        # Check if person exists already
        existing_person = Person.query \
            .filter(Person.first_name == first_name) \
            .filter(Person.last_name == last_name) \
            .filter(Person.email == email) \
            .filter(Person.age == age) \
            .one_or_none()

        if existing_person is None:
            person_schema = PersonSchema()
            new_person = person_schema.load(person, session=db.session)
            db.session.add(new_person)
            db.session.commit()

            data = person_schema.dump(new_person)
            return data, 201

        abort(409, f"Person with name {first_name} {last_name} already exists")
    except sqlalchemy.exc.SQLAlchemyError as e:
        logging.error(f'SQLAlchemyError: {e}')
예제 #19
0
def create(person):
    
    fname = person.get("fname")
    lname = person.get("lname")

    existing_person = (
        Person.query.filter(Person.fname == fname)
        .filter(Person.lname == lname)
        .one_or_none()
    )

    if existing_person is None:

        # Create a person instance using the schema and the passed in person
        schema = PersonSchema()
        new_person = schema.load(person, session=db.session)

        # Add the person to the database
        db.session.add(new_person)
        db.session.commit()

        # Serialize and return the newly created person in the response
        data = schema.dump(new_person)

        return data, 201

    # Otherwise, person exists already
    else:
        abort(
            409,
            "Person {fname} {lname} exists already".format(
                fname=fname, lname=lname
            ),
        )
예제 #20
0
def create(person):
    """
    This function creates a new person in the people list based on the passed in person data

    :param person:  person to create in the people list
    :return:    201 on success, 406 on person exist
    """
    lname = person.get("lname", None)
    fname = person.get("fname", None)

    # Existing person
    existing_person = Person.query.filter(Person.fname==fname).filter(Person.lname==lname).one_or_none()

    # does the person exist already
    if existing_person is None:

        # Create the person instance using the schema and the person data that is passed in
        person_schema = PersonSchema()
        new_person = person_schema.load(person, session=db.session)

        # Add the person to the database
        db.session.add(new_person)
        db.session.commit()

        # Serialize and return the newly created person as response
        data = person_schema.dump(new_person)
        return data, 201
    else:
        abort(
            406, f"Person {fname} {lname} already exists"
        )
예제 #21
0
def create(person):
    fname = person.get("fname", None)
    lname = person.get("lname", None)

    schema = PersonSchema()
    new_person = schema.load(person)

    db.session.add(new_person)
    db.session.commit()

    return schema.dump(new_person)
예제 #22
0
def update(person_id, person):
    """
    This function updates an existing person in the people structure

    :param person_id:   Id of the person to update in the people structure
    :param person:      person to update
    :return:            updated person structure
    """
    # Get the person requested from the db into session
    person_to_update = Person.query.filter(
        Person.person_id == person_id).one_or_none()

    # Did we find a person?
    if person_to_update is not None:

        # turn the passed in person into a db object
        schema = PersonSchema()
        person_new_data = schema.load(person, session=db.session).data

        print('============update============')
        print(person_new_data)
        #print(person_new_data.id)
        print(person_new_data.person_id)
        print(person_new_data.fname)
        print(person_new_data.lname)
        print('============person_new_data============')

        print('============person_to_update============')
        print(person_to_update)
        print(person_to_update.person_id)
        print(person_to_update.fname)
        print(person_to_update.lname)
        print('============person_to_update============')

        # Set the id to the person we want to update
        #person_new_data.id = person_to_update.person_id # this only works using the web form (home.html) and not when calling the API directly via curl/postman
        person_new_data.person_id = person_to_update.person_id  # this works in bith: using the web form (home.html), and not when calling the API directly via curl/postman

        # merge the new object into the old and commit it to the db
        db.session.merge(person_new_data)
        db.session.commit()

        # return updated person in the response
        data = schema.dump(person_to_update).data

        return data, 200

    # Otherwise, nope, didn't find that person
    else:
        abort(
            404,
            "Person not found for Id: {person_id}".format(person_id=person_id),
        )
예제 #23
0
def update(person_id, person):
    """
    This function updates an existing person in the people list

    :param person_id: id of the person to update in the people list
    :param person:  person to update
    :return: updated person structure
    """
    # Get the person requested
    update_person = Person.query.filter(Person.person_id==person_id).one_or_none()

    # Try to find an existing person with the same name as update
    lname = person.get("lname", None)
    fname = person.get("fname", None)

    existing_person = Person.query.filter(Person.fname == fname).filter(Person.lname == lname).one_or_none()

    # Are we trying to update a person that does not exist?
    if update_person is None:
        abort(
            404, "Person with person id {person_id} not found".format(person_id=person_id)
        )

    # Will our update create a duplicate person?
    elif existing_person is not None and existing_person.person_id != person_id:
        abort(
            406, "Person {fname} {lname} already exists".format(fname=fname, lname=lname)
        )

    # Otherwise go ahead and update
    else:
        # Convert the passed in person into a db object
        person_schema = PersonSchema()
        update = person_schema.load(person, session=db.session)

        # set the id to the person we want to update
        update.person_id = update_person.person_id

        # merge the new object into the old and commit it
        db.session.merge(update)
        db.session.commit()

        # retrun the updated person in response
        data = person_schema.dump(update_person)
        return data, 200
예제 #24
0
def update(person_id, person):
    # Get the person requested from the db into session
    update_person = Person.query.filter(
        Person.person_id == person_id
    ).one_or_none()

    # Try to find an existing person with the same name as the update
    fname = person.get("fname")
    lname = person.get("lname")

    existing_person = (
        Person.query.filter(Person.fname == fname)
        .filter(Person.lname == lname)
        .one_or_none()
    )

    if update_person is None:
        abort(
            404,
            "Person not found for Id: {person_id}".format(person_id=person_id),
        )

    elif (
        existing_person is not None and existing_person.person_id != person_id
    ):
        abort(
            409,
            "Person {fname} {lname} exists already".format(
                fname=fname, lname=lname
            ),
        )

    else:
        schema = PersonSchema()
        update = schema.load(person, session=db.session)

        update.person_id = update_person.person_id


        db.session.merge(update)
        db.session.commit()

        data = schema.dump(update_person)

        return data, 200
예제 #25
0
def update(person_id, person):
    """
    This function updates an existing person in the people structure

    :param person_id:   ID of person to update in the people structure
    :param person:  person to update
    :return:        updated person structure
    """
    # Get the person requested from the db into session
    update_person = Person.query.filter(
        Person.person_id == person_id).one_or_none()

    # try to find an existing person with the same name as the update
    fname = person.get("fname")
    lname = person.get("lname")

    existing_person = (Person.query.filter(Person.fname == fname).filter(
        Person.lname == lname).one_or_none())

    # are we trying to find a person that does not exist?
    if update_person is None:
        abort(404, f"Person for ID {person_id} not found.")

    # would our update create a duplicate of another person already existing?
    elif (existing_person is not None
          and existing_person.person_id != person_id):
        abort(409, f"Person {fname} {lname} already exists.")

    # no? then update
    else:
        # turn the person into a db object
        schema = PersonSchema()
        update = schema.load(person, session=db.session)

        # set the ID of the person we want to update
        update.person_id = update_person.person_id

        # merge the new object into the old and commit it to the db
        db.session.merge(update)
        db.session.commit()

        # return the updated person in the response
        data = schema.dump(update_person)

        return data, 200
예제 #26
0
def create(person):
    """
    This function creates a new person in the people structure
    based on the passed in person data

    :param person:  person to create in people structure
    :return:        201 on success, 406 on person exists
    """
    Survived = person.get("Survived")
    Pclass = person.get("Pclass")
    Name = person.get("Name")
    Sex = person.get("Sex")
    Age = person.get("Age")
    SiblingsSpousesAboard = person.get("SiblingsSpousesAboard")
    ParentsChildrenAboard = person.get("ParentsChildrenAboard")
    Fare = person.get("Fare")

    existing_person = (Person.query.filter(Person.Survived == Survived).filter(
        Person.Name == Name).one_or_none())

    # Can we insert this person?
    if existing_person is None:

        # Create a person instance using the schema and the passed in person
        schema = PersonSchema()
        new_person = schema.load(person, session=db.session).data

        # Add the person to the database
        db.session.add(new_person)
        db.session.commit()

        # Serialize and return the newly created person in the response
        data = schema.dump(new_person).data

        return data, 201

    # Otherwise, nope, person exists already
    else:
        abort(
            409,
            "Person {Name} {Survived} exists already".format(
                Name=Name, Survived=Survived),
        )
예제 #27
0
def create(person):
    """
    Creates a person in the people structure based on passed in person data
    """
    fname = person.get("fname")
    lname = person.get("lname")

    existing_person = (Person.query.filter(Person.fname == fname).filter(
        Person.lname == lname).one_or_none())

    if existing_person is None:
        # Create a person instance using schema and passed-in person
        schema = PersonSchema()
        new_person = schema.load(person, session=db.session)

        # Add person to db
        db.session.add(new_person)
        db.session.commit()
    else:
        abort(409, f"Person with {fname} {lname} already exists")
예제 #28
0
def create(person):
    """
    This function creates a new person in the people structure
    based on the passed in person data

    :param person:  person to create in people structure
    :return:        201 on success, 406 on person exists
    """
    fio = person.get("fio")
    birthday = person.get("birthday")
    office = person.get("office")  #______________#______________

    existing_person = (
        Person.query.filter(
            Person.fio == fio).filter(Person.birthday == birthday).filter(
                Person.office == office)  #______________#______________
        .one_or_none())

    # Can we insert this person?
    if existing_person is None:

        # Create a person instance using the schema and the passed in person
        schema = PersonSchema()
        new_person = schema.load(person, session=db.session).data

        # Add the person to the database
        db.session.add(new_person)
        db.session.commit()

        # Serialize and return the newly created person in the response
        data = schema.dump(new_person).data

        return data, 201

    # Otherwise, nope, person exists already
    else:
        abort(
            409,
            "Person {fio} {birthday} {office} exists already".format(
                fio=fio, birthday=birthday, office=office),
        )
예제 #29
0
def update(person_id, person):
    '''
    This function updates an existing person in the people structure

    Parameters
    ----------
    person_id: int
        Id of the person to update in the people structure
    person: Person
        Person to update

    Returns
    -------
    Person
        Updated person structure
    '''
    # Get the person requested from the db into session
    update_person = Person.query.filter(
        Person.person_id == person_id).one_or_none()

    # Did we find an existing person?
    if update_person is not None:
        # Turn the passed in person into a db object
        schema = PersonSchema()
        update = schema.load(person, session=db.session).data

        # Set the id to the person we want to update
        update.person_id = update_person.person_id

        # Merge the new object into the old and commit it to the db
        db.session.merge(update)
        db.session.commit()

        # Return the updated person in the response
        data = schema.dump(update_person)

        return data, 200
    # Otherwise, nope, that's an error
    else:
        abort(404, f'Person with Id {person_id} not found')
예제 #30
0
def create(person):
    """
    This function creates a new person in the people structure
    based on the passed in person data
    :param person:  person to create in people structure
    :return:        201 on success, 406 on person exists
    """
    fname = person.get("fname")
    lname = person.get("lname")

    existing_person = (Person.query.filter(Person.fname == fname).filter(
        Person.lname == lname).one_or_none())

    # Can we insert this person?
    if existing_person is None:

        print(f"create Rx = {person}")
        # Create a person instance using the schema and the passed in person
        schema = PersonSchema()
        new_person = schema.load(person)
        # Add the person to the database
        new_person_model = Person(**new_person)
        db.session.add(new_person_model)
        db.session.commit()

        # get new info from DB ( timestam and id ) to return
        db.session.refresh(new_person_model)

        # Serialize and return the newly created person in the response
        data = schema.dump(new_person_model)
        return data, 201

    # Otherwise, nope, person exists already
    else:
        abort(
            409,
            "Person {fname} {lname} exists already".format(fname=fname,
                                                           lname=lname),
        )