def get(self, name=None, id=None): if id: person = PersonModel.find_by_id(id) else: person = PersonModel.find_by_name(name) if person: return person.json() return {'message': 'Person not found'}, 404
def delete(self, name=None, id=None): if id: person = PersonModel.find_by_id(id) else: person = PersonModel.find_by_name(name) if person: person.delete_from_db() return {'message': 'Person deleted'}, 200 else: return {'message': 'Person not found'}, 400
def patch(self, id): """Update a single person using their id""" try: valid_id = is_uuid(id) if valid_id: person_data = PersonModel.find_by_id(id) if person_data: original_data = person_data.json() request_data = request.get_json() schema = PersonRequestSchema() proposed_modification = schema.load(request_data, partial=True) if proposed_modification and is_different( proposed_modification, original_data, USER_INPUT_FIELDS): updated_person = PersonModel( id=person_data.id, first_name=proposed_modification.get('first_name') or original_data.get('first_name'), middle_name=proposed_modification.get( 'middle_name') or original_data.get('middle_name'), last_name=proposed_modification.get('last_name') or original_data.get('last_name'), age=proposed_modification.get('age') or original_data.get('age'), email=proposed_modification.get('email') or original_data.get('email'), version=original_data.get('version') + 1, latest=True) updated_person.save_to_db() # The original person object is no longer the latest person_data.latest = False person_data.save_to_db() return { 'message': f"Successfully updated person with id: {id}" } else: return { 'message': f"No changes detected from user input versus database record" } else: return {'message': f"Resource not found"}, 404 else: return {'message': f"Resource not found"}, 404 except ValidationError as error: return error.messages, 400 except Exception as error: print(error) return "Internal Server Error", 500
def put(self, id): data = Person.parser.parse_args() person = PersonModel.find_by_id(id) current_user = current_identity.id if person: person.name = data['name'] person.admin = current_user else: person = PersonModel(name=data['name'], admin=current_user) person.save_to_db() return person.json(), 200
def get(self,id): if request.method == 'GET': try: user = PersonModel.find_by_id(id) if user: resp = jsonify(person=user.json()) resp.status_code = 200 else: resp = jsonify(message='No se encontró persona con ese id!') resp.status_code = 404 except ValueError as e: resp = jsonify(message='Error, {}'.format(e)) resp.status_code = 500 return resp
def delete(self, id): """Delete a single person using their id""" try: valid_id = is_uuid(id) if valid_id: person = PersonModel.find_by_id(id) if person: person.delete_from_db() return { 'message': f"Successfully deleted person with id: {person.id} and version: {person.version}" } else: return {'message': f"Resource not found"}, 404 else: return {'message': f"Resource not found"}, 404 except Exception as error: print(error) return "Internal Server Error", 500
def get(self, id): """Fetch the latest version of a single person using their id; if version is provided, fetch a single person using their id and the specified version""" try: valid_id = is_uuid(id) version = request.args.get('version', '') person_schema = PersonSchema(only=OUTPUT_FIELDS) if valid_id and version: person_data = PersonModel.find_by_id_and_version(id, version) if person_data: return person_schema.dump(person_data) else: return {'message': f"Resource not found"}, 404 elif valid_id: person_data = PersonModel.find_by_id(id) if person_data: return person_schema.dump(person_data) else: return {'message': f"Resource not found"}, 404 else: return {'message': f"Resource not found"}, 404 except Exception as error: print(error) return "Internal Server Error", 500