def get(self, item_id): item = ItemModel.find_by_id(item_id) if item is None: return {'status': False, 'message': 'Item not found', 'data': None} product_id = item.json()['product_id'] product = ProductModel.find_by_id(product_id) promotion_items = PromotionModel.find_promotion_items(product_id) sold_price = product.json()['price'] if promotion_items: for p_item in promotion_items: sold_price -= (p_item.json()['discount'] / 100) * product.json()['price'] p_item.used_items += 1 p_item.save_to_db() order = OrderModel(item_id, float("{:.2f}".format(sold_price))) order.save_to_db() item.delete_from_db() return { 'status': True, 'message': 'A item has been sold.', 'data': order.json() }
def get(self, _id): product = ProductModel.find_by_id(_id) if not product: return {'message': 'Product not found'}, 404 return product.json_with_children()
def post(self): """ Agrega un nuevo producto a los favoritos del usuario. """ if current_identity.user_type == user_types['vendor']: return { "message": "No puede marcar un producto como favorito." }, 401 data = FavouriteList.parser.parse_args() if not ProductModel.find_by_id(data['product_id']): return { "message": f"El producto con ID {data['product_id']!r} no ha sido encontrado." }, 404 if FavouriteModel.find_if_exists(current_identity.id, data['product_id']): return { "message": f"El producto con ID {data['product_id']!r} ya está marcado como favorito." }, 400 new_favourite = FavouriteModel(current_identity.id, data['product_id']) new_favourite.save_to_db() return new_favourite.json()
def put(self, product_id: int): """ Actualiza los detalles de un producto si pertenece al vendedor. """ product = ProductModel.find_by_id(product_id) if not product: return {"message": f"El producto con ID {product_id!r} no ha sido encontrado."}, 404 if product.user_id != current_identity.id or current_identity.user_type != user_types['vendor']: return {"message": "No tiene permitido modificar este producto."}, 401 data = Product.parser.parse_args() if imgurlRegex.match(data['image']) is None: return {"message": "La imagen debe ser una URL."}, 400 product.category_id = data['category_id'] product.name = data['name'] product.description = data['description'] product.price = data['price'] product.image = data['image'] product.visible = data['visible'] product.save_to_db() return product.json()
def get(self): recv_data = request.get_json() if recv_data is not None: product = ProductModel.find_by_id(recv_data['product_id']) if ProductModel.find_by_id(recv_data['product_id']): product_id = recv_data['product_id'] print(r.get("product_id/%d" % product_id)) result = eval(r.get("product_id/%d" % product_id)) return product.get_info() return {"message": "Product does not exist!!"}, 404 else: return { 'products': [product.get_info() for product in ProductModel.query.all()] }
def delete(self, _id): product = ProductModel.find_by_id(_id) status = 404 message = 'Product not found.' if product: status = 200 message = 'Product deleted' product.delete() return {'message': message}, status
def get(self, _id): product = ProductModel.find_by_id(_id) if not product: return {'message': 'Product not found.'}, 404 images = product.get_images() if len(images) == 0: return {'message': 'No images found.'}, 404 return {'images': [x.json() for x in images]}
def get(self, _id): product = ProductModel.find_by_id(_id) if not product: return {'message': 'Product not found.'}, 404 products = product.get_children() if len(products) == 0: return {'message': 'No children found.'}, 404 return {'products': [x.json() for x in products]}
def get(self, product_id: int): """ Devuelve los detalles de un producto si este es visible o pertenece al usuario. """ product = ProductModel.find_by_id(product_id) if not product: return {"message": f"El producto con ID {product_id!r} no ha sido encontrado."}, 404 if product.user_id != current_identity.id and not product.visible: return {"message": "No tiene permitido visualizar este producto."}, 401 return product.json()
def delete(self, product_id: int): """ Elimina el producto de la base de datos si le pertenece al vendedor. """ product = ProductModel.find_by_id(product_id) if not product: return {"message": f"El producto con ID {product_id!r} no ha sido encontrado."}, 404 if product.user_id != current_identity.id: return {"message": "No tiene permitido eliminar este producto."}, 401 product.delete_from_db() return {"message": f"Producto con ID {product_id!r} eliminado correctamente."}
def post(self): data = Promotion.parser.parse_args() if ProductModel.find_by_id(data['product_id']) is None: return { 'status': False, 'message': "Product with id = '{}' not found.".format(data['product_id']), 'data': None } t1 = datetime.strptime(data['t1'], "%d-%m-%Y %H:%M:%S") t2 = datetime.strptime(data['t2'], "%d-%m-%Y %H:%M:%S") if t1 >= t2: return { 'status': False, 'message': 'Start time must not be later than end time.', 'data': None } if data['discount'] > 100: return { 'status': False, 'message': 'Discount must not be bigger 100.', 'data': None } promotion_item = PromotionModel(data['product_id'], data['desc'], data['t1'], data['t2'], data['discount'], data['max_items']) try: promotion_item.save_to_db() except: return { 'status': False, 'message': 'An error occurred inserting the promotion item.', 'data': None }, 500 return { 'status': True, 'success': 'Promotion item created success,', 'data': promotion_item.json() }
def post(self): recv_data = GeoPrice.parser.parse_args() if ProductModel.find_by_id(recv_data['product_id']): product_id = recv_data['product_id'] data = {"price": recv_data['price'], "stock": recv_data['stock']} data = str(data) try: result = r.set("product_id/%d" % product_id, data) except: return { "message": "An Error occured while inserting the information!!" }, 500 return {"message": "Successfully Entered the product details"}, 201
def delete(self, id): product = ProductModel.find_by_id(id) if product is None: return { 'status': False, 'message': 'Product not found', 'data': None } product.delete_from_db() return { 'status': True, 'message': 'Product has been deleted', 'data': None }
def put(self,id): data = Product.parser.parse_args() product = ProductModel.find_by_id(id) if product is None: return { 'status': False, 'message':'Product not found', 'data': None } if not data['name']: return { 'status': False, 'message': 'Product name must not be empty.', 'data': None } if data['name'] != product.name: if ProductModel.find_by_name(data['name']): return { 'status': False, 'message': 'Product name is already exists.', 'data': None } product.name = data['name'] product.price = data['price'] product.category = data['category'] product.vendor = data['vendor'] product.save_to_db() return { 'status': True, 'message': 'The product has been updated.', 'data': product.json() }
def put(self, _id): data = parser.parse_args() product = ProductModel.find_by_id(_id) status = 200 if product: product.name = data['name'] product.description = data['description'] product.price = data['price'] else: product = ProductModel( data['name'], data['description'], data['price'], data['parent_id'] ) status = 201 product.save() return product.json(), status
def get(self, id): product = ProductModel.find_by_id(id) promotion_items = PromotionModel.find_promotion_items(id) if product: product_json = product.json() if promotion_items: origin_price = product_json['price'] sale_price = origin_price for item in promotion_items: sale_price -= (item.json()['discount']/100)*origin_price product_json['sale_price'] = float("{:.2f}".format(sale_price)) return { 'status': True, 'message': 'Success', 'data': product_json } return { 'status': False, 'message': 'Product not found.', 'data': None }, 404
def post(self): """ Permite realizar un nuevo pedido, los vendedores no pueden realizarlos. """ if current_identity.user_type == user_types['vendor']: return {"message": "No tiene permitido realizar pedidos."}, 401 data = OrderList.parser.parse_args() product = ProductModel.find_by_id(data['product_id']) if not product: return { "message": f"El producto con ID {data['product_id']!r} no ha sido encontrado." }, 404 if data['amount'] <= 0: return { "message": f"La cantidad ({data['amount']}) no es válida." }, 400 new_order = OrderModel(current_identity.id, product.user_id, **data) new_order.save_to_db() return new_order.json(), 201
def get(self, _id): return ProductModel.find_by_id(_id).json()