def product_create_step2(): form = ProductForm() product_type_id = request.args.get("product_type_id", 1, int) product_type = ProductType.get_by_id(product_type_id) categories = Category.query.all() if form.validate_on_submit(): product = Product(product_type_id=product_type_id) product = _save_product(product, form) product.generate_variants() return redirect(url_for("dashboard.product_detail", id=product.id)) return render_template( "product/product_create_step2.html", form=form, product_type=product_type, categories=categories, )
def check_available_by_cart(self, cart): if self.type == VoucherTypeKinds.value.value: if self.limit and cart.subtotal < self.limit: raise Exception( f"The order total amount is not enough({self.limit}) to use this voucher code" ) elif self.type == VoucherTypeKinds.shipping.value: if self.limit and cart.shipping_method_price < self.limit: raise Exception( f"The order shipping price is not enough({self.limit}) to use this voucher code" ) elif self.type == VoucherTypeKinds.product.value: product = Product.get_by_id(self.product_id) # got any product in cart, should be zero if cart.get_product_price(self.product_id) == 0: raise Exception( f"This Voucher Code should be used for {product.title}") if self.limit and cart.get_product_price( self.product_id) < self.limit: raise Exception( f"The product {product.title} total amount is not enough({self.limit}) to use this voucher code" ) elif self.type == VoucherTypeKinds.category.value: category = Category.get_by_id(self.category_id) if cart.get_category_price(self.category_id) == 0: raise Exception( f"This Voucher Code should be used for {category.title}") if self.limit and cart.get_category_price( self.category_id) < self.limit: raise Exception( f"The category {category.title} total amount is not enough({self.limit}) to use this voucher code" )
def product_del(id): try: product = Product.get_by_id(id) product.delete() except Exception as e: return ApiResult({"r": 1, "msg": str(e)}) return ApiResult(dict())
def product_edit(id): product = Product.get_by_id(id) form = ProductForm(obj=product) if form.validate_on_submit(): _save_product(product, form) return redirect(url_for("dashboard.product_detail", id=product.id)) categories = Category.query.all() context = {"form": form, "categories": categories, "product": product} return render_template("product/product_edit.html", **context)
def create_product(**kwargs): description = fake.paragraphs(5) defaults = { "title": fake.company(), "basic_price": fake.pydecimal(2, 2, positive=True), "description": "\n\n".join(description), "is_featured": random.choice([0, 1]), } defaults.update(kwargs) return Product.create(**defaults)
def index(): def get_today_num(model): target = db.cast(datetime.now(), db.DATE) which = db.cast(model.created_at, db.DATE) return model.query.filter(which == target).count() def get_order_status(status): return { "count": Order.query.filter_by(status=status).count(), "kind": status, } onsale_products_count = Product.query.filter_by(on_sale=True).count() hot_product_ids = ( db.session.query(OrderLine.product_id, func.count(OrderLine.product_id)) .group_by(OrderLine.product_id) .order_by(func.count(OrderLine.product_id).desc()) .all() ) top5_products = [] for product_id, order_count in hot_product_ids[:5]: p = Product.get_by_id(product_id) p.order_count = order_count top5_products.append(p) activity = OrderEvent.query.order_by(OrderEvent.id.desc()).limit(10) context = { "orders_total": Order.query.count(), "orders_today": get_today_num(Order), "users_total": User.query.count(), "users_today": get_today_num(User), "order_unfulfill": get_order_status(OrderStatusKinds.unfulfilled.value), "order_fulfill": get_order_status(OrderStatusKinds.fulfilled.value), "onsale_products_count": onsale_products_count, "top_products": top5_products, "activity": activity, "order_events": OrderEvents, } return render_template("index.html", **context)
def product_detail(id): product = Product.get_by_id(id) return render_template("product/detail.html", product=product)
def home(): products = Product.get_featured_product() return render_template("public/home.html", products=products)