Example #1
0
def apply_promo_store(userid, store_name):
    itemlist = find_shelf(userid, store_name)
    num_items = len(itemlist)
    print "Apply_promo: We have " + str(num_items) + " items in " + store_name + " shelf."
    total_combinations = 1 << (num_items)
    total_combinations -= 1
    print "Total combination: " + str(total_combinations)
    date_ = datetime.date.today()
    promo_date = Promoinfo.objects.filter(d=date_)
    if store_name == "Express":
        i = 0
    if store_name == "J.Crew":
        i = 1
    promo = promo_date.filter(store__id=i)

    # for all possible combinations
    # find the price by calling match.py
    # upper bound is total_combinations+1 because we are starting with index 1
    # and that is because we don't want to calculate discount for an empty wishlist
    # which will happen when j = 0

    for j in range(1, total_combinations + 1):
        wishlist = find_combination(itemlist, j)
        cached_result, digest = check_if_combination_exists(wishlist)
        if cached_result == None:
            print "No, didn't find result for list " + str(j) + " in cache, so storing it"
            orig_cost, total_cost, savings, shipping = match.match(store_name, date_, copy.deepcopy(wishlist), promo)
            # store this result
            new_result = StoreItemCombinationResults(
                combination_id=digest, price=orig_cost, saleprice=total_cost, free_shipping=shipping
            )
            new_result.save()

    print "Done with apply_promo_store"
Example #2
0
def apply_promo(request, d1, d2):
    if "u" in request.GET and request.GET["u"]:
        userid = urllib.unquote(request.GET["u"].decode("utf-8"))
        result_list = {}
        # for each store-shelf
        shelf_per_store = find_shelf_store_based_for_user(userid)
        for i in range(0, len(shelf_per_store)):
            # how many items in this shelf
            store_name = stores[i]
            num_items = len(shelf_per_store[store_name])
            print "Apply_promo: We have " + str(num_items) + " items in " + store_name + " shelf."
            total_combinations = 1 << (num_items)
            total_combinations -= 1
            print "Total combination: " + str(total_combinations)
            date_ = datetime.date.today()
            promo_date = Promoinfo.objects.filter(d=date_)
            promo = promo_date.filter(store__id=i)

            # for all possible combinations
            # find the price by calling match.py
            # upper bound is total_combinations+1 because we are starting with index 1
            # and that is because we don't want to calculate discount for an empty wishlist
            # which will happen when j = 0
            itemlist = []
            for j in range(1, total_combinations + 1):
                wishlist = find_combination(shelf_per_store[store_name], j)
                cached_result, digest = check_if_combination_exists(wishlist)
                if cached_result == None:
                    print "No, didn't find result for list " + str(j) + " in cache, so storing it"
                    orig_cost, total_cost, savings, shipping = match.match(
                        store_name, date_, copy.deepcopy(wishlist), promo
                    )
                    # store this result
                    new_result = StoreItemCombinationResults(
                        combination_id=digest, price=orig_cost, saleprice=total_cost, free_shipping=shipping
                    )
                    new_result.save()
                else:
                    print "Great, found the result! Using it here."
                    orig_cost = cached_result.price
                    total_cost = cached_result.saleprice
                    savings = cached_result.price - cached_result.saleprice
                    shipping = cached_result.free_shipping

                print "RESULT:: " + str(j) + " " + str(store_name) + " " + str(orig_cost) + " " + str(
                    total_cost
                ) + " " + str(savings)
                itemlist.append(
                    {"orig_cost": orig_cost, "total_cost": total_cost, "savings": savings, "shipping": shipping}
                )

            result_list[store_name] = itemlist
        return list_detail.object_list(
            request,
            queryset=WishlistI.objects.none(),
            template_name="apply_promo.html",
            extra_context={"uid": userid, "result_list": result_list},
        )