def test_get_product(self): product = ProductModel.get(1) assert product.name == 'Produto 01' products = ProductModel.list() assert len(products) == 1 assert type(products) == type([])
def test_update_product(self): product = ProductModel.get(2) product.name = "Produto 02 editado" product.quantity = 0 product.price = 50.0 product.available = False product.update() assert product.name == "Produto 02 editado" assert product.available == False
def get(self): if 'id' in request.args: item = ProductModel.get(request.args['id']) item = serialize_model(item) return item elif 'id_provider' in request.args: itens = ProductModel.list_by_provider(request.args['id_provider']) itens = serialize_model_list(itens) return itens list = ProductModel.list() return serialize_model_list(list)
def put(self): try: data = request.get_json() item = ProductModel.get(data['id']) for parameter in data: setattr(item, parameter, data[parameter]) item.update() return "success", 201 except: return "error", 401
def post(self): current_user = jwt.decode(request.headers['Authorization'], environ.get('JWT_SECRET_KEY'), options={'verify_exp': False}) if Roles().enum_to_name(current_user['sub']['role']) == 'admin': data = request.get_json() item = OrderModel() error = '' for product in data['products']: prod = ProductModel.get(product['id']) if prod == None: error = "Product not found. ID: " + product['id'] if float(prod.quantity) - float(product['quantity']) < 0: error = "Insuficient Product" if prod.available is False: error = "Unavailable Product" if float(prod.quantity) - float(product['quantity']) == 0: prod.available = False prod.update() for parameter in data: setattr(item, parameter, data[parameter]) item.products = str(data['products']) item.status = 0 if error == '' else 4 item.save() return "success", 201 else: error = '' data = request.get_json() item = OrderModel() for product in data['products']: prod = ProductModel.get(product['id']) if prod == None: error = "Product not found. ID: " + product['id'] if float(prod.quantity) - float(product['quantity']) < 0: error = "Insuficient Product" if prod.available is False: error = "Unavailable Product" if float(prod.quantity) - float(product['quantity']) == 0: prod.available = False prod.update() item.id_user = current_user['sub']['id'] item.products = str(data['products']) item.status = 0 if error == '' else 4 item.save() return "success", 201