def _container_discount(coupon, order_obj): all_items = order_obj.items # {obj_id: None, coll_name: None, quantity:None} total_price = 0 coupon_container = items.get_container(coupon.obj_id) for i in all_items: item = items.get(i["obj_id"]) item_container = items.item_container(item) if item_container._id == coupon.obj_id or items.parent_container(coupon_container, item_container): total_price += item.price * i["quantity"] if total_price >= coupon.coupon_value: return Decimal(coupon.coupon_value) else: return Decimal(total_price)
def is_valid(coupon, order_obj): """ Checks if a coupon is valid on an order """ user_obj = users.get(order_obj.user_id) user_groups = user_obj.groups utc_now = datetime.datetime.utcnow() order_price = orders.total_price(order_obj) if coupon.user_scope == CouponUserScope.GROUP: if coupon.user_group not in user_groups: return False if coupon.user_scope == CouponUserScope.SPECIFIC: if coupon.user_id != user_obj._id: return False if coupon.valid_times <= 0: return False if coupon.coupon_lifetime_type == CouponLifetime.LIMITED: if utc_now > coupon.expire_utc_datetime: return False if utc_now < coupon.begins_utc_datetime: return False if coupon.order_minimum_spending < order_price: return False all_items = order_obj.items # {obj_id: None, coll_name: None, quantity:None} flag = False for i in all_items: i["obj"] = items.get(i["obj_id"]) i["container"] = items.item_container(i["obj"]) if coupon.coupon_scope == CouponScope.CONTAINER_WIDE: container_obj = items.get_container(coupon.obj_id) if items.is_parent_container(container_obj, i["container"]) or i["container"]._id == coupon.obj_id: flag = True break if coupon.coupon_scope == CouponScope.ITEM_ONLY: if i["obj_id"] == coupon.obj_id: flag = True break if not flag: return False return True
def valid_on_item(discount, item_obj): """ Checks if a discount is valid on an item. """ item_container = items.item_container(item_obj) if discount.discount_scope == DiscountScope.ALL_ITEMS: return False if not is_item_scoped(discount, item_obj) and is_container_scoped(discount, item_container): return False utc_now = datetime.datetime.utcnow() if item_obj.price <= discount.item_minimum_spending: return False if discount.discount_lifetime_type == DiscountLifetime.LIMITED: if discount.expire_utc_datetime is not None and utc_now > discount.expire_utc_datetime: return False if discount.begins_utc_datetime is not None and utc_now < discount.begins_utc_datetime: return False if discount.status == DiscountStatus.DISABLED: return False return True