def insert_into_provider(item, user): try: if item: provider = ProviderModel() provider.business_name = item['business_name'] provider.fantasy_name = item['fantasy_name'] provider.type_provider = item['type_provider'] provider.cnpj = item['cnpj'] provider.address = item['address'] # provider.password = item['password'] provider.user_id = user.id provider.save() return {"succes": True, "message": 'Provider created'} else: user.delete() return { "succes": False, "message": 'Not created provider, invalid payload' } except Exception as e: user.delete() return { "succes": False, "message": f'{e} invalid payload', 'type_error': 'provider/utils' }
def delete(self, provider_id): try: ProviderModel.delete_from_db(provider_id) except: return {"message": "An error occurred deleting the Provider"}, 500 return {"message": "Provider deleted"}, 200
def test_get_provider(self): provider = ProviderModel.get(1) assert provider.name == 'Fornecedor 01' provider = ProviderModel.get_by_cnpj('04790618000153') assert provider.name == 'Fornecedor 01' providers = ProviderModel.list() assert len(providers) == 1 assert type(providers) == type([])
def get(self): custom_filter = ProviderList.parser.parse_args() if custom_filter.get('deleted'): return [ transaction.json() for transaction in ProviderModel.filter_by_deleted( str_to_bool(custom_filter.get('deleted'))) ] else: return [provider.json() for provider in ProviderModel.find_all()]
def put(self): data = Provider.parser.parse_args() provider = ProviderModel(**data) try: provider.update_to_db() except: return {"message": "An error occurred updating the operation"}, 500 return provider.json(), 200
def post(self): try: data = request.get_json() item = ProviderModel() for parameter in data: setattr(item, parameter, data[parameter]) item.save() return "success", 201 except: return "error", 401
def put(self): provider = ObjectDict(json_decode(self.request.body)) _provider = ProviderModel.get_by_id(self.db, provider.id) if _provider: try: _provider = ProviderModel.update(self.db, provider.id, provider.chain_id, provider.name, provider.contact, provider.phone, provider.email) self.finish_json(result=_provider.tojson()) except Exception, e: print e self.finish_json(errcode=507, errmsg=str(e))
def first_product(): if ProviderModel.get_by_cnpj("04790618000153") == None: first_provider() return new_product = ProductModel() new_product.name = "Produto 01" new_product.provider = ProviderModel.get_by_cnpj("04790618000153") new_product.quantity = 100 new_product.price = 10.0 new_product.available = True new_product.save() return
def get(self): if 'id' in request.args: item = ProviderModel.get(request.args['id']) item = serialize_model(item) return item elif 'email' in request.args: item = ProviderModel.get_by_email(request.args['email']) item = serialize_model(item) return item elif 'cnpj' in request.args: item = ProviderModel.get_by_cnpj(request.args['cnpj']) item = serialize_model(item) return item list = ProviderModel.list() return serialize_model_list(list)
def get(self): start = self.get_query_argument('start', None) limit = self.get_query_argument('limit', None) providers = ProviderModel.get_all(self.db, start, limit) total = ProviderModel.get_count(self.db) providers = [provider.tojson() for provider in providers] self.finish_json(result=dict( providers=providers, start=start, limit=limit, total=total))
def _get_pet_schedule(self, id_pet_schedule): pet_schedule = PetScheduleModel.get_by_id(id_pet_schedule) provider = ProviderModel.get_by_id(pet_schedule.provider_id) pacient = select_pet_by_proposal_id(pet_schedule.proposal_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}' } if pet_schedule is None: return {'message': 'Plan not found'}, 404 return { 'id': pet_schedule.id, 'name': pet_schedule.name, 'date': pet_schedule.date, 'time': pet_schedule.time, 'email': pet_schedule.email, 'status': pet_schedule.status, 'created_date': pet_schedule.created_date.strftime("%d/%m/%Y"), 'provider': provider_associated, 'pacient': pacient }
def delete(self): try: if 'id' in request.args: item = ProviderModel.delete(request.args['id']) return "success", 201 return "No ID", 401 except: return "error", 401
def delete_provider(id): try: provider = ProviderModel.get_by_user_id(id) provider.delete() return {"success": True, "message": 'provider deleted'} except Exception as e: return {"success": False, "message": f"{e}"}
def post(self): data = request.get_json() email = data['email'].strip() password = encrypt(data['password']) user = UserModel.authenticate(email, password) if user: if user.type_user == 'insured': user_data = InsuredModel.get_by_user_id(user.id) user_data_json = { 'id': user_data.id, 'first_name':user_data.first_name, 'last_name': user_data.last_name, 'cpf':user_data.cpf, } elif user.type_user == 'provider': user_data = ProviderModel.get_by_user_id(user.id) user_data_json = { 'id': user_data.id, 'business_name':user_data.business_name, 'fantasy_name': user_data.fantasy_name, 'cnpj':user_data.cnpj, } else: user_data = CollaboratorModel.get_by_user_id(user.id) user_data_json ={ 'id': user_data.id, 'first_name':user_data.first_name, 'last_name': user_data.last_name, 'registration':user_data.registration, } access = create_jwt({ 'id_insured': user.id, 'email': user.email, 'status': user.status, 'type_user':user.type_user }) return { 'id_insured': user.id, 'email': user.email, 'status': user.status, 'type_user':user.type_user, 'created_date':user.created_date.strftime("%d/%m/%Y"), 'jwt': access, 'user_data':user_data_json }, 200 else: return {'message': 'Invalid credentials'}, 400
def test_save_product(self): products = len(ProductModel.list()) new_product = ProductModel() new_product.name = "Produto 02" new_product.provider = ProviderModel.get_by_cnpj("04790618000153") new_product.quantity = 100 new_product.price = 10.0 new_product.available = True new_product.save() assert products + 1 == len(ProductModel.list())
def put(self): try: data = request.get_json() item = ProviderModel.get(data['id']) for parameter in data: setattr(item, parameter, data[parameter]) item.update() return "success", 201 except: return "error", 401
def post(self): product_data = Product.attribute.parse_args() product = ProductModel(**product_data) if not ProviderModel.find_by_id_provider(product.id_provider): return { 'message': 'The product must be associated with a valid id. ' }, 400 try: product.save_product() except: return {'message': 'Internal error'}, 500 return product.json(), 200
def get(self): provider = self.get_query_argument('provider', None) contact = self.get_query_argument('contact', None) providers = ProviderModel.query_by_provider_and_contact(self.db, provider, contact) providers = [dict( id=provider.id, name=provider.name, contact=provider.contact, phone=provider.phone, email=provider.email) for provider in providers] self.finish_json(result=dict( providers=providers))
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 select_emergency_insured_by_policy(policy_number): policy = PolicyModel.get_by_number(policy_number) emergency = EmergencyInsuredModel.get_by_policy(policy.id) insured = InsuredModel.get_by_policy_id(emergency.policy_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': insured.id, 'first_name': insured.first_name, 'last_name': insured.last_name, 'cpf': insured.cpf, 'policy': { 'id': policy.id, 'number': policy.number, 'status': policy.status, 'created_date': policy.created_date.strftime("%d/%m/%Y"), 'plan': select_plan_policy_by_id(policy.plan_policy_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 select_provider_by_id(id): try: provider = ProviderModel.get_by_id(id) if provider is None: return {'success': False, 'message': 'Provider not found'} else: return { 'id': provider.id, 'business_name': provider.business_name, 'fantasy_name': provider.fantasy_name, 'cnpj': provider.cnpj, 'address': provider.address, 'type_provider': provider.type_provider } return {"succes": True, "message": 'Provider found'} except Exception as e: return {"succes": False, "message": f'{e} invalid payload'}
def select_emergency_pet_by_id(id_emergency): emergency = EmergencyPetModel.get_by_id(id_emergency) 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}' } return { 'id': emergency.id, 'name': emergency.name, 'date': emergency.date, 'time': emergency.time, 'email': emergency.email, 'status_emergency': emergency.status, 'created_date': emergency.created_date.strftime("%d/%m/%Y"), 'provider': provider_associated, 'pet_pacient': select_pet_by_proposal_id(emergency.proposal_id) }
def _list_provider(self): provider = ProviderModel.list_all() return list( map( lambda provider: { 'id': provider.id, 'business_name': provider.business_name, 'fantasy_name': provider.fantasy_name, 'cnpj': provider.cnpj, 'address': provider.address, 'type_provider': provider.type_provider, 'url_details': f'http://127.0.0.1:8080/api/user/{provider.user_id}' }, provider))
def update_provider(item, user): try: if item: provider = ProviderModel.get_by_user_id(user.id) if 'business_name' in item: provider.business_name = item['business_name'] if 'fantasy_name' in item: provider.fantasy_name = item['fantasy_name'] if 'type_provider' in item: provider.type_provider = item['type_provider'] if 'cnpj' in item: provider.cnpj = item['cnpj'] if 'tel' in item: provider.tel = item['tel'] if 'cel' in item: provider.cel = item['cel'] if 'address' in item: provider.address = item['address'] if 'status' in item: provider.status = item['status'] if 'password' in item: provider.password = item['password'] provider.save() return {"success": True, "message": 'Provider edited'} else: return { "success": False, "message": 'Not edited provider, invalid payload' } except Exception as e: return { "success": False, "message": f'{e} invalid payload', 'type_error': 'provider/utils' }
def put(self, cnpj): provider_data = Provider.attribute.parse_args() provider_found = ProviderModel.find_provider(cnpj) if not provider_found: return { "message": "Provider CNPJ '{}' does not found.".format(cnpj) }, 204 provider_found = ProviderModel.find_provider(cnpj) if provider_found: provider_found.update_provider(**provider_data) provider_found.save_provider() return provider_found.json(), 200 provider = ProviderModel(cnpj, **provider_data) try: provider.save_provider() except: return {'message': 'Internal error'}, 500 return provider.json(), 201
def post(self): name = request.json['name'] cnpj = re.sub('[^0-9]', '', request.json['cnpj']) city = request.json['city'] if ProviderModel.find_provider(cnpj): return {"message": "CNPJ '{}' already exists.".format(cnpj)}, 400 if not ProviderModel.validate_cnpj(cnpj): return {"message": "CNPJ '{}' is not valid.".format(cnpj)}, 400 provider = ProviderModel(name, cnpj, city) try: provider.save_provider() except: return { "message": "An internal error ocurred trying to create a new provider" }, 500 return provider.json()
def select_emergency_insured_by_id(id_emergency): emergency = EmergencyInsuredModel.get_by_id(id_emergency) provider = ProviderModel.get_by_id(emergency.provider_id) insured = InsuredModel.get_by_proposal(emergency.policy_id) policy = PolicyModel.get_by_id(emergency.policy_id) insured_pacient = { 'id': insured.id, 'first_name': insured.first_name, 'last_name': insured.last_name, 'cpf': insured.cpf, 'policy': { 'id': policy.id, 'number': policy.number, 'status': policy.status, 'created_date': policy.created_date.strftime("%d/%m/%Y"), 'plan': select_plan_policy_by_id(policy.plan_policy_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}' } return { 'id': emergency.id, 'name': emergency.name, 'date': emergency.date, 'time': emergency.time, 'email': emergency.email, 'status_emergency': emergency.status, 'created_date': emergency.created_date.strftime("%d/%m/%Y"), 'provider': provider_associated, 'insured_pacient': insured_pacient }
def delete(self, cnpj): provider = ProviderModel.find_provider(cnpj) if provider: provider.delete_provider() return {'message': 'Provider deleted.'}, 200 return {'message': 'Provider not found.'}, 204
def test_save_provider(self): providers = len(ProviderModel.list()) new_provider = ProviderModel() new_provider.name = "Fornecedor 02" new_provider.cnpj = "98617744000154" new_provider.address = "Endereço 02" new_provider.city = "Cidade 02" new_provider.country = "País 02" new_provider.cep_code = "000000-000" new_provider.responsible_name = "Matheus D'Adamo Milani 01" new_provider.responsible_email = "*****@*****.**" new_provider.responsible_phone = "+55(11)97717-8888" new_provider.active = True new_provider.save() assert providers + 1 == len(ProviderModel.list())
def test_update_provider(self): provider = ProviderModel.get_by_cnpj('98617744000154') provider.name = "Fornecedor 02 editado" provider.update() assert provider.name == "Fornecedor 02 editado"
def test_delete_provider(self): providers = len(ProviderModel.list()) provider = ProviderModel.delete(2) assert providers - 1 == len(ProviderModel.list())
def post(self): provider = ObjectDict(json_decode(self.request.body)) _provider = ProviderModel.add(self.db, provider.chain_id, provider.name, provider.contact, provider.phone, provider.email) self.finish_json(result=_provider.tojson())
def get(self, cnpj): provider = ProviderModel.find_provider(cnpj) if provider: return provider.json(), 200 return {'message': 'Not found'}, 204
def get(self, provider_id): return ProviderModel.find_by_id(provider_id).json()