Exemple #1
0
def test_form_populate_initial_data(rf, admin_user):
    shop = get_default_shop()
    supplier = get_default_supplier()

    campaign = BasketCampaign(discount_percentage=0.1, shop=shop)
    campaign.save()

    # Test that correct initial value is returned for non-many-to-many field
    product_amount_initial = 10
    product_amount_condition = BasketTotalProductAmountCondition(product_count=product_amount_initial)
    product_amount_condition.save()
    campaign.conditions.add(product_amount_condition)

    products_count_initial = 5
    for i in range(products_count_initial):
        create_product(printable_gibberish(), shop=shop, supplier=supplier, default_price="20")
    products_initial = Product.objects.all()[:products_count_initial]
    assert len(products_initial) == products_count_initial

    # Test that correct initial value is returned for many-to-many field
    products_in_basket_condition = ProductsInBasketCondition.objects.create()
    products_in_basket_condition.values = products_initial
    products_in_basket_condition.save()
    campaign.conditions.add(products_in_basket_condition)

    assert len(campaign.conditions.all()) == 2

    request=apply_request_middleware(rf.get("/"), user=admin_user)
    form = BasketCampaignForm(request=request, instance=campaign)
    assert form.fields["basket_product_condition"].initial == product_amount_initial
    assert set(form.fields["basket_products_condition"].initial) == set([p.pk for p in products_initial])
Exemple #2
0
    def get_new_lines(self, order_source, lines):
        price_so_far = sum((x.price for x in lines), order_source.zero_price)

        def get_discount_line(campaign, amount, price_so_far):
            new_amount = min(amount, price_so_far)
            price_so_far -= new_amount
            return self._get_campaign_line(campaign, new_amount, order_source)

        best_discount = None
        best_discount_campaign = None
        for campaign in BasketCampaign.get_matching(order_source, lines):
            if campaign.discount_amount:
                discount_amount = campaign.discount_amount
            else:
                discount_amount = order_source.total_price_of_products * campaign.discount_percentage

            # if campaign has coupon, match it to order_source.codes
            if campaign.coupon:
                # campaign was found because discount code matched. This line is always added
                yield get_discount_line(campaign, discount_amount,
                                        price_so_far)
            elif best_discount is None or discount_amount > best_discount:
                best_discount = discount_amount
                best_discount_campaign = campaign

        if best_discount is not None:
            yield get_discount_line(best_discount_campaign, best_discount,
                                    price_so_far)
Exemple #3
0
    def get_new_lines(self, order_source, lines):
        price_so_far = sum((x.price for x in lines), order_source.zero_price)

        def get_discount_line(campaign, amount, price_so_far):
            new_amount = min(amount, price_so_far)
            price_so_far -= new_amount
            return self._get_campaign_line(campaign, new_amount, order_source)

        best_discount = None
        best_discount_campaign = None
        for campaign in BasketCampaign.get_matching(order_source, lines):
            if campaign.discount_amount:
                discount_amount = campaign.discount_amount
            else:
                discount_amount = order_source.total_price_of_products * campaign.discount_percentage

            # if campaign has coupon, match it to order_source.codes
            if campaign.coupon:
                # campaign was found because discount code matched. This line is always added
                yield get_discount_line(campaign, discount_amount, price_so_far)
            elif best_discount is None or discount_amount > best_discount:
                best_discount = discount_amount
                best_discount_campaign = campaign

        if best_discount is not None:
            yield get_discount_line(best_discount_campaign, best_discount, price_so_far)
Exemple #4
0
    def get_new_lines(self, order_source, lines):
        matching_campaigns = BasketCampaign.get_matching(order_source, lines)
        for line in self._handle_total_discount_effects(matching_campaigns, order_source, lines):
            yield line

        for line in self._handle_line_effects(matching_campaigns, order_source, lines):
            yield line
Exemple #5
0
    def get_new_lines(self, order_source, lines):
        matching_campaigns = BasketCampaign.get_matching(order_source, lines)
        for line in self._handle_total_discount_effects(
                matching_campaigns, order_source, lines):
            yield line

        for line in self._handle_line_effects(matching_campaigns, order_source,
                                              lines):
            yield line