def add_pet(member_id): if request.method == 'POST': pet_type= request.form['pet_type'] pet_name= request.form['pet_name'] new_pet = PetModel(member_id=member_id, pet_name=pet_name, pet_type=pet_type) new_pet.create_record() flash("Pet added successifully","success") return redirect(url_for('members'))
def get(cls, id: int): user_id = get_jwt_identity() pet = PetModel.find_one_by_user_id(id, user_id) if pet: return pet_schema.dump(pet), 200 return {"message": PET_NOT_FOUND}, 404
def put(self, _id, owner): parser = reqparse.RequestParser() parser.add_argument('name', type=str, required=False) parser.add_argument('sex', type=str, required=False) parser.add_argument('type', type=str, required=False) parser.add_argument('breed', type=str, required=False) parser.add_argument('owner_id', type=int, required=False) parser.add_argument('dob', type=lambda x: dt.datetime.strptime(x, "%b %d, %Y").date(), required=False) pet = PetModel.find_by_id(_id, owner) if pet: payload = parser.parse_args() if payload.get('name') is not None: pet.name = payload['name'] if payload.get('sex') is not None: pet.sex = payload['sex'] if payload.get('type') is not None: pet.type = payload['type'] if payload.get('breed') is not None: pet.breed = payload['breed'] if payload.get('owner_id') is not None: pet.owner_id = payload['owner_id'] if payload.get('dob') is not None: pet.dob = payload['dob'] pet.save() return { 'message' : f'Pet {_id} changed.', 'record' : pet.json() } else: return { 'message' : f'Pet {_id} not found - owner : {owner}'}, 404
def put(self, id): item = request.get_json() if request.get_json() else request.form try: if item: model = PetModel.get_by_id(id) if 'name' in item: model.name = item['name'] if 'species' in item: model.species = item['species'] if 'breed' in item: model.breed = item['breed'] if 'size' in item: model.size = item['size'] if 'weight' in item: model.weight = item['weight'] if 'status' in item: model.status = item['status'] if 'status' in item: model.status = item['status'] model.save() return 'edited', 204 else: return 'unedited, invalid payload', 400 except Exception as e: return f"{e}", 500
def get(self, _id, owner): pet = PetModel.find_by_id(_id, owner) if pet: return pet.json() else: return { 'message' : f'Pet {_id} not found - owner : {owner}'}, 404
def delete(self, _id, owner): pet = PetModel.find_by_id(_id, owner) if pet: pet.delete() return { 'message' : f'Pet {_id} deleted - owner : {owner}'} else: return { 'message' : f'Pet {_id} not found - owner : {owner}'}, 404
def delete(self, id): try: pet = PetModel.get_by_id(id) proposal = ProposalModel.get_by_id(pet.proposal_id) proposal.delete() pet.delete() return 'No Content', 204 except Exception as e: return f"{e}", 500
def post(self): parser = reqparse.RequestParser() parser.add_argument('name', type=str, required=True, help="Field name cannot be empty") parser.add_argument('sex', type=str, required=True, help="Field sex cannot be empty") parser.add_argument('type', type=str, required=True, help="Field type cannot be empty") parser.add_argument('breed', type=str, required=True, help="Field breed cannot be empty") parser.add_argument('owner_id', type=int, required=True, help="Field owner_id cannot be empty") parser.add_argument('bod', type=lambda x: dt.datetime.strptime(x, "%Y-%m-%d").date(), required=True, help="Field bod cannot be empty") payload = parser.parse_args() if PetModel.find_by_name(payload['name'], payload['owner_id']): return {'message' : f'Pet {payload["name"]} already exits'} else: pet = PetModel(payload['name'], payload['sex'], payload['type'], payload['breed'], payload['owner_id'], payload['bod']) pet.save() return {'message' : 'Pet created without issues'}, 201
def put(cls, id: int): pet_json = request.get_json() user_id = get_jwt_identity() pet = PetModel.find_one_by_user_id(id, user_id) process = "" if pet: pet.name = pet_json["name"] pet.type = pet_json["type"] pet.breed = pet_json["breed"] pet.image_uri = pet_json["image_uri"] pet.disabled = bool(pet_json["disabled"]) pet.updated_at = str(datetime.utcnow()) for claw_data in pet_json["claws"]: claw = ClawModel.find_one_by_pet_id_and_name( id, claw_data["name"], claw_data["paw"]) if claw: claw.name = claw_data["name"] claw.paw = claw_data["paw"] claw.disabled = bool(claw_data["disabled"]) claw.skipped = bool(claw_data["skipped"]) claw.skip_length = int(claw_data["skip_length"]) claw.updated_at = str(datetime.utcnow()) else: # claw_data["pet_id"] = id # TODO: problem with foreign keys included when creating or updating pet with claws data... when creating I don't have pet_id, but I have it and I need to have it (probably) when updating. I think I need to remove include_fk in schemas and just use db.ForeignKey (that will probably do the trick?) or other way - use include_fk instead db.ForeignKey ??? claw_data["created_at"] = str(datetime.utcnow()) claw_data["updated_at"] = str(datetime.utcnow()) claw = claw_schema.load(claw_data) claw.save_to_db() process = "updating" else: pet_json["user_id"] = user_id pet_json["created_at"] = str(datetime.utcnow()) pet_json["updated_at"] = str(datetime.utcnow()) for claw in pet_json["claws"]: claw["created_at"] = str(datetime.utcnow()) claw["updated_at"] = str(datetime.utcnow()) pet = pet_schema.load(pet_json) process = "inserting" try: pet.save_to_db() except: return {"message": ERROR_INSERTING.format(process)}, 500 return pet_schema.dump(pet), 200
def delete(cls, id: int): user_id = get_jwt_identity() pet = PetModel.find_one_by_user_id(id, user_id) claws = ClawModel.find_all_by_pet_id(id) for claw in claws: claw.delete_from_db() if pet: pet.delete_from_db() return {"message": PET_DELETED}, 200 return {"message": PET_NOT_FOUND}, 404
def _list_pet(self): pet = PetModel.list_all() return list( map( lambda pet: { 'id': pet.id, 'name': pet.name, 'species': pet.species, 'breed': pet.breed, 'size': pet.size, 'weight': pet.weight, 'status': pet.status, 'insured_id': pet.insured_id }, pet))
def select_emergency_pet_by_proposal(proposal_number): proposal = ProposalModel.get_by_number(proposal_number) emergency = EmergencyPetModel.get_by_proposal(proposal.id) pet = PetModel.get_by_proposal(emergency.proposal_id) if emergency.provider_id != 0: provider = ProviderModel.get_by_id(emergency.provider_id) provider_associated = { 'id': provider.id, 'fantasy_name': provider.fantasy_name, 'provider_details': f'http://127.0.0.1:8080/api/user/{provider.user_id}' } else: provider_associated = '' insured_pacient = { 'id': pet.id, 'name': pet.name, 'species': pet.species, 'breed': pet.breed, 'size': pet.size, 'weight': pet.weight, 'proposal': { 'id': proposal.id, 'number': proposal.number, 'status': proposal.status, 'created_date': proposal.created_date.strftime("%d/%m/%Y"), 'plan': select_plan_proposal_by_id(proposal.plan_proposal_id) } } return { 'id': emergency.id, 'label': emergency.label, 'call_type': emergency.call_type, 'call': emergency.call, 'latitude': emergency.latitude, 'longitude': emergency.longitude, 'status_emergency': emergency.status, 'created_date': emergency.created_date.strftime("%d/%m/%Y"), 'provider': provider_associated, 'insured_pacient': insured_pacient }
def delete_insured(id): try: insured = InsuredModel.get_by_user_id(id) policy = PolicyModel.get_by_id(insured.policy_id) pet = PetModel.get_by_insured(insured.insured_id) proposal = ProposalModel.get_by_id(pet.proposal_id) pet_schedule = PetScheduleModel.get_by_pet(pet.id) if pet_schedule: pet_schedule.delete() proposal.delete() pet.delete() policy.delete() insured.delete() return {"success": True, "message": 'insured deleted'} except Exception as e: return {"success": False, "message": f"{e}"}
def select_pet_by_proposal_id(proposal_id): try: pet = PetModel.get_by_proposal(proposal_id) proposal = ProposalModel.get_by_id(proposal_id) plan = PlanProposalModel.get_by_id(proposal.plan_proposal_id) insured = InsuredModel.get_by_id(pet.insured_id) if pet is None: return {'success': False, 'message': 'Insured not found'} else: return { 'pet': { 'id': pet.id, 'name': pet.name, 'species': pet.species, 'breed': pet.breed, 'size': pet.size, 'weight': pet.weight }, 'proposal_details': { 'id': proposal.id, 'number': proposal.number, 'status': proposal.status, 'created_date': proposal.created_date.strftime("%d/%m/%Y"), 'plan_proposal': { 'id': plan.id, 'name': plan.name, 'desc': plan.desc, 'status': plan.status, 'created_date': plan.created_date.strftime("%d/%m/%Y") } }, 'owner': { 'name': f'{insured.first_name} {insured.last_name}', 'details': f'http://127.0.0.1:8080/api/user/{insured.user_id}' } } except Exception as e: return {"succes": False, "message": f'{e} invalid payload'}
def put(self, _id, owner): parser = reqparse.RequestParser() parser.add_argument('sex', type=str, required=True, help="Field sex cannot be empty") parser.add_argument('type', type=str, required=True, help="Field type cannot be empty") parser.add_argument('breed', type=str, required=True, help="Field breed cannot be empty") pet = PetModel.find_by_id(_id, owner) if pet: payload = parser.parse_args() pet.sex = payload['sex'] pet.type = payload['type'] pet.breed = payload['breed'] pet.save() return { 'message' : f'Pet {_id} changed.'} else: return { 'message' : f'Pet {_id} not found - owner : {owner}'}, 404
def post(self): item = request.get_json() if request.get_json() else request.form try: if item: insert_into_proposal(item['proposal']) proposal = ProposalModel.get_by_number( item['proposal']['number']) model = PetModel() model.name = item['name'] model.species = item['species'] model.breed = item['breed'] model.size = item['size'] model.weight = item['weight'] model.insured_id = item['insured_id'] model.proposal_id = proposal.id model.created_date = date.today() model.save() return 'created', 201 else: proposal.delete() return 'not created, invalid payload', 400 except Exception as e: return f"{e}", 500
def get(self, owner): return { 'pets' : [pet.json() for pet in PetModel.find_by_owner(owner)]}
def get(cls): user_id = get_jwt_identity() return { "pets": pet_list_schema.dump(PetModel.find_all_by_user_id(user_id)) }, 200