Exemplo n.º 1
0
def is_container_scoped(discount, container_obj):
    if discount.discount_scope == DiscountScope.ALL_ITEMS:
        return True
    if discount.collection_name != items.Container.coll_name():
        return False
    if container_obj._id != discount.obj_id and not items.is_parent_container(items.get_container(discount.obj_id),
                                                                              container_obj):
        return False
    return True
Exemplo n.º 2
0
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