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 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