def variant_del(id): try: variant = ProductVariant.get_by_id(id) variant.delete() except Exception as e: return ApiResult({"r": 1, "msg": str(e)}) return ApiResult(dict())
def create_variant(product, **kwargs): defaults = {"product_id": product.id, "quantity": fake.random_int(1, 50)} defaults.update(kwargs) attributes = defaults.pop("attributes") variant = ProductVariant(**defaults) variant.attributes = attributes if variant.attributes: variant.title = get_name_from_attributes(variant) variant.save() return variant
def add_to_currentuser_cart(cls, quantity, variant_id): cart = cls.get_current_user_cart() variant = ProductVariant.get_by_id(variant_id) result, msg = variant.check_enough_stock(quantity) if result is False: flash(msg, "warning") return if cart: cart.quantity += quantity cart.save() else: cart = cls.create(user_id=current_user.id, quantity=quantity) line = CartLine.query.filter_by(cart_id=cart.id, variant_id=variant_id).first() if line: quantity += line.quantity line.update(quantity=quantity) else: CartLine.create(variant_id=variant_id, quantity=quantity, cart_id=cart.id)
def variant_manage(id=None): if id: variant = ProductVariant.get_by_id(id) form = VariantForm(obj=variant) else: form = VariantForm() if form.validate_on_submit(): if not id: variant = ProductVariant() form.populate_obj(variant) product_id = request.args.get("product_id") if product_id: variant.product_id = product_id variant.sku = str(variant.product_id) + "-" + str(form.sku_id.data) variant.save() return redirect(url_for("dashboard.product_detail", id=variant.product_id)) return render_template("product/variant.html", form=form)
def create_whole_order(cls, cart, note=None): # Step1, certify stock, voucher to_update_variants = [] to_update_orderlines = [] total_net = 0 for line in cart.lines: variant = ProductVariant.get_by_id(line.variant.id) result, msg = variant.check_enough_stock(line.quantity) if result is False: return result, msg variant.quantity_allocated += line.quantity to_update_variants.append(variant) orderline = OrderLine( variant_id=variant.id, quantity=line.quantity, product_name=variant.display_product(), product_sku=variant.sku, product_id=variant.sku.split("-")[0], unit_price_net=variant.price, is_shipping_required=variant.is_shipping_required, ) to_update_orderlines.append(orderline) total_net += orderline.get_total() voucher = None if cart.voucher_code: voucher = Voucher.get_by_code(cart.voucher_code) try: voucher.check_available(cart) except Exception as e: return False, str(e) # Step2, create Order obj try: shipping_method_id = None shipping_method_title = None shipping_method_price = 0 shipping_address = None if cart.shipping_method_id: shipping_method = ShippingMethod.get_by_id( cart.shipping_method_id) shipping_method_id = shipping_method.id shipping_method_title = shipping_method.title shipping_method_price = shipping_method.price shipping_address = UserAddress.get_by_id( cart.shipping_address_id).full_address order = cls.create( user_id=current_user.id, token=str(uuid4()), shipping_method_id=shipping_method_id, shipping_method_name=shipping_method_title, shipping_price_net=shipping_method_price, shipping_address=shipping_address, status=OrderStatusKinds.unfulfilled.value, total_net=total_net, ) except Exception as e: return False, str(e) # Step3, process others if note: order_note = OrderNote(order_id=order.id, user_id=current_user.id, content=note) db.session.add(order_note) if voucher: order.voucher_id = voucher.id order.discount_amount = voucher.get_vouchered_price(cart) order.discount_name = voucher.title voucher.used += 1 db.session.add(order) db.session.add(voucher) for variant in to_update_variants: db.session.add(variant) for orderline in to_update_orderlines: orderline.order_id = order.id db.session.add(orderline) for line in cart.lines: db.session.delete(line) db.session.delete(cart) db.session.commit() return order, "success"
def variant(self): return ProductVariant.get_by_id(self.variant_id)