def category_del(id): try: category = Category.get_by_id(id) category.delete() except Exception as e: return ApiResult({"r": 1, "msg": str(e)}) return ApiResult(dict())
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 get_or_create_category(category_schema, placeholder_dir): if "parent" in category_schema: parent_id = get_or_create_category(category_schema["parent"], placeholder_dir).id else: parent_id = 0 category_name = category_schema["name"] image_name = category_schema["image_name"] image_dir = placeholder_dir / "products-list" defaults = {"background_img": str(image_dir / image_name)} category, _ = Category.get_or_create(title=category_name, parent_id=parent_id, **defaults) return category
def category_manage(id=None): if id: category = Category.get_by_id(id) form = CategoryForm(obj=category) else: form = CategoryForm() if form.validate_on_submit(): if not id: category = Category() category.title = form.title.data category.parent_id = form.parent_id.data image = form.bgimg_file.data background_img = image.filename upload_file = current_app.config["UPLOAD_DIR"] / background_img upload_file.write_bytes(image.read()) category.background_img = ( current_app.config["UPLOAD_FOLDER"] + "/" + background_img ) category.save() return redirect(url_for("dashboard.categories")) parents = Category.first_level_items() return render_template("product/category.html", form=form, parents=parents)