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) # 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 # Otherwise, nope, didn't find that person else: abort(404, f"Person not found for Id: {person_id}")
def read_all(): people = Person.query.order_by(Person.lname).all() person_schema = PersonSchema(many=True) data = person_schema.dump(people) return data
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 return schema.dump(new_person).data, 201 # Otherwise, nope, person exists already else: abort(409, f'Person {fname} {lname} exists already')
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" )
def read_all(): # return a list of Person Python objects as the variable people. people = Person.query.order_by(Person.lname).all() # if exists many Python objects that need to be serialized at the same time, you need set "many=True" person_schema = PersonSchema(many=True) # Serialize an object to native Python data types according to this Schema's fields. return person_schema.dump(people).data
def read_one(person_id): """= This function responds to a request for /api/people/{person_id} with one matching person from people :param person_id: Id of person to find :return: person matching id """ # Get the person requested person = Person.query.filter(Person.person_id == person_id).one_or_none() # Did we find a person? if person is not None: # Serialize the data for the response person_schema = PersonSchema() data = person_schema.dump(person).data return data # Otherwise, nope, didn't find that person else: abort( 404, "Person not found for Id: {person_id}".format(person_id=person_id), )
def read_one(person_id): """ This function responds to a request for /api/people/{person_id} with one matching person from people. This is where the outer join comes in handy. It’s a kind of boolean or operation. It returns person data even if there is no associated note data to combine with. This is the behavior you want for read_one(person_id) to handle the case of a newly created Person object that has no notes yet. :param person_id: Id of person to find :return: person matching id """ # Build the initial query person = ( Person.query.filter(Person.person_id == person_id) .outerjoin(Note) # 外连接 .one_or_none() ) # Did we find a person? if person is not None: # Serialize the data for the response person_schema = PersonSchema() data = person_schema.dump(person).data return data # Otherwise, nope, didn't find that person else: abort(404, f"Person not found for Id: {person_id}")
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) #removed.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) #removed.data return data, 200
def read_one(person_id): person = Person.query.filter(Person.person_id == person_id).one_or_none() if person is not None: person_schema = PersonSchema() return person_schema.dump(person) else: abort(404, "Person with last name {lname} not found".format(lname=lname))
def read_all(): """ This function responds to a request for /api/people with the complete lists of people :return: json string of list of people """ # Create the list of people from our data people = Person.query.order_by(Person.lname).all() person_schema = PersonSchema(many=True) return person_schema.dump(people).data
def read_all(): """ This function responds to a request for /api_v1/users with the complete lists of users :return: sorted list of users """ users = Person.query.order_by(Person.lname).all() person_schema = PersonSchema(many=True) return person_schema.dump(users).data
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)
def read_all(): """ Responds to a request for /api/people with the complete list of people :return: json string of list of people """ people = Person.query.order_by(Person.lname).all() # Serialize person_schema = PersonSchema(many=True) return person_schema.dump(people).data
def healthz(): """ This function responds to a request healthz :return: json message ok """ # Create the list of people from our data person = Person.query.filter(Person.person_id).all() person_schema = PersonSchema(many=True) data = person_schema.dump(person) db.session.commit() return data
def get(): """ This function responds to a request for /api/people with complete lists of people :return: sorted list of people """ # Create the list of people from our data people = Person.query.order_by(Person.lname).all() # Serialize the data for the response person_schema = PersonSchema(many=True) return person_schema.dump(people)
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), )
def read_all(): """ :return: list of person in people db """ # Create a list of people from our data # Uses SQLAlchemy model to write a SQL like query people = Person.query.order_by(Person.lname).all() # Serialize data for the response # Note: many=True tells PersonSchema to expect an iterable to serialize person_schema = PersonSchema(many=True) return person_schema.dump(people)
def fetch_all(): """ This function responds to a GET request for /api/persons with the complete lists of persons :return: sorted list of persons """ try: persons = Person.query.order_by(Person.last_name).all() person_schema = PersonSchema(many=True) return person_schema.dump(persons) except sqlalchemy.exc.SQLAlchemyError as e: logging.error(f'SQLAlchemyError: {e}')
def read_one(person_id): person = (Person.query.filter( Person.person_id == person_id).outerjoin(Note).one_or_none()) if person is not None: person_schema = PersonSchema() data = person_schema.dump(person) return data else: abort(404, f"Person not found for Id: {person_id}")
def read_all(): """ This function responds to a request for /api/people with the complete lists of people :return: json string of list of people """ # Create the list of people from our data people = db.session.query(Person).order_by(Person.lname).all() # Serialize the data for the response person_schema = PersonSchema(many=True) data = person_schema.dump(people) return data
def read_all(): """ Function for responding to request for /api/people with complete list of people :return: json string of list of people """ # Create list of people from data people = Person.query.order_by(Person.lname).all() # serialize data for making response person_schema = PersonSchema(many=True) data = person_schema.dump(people).data return data
def read_all(): """ This function will respond to a request for /api/people with the complete list of people :return: sorted list of people """ # create the list of people from our data people = Person.query.order_by(Person.lname).all() # serialize the data from the response person_schema = PersonSchema(many=True) data = person_schema.dump(people) return data
def read_all(): """ This function responds to a request for /api/people with the complete lists of people :return: json string of list of people """ # Create the list of people from our data people = Person.query.order_by(Person.fio).all() # сортировка.отображение # Serialize the data for the response person_schema = PersonSchema(many=True) data = person_schema.dump(people).data return data
def read_one(person_id): """ Responds to a request for /api/people/{person_id} with one matching person from people :param person_id: Id of person to find :return: person matching id """ person = Person.query.filter(Person.person_id == person_id).one_or_none() if person is not None: person_schema = PersonSchema() return person_schema.dump(person).data else: abort(404, f"Person not found for id: {person_id}")
def read_one(person_id): # Get the person requested person = Person.query.filter(Person.person_id == person_id).one_or_none() if person is not None: person_schema = PersonSchema() data = person_schema.dump(person) return data else: abort( 404, "Person not found for Id: {person_id}".format(person_id=person_id), )
def read_one(person_id): """ Read a person by person_id """ # Find person with required person_id person = Person.query.filter(Person.person_id == person_id).one_or_none() if person: # Note: We don't pass many=True because a single object is passed in to # serialize person_schema = PersonSchema() return person_schema.dump(person) else: abort(404, f"Person with person_id {person_id} not found") return {}
def read_one(person_id): person = Person.query \ .filter(Person.person_id == person_id) \ .one_or_none() if person is not None: person_schema = PersonSchema() return person_schema.dump(person).data else: abort( 404, 'Person not found for Id: {person_id}'.format(person_id=person_id))
def read_one(person_id): """ This function responds to a request for /api/people/{person_id} with one matching person from people :param person_id: ID of person to find :return: person matching ID """ # Get the person requested person = Person.query \ .filter(Person.person_id == person_id) \ .one_or_none() # Did we find a person? if person is not None: # Serialize the data for the response person_schema = PersonSchema() return person_schema.dump(person).data # Otherwise, nope, didn't find that person else: abort( 404, 'Person not found for Id: {person_id}'.format(person_id=person_id)) 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 return schema.dump(new_person).data, 201 # Otherwise, nope, person exists already else: abort(409, f'Person {fname} {lname} exists already')
def read_one(person_id): """ This function responds to a request for /api_v1/users/{lname} with one matching person from users :param person_id: last name of person to find :return: person matching last name """ person = Person.query.filter(Person.id == person_id).one_or_none() if person: person_schema = PersonSchema() return person_schema.dump(person).data else: abort(404, "Person with last name {0} not found".format(person_id)) return person
def get(self, person_id): """ This function responds to a request for /api/people/{person_id} with one matching person from people :param person_id: id of person to find :return: person matching last name """ # Does the person exist in people? person = Person.query.filter( Person.person_id == person_id).one_or_none() if not person: api.abort(404, 'Person not found for ID: {}'.format(person_id)) person_schema = PersonSchema() return person_schema.dump(person)