示例#1
0
def test_computing_simple_product_relations(rf):
    shop = get_default_shop()
    supplier = get_default_supplier()
    product = create_product("simple-test-product", shop)
    related_product = create_product("simple-related-product", shop)
    quantities = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
    for quantity in quantities:
        order = create_order_with_product(product,
                                          supplier,
                                          quantity=1,
                                          taxless_base_unit_price=6,
                                          shop=shop)
        add_product_to_order(order,
                             supplier,
                             related_product,
                             quantity=quantity,
                             taxless_base_unit_price=6)

    assert ProductCrossSell.objects.count() == 0
    add_bought_with_relations_for_product(product.pk)
    assert ProductCrossSell.objects.count() == 1
    cross_sell_product = ProductCrossSell.objects.filter(
        product1=product).first()
    assert cross_sell_product.product2 == related_product
    assert cross_sell_product.weight == sum(quantities)

    add_bought_with_relations_for_product(related_product.id)
    assert ProductCrossSell.objects.count() == 2
    cross_sell_product = ProductCrossSell.objects.filter(
        product1=related_product).first()
    assert cross_sell_product.product2 == product
    assert cross_sell_product.weight == len(quantities)
    def handle(self, *args, **options):
        # Clear all existing ProductCrossSell objects
        ProductCrossSell.objects.filter(type=ProductCrossSellType.BOUGHT_WITH).delete()

        # Handle all ordered products
        ordered_product_ids = OrderLine.objects.filter(type=OrderLineType.PRODUCT).values_list("product_id", flat=True)
        seen_product_ids = set()
        for product_id in ordered_product_ids:
            if product_id in seen_product_ids:
                continue
            seen_product_ids.add(product_id)
            add_bought_with_relations_for_product(product_id)
    def handle(self, *args, **options):
        # Clear all existing ProductCrossSell objects
        ProductCrossSell.objects.filter(type=ProductCrossSellType.BOUGHT_WITH).delete()

        # Handle all ordered products
        ordered_product_ids = OrderLine.objects.filter(type=OrderLineType.PRODUCT).values_list("product_id", flat=True)
        seen_product_ids = set()
        for product_id in ordered_product_ids:
            if product_id in seen_product_ids:
                continue
            seen_product_ids.add(product_id)
            add_bought_with_relations_for_product(product_id)
    def handle(self, *args, **options):
        # Clear all existing ProductCrossSell objects
        ProductCrossSell.objects.filter(type=ProductCrossSellType.BOUGHT_WITH).delete()

        # Handle all ordered products
        ordered_product_ids = OrderLine.objects.filter(
            type=OrderLineType.PRODUCT).values_list("product_id", flat=True).distinct()
        for product_id in ordered_product_ids.distinct():
            add_bought_with_relations_for_product(product_id)

        for shop in Shop.objects.all():
            context_cache.bump_cache_for_item(cache_utils.get_cross_sells_cache_item(shop))
def test_product_relations_max_quantity(rf):
    shop = get_default_shop()
    supplier = get_default_supplier()
    product = create_product("simple-test-product", shop)
    quantities = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
    for i, quantity in enumerate(quantities):
        order = create_order_with_product(product, supplier, quantity=1, taxless_base_unit_price=6, shop=shop)
        add_product_to_order(
            order, supplier, create_product("product-%s" % i, shop), quantity=quantity, taxless_base_unit_price=6
        )

    assert ProductCrossSell.objects.count() == 0
    add_bought_with_relations_for_product(product.pk, max_quantity=5)
    assert ProductCrossSell.objects.count() == 5
    # Test that ordering is ok
    assert not ProductCrossSell.objects.filter(weight=1).exists()
    assert ProductCrossSell.objects.filter(weight=11).exists()
def test_product_relations_variation_products(rf):
    t_shirt, hoodie = _init_test_with_variations()

    add_bought_with_relations_for_product(t_shirt.pk)

    # T-shirt should be related to hoodie parent product
    assert ProductCrossSell.objects.count() == 1
    t_shirt_relation = ProductCrossSell.objects.filter(product2=hoodie).first()
    assert t_shirt_relation
    assert t_shirt_relation.product1 == t_shirt

    add_bought_with_relations_for_product(hoodie.pk)
    assert ProductCrossSell.objects.count() == 2

    # Hoodie should be related to t-shirt product
    hoodie_relation = ProductCrossSell.objects.filter(product2=t_shirt).first()
    assert hoodie_relation
    assert hoodie_relation.product1 == hoodie
def test_product_relations_variation_products(rf):
    t_shirt, hoodie = _init_test_with_variations()

    add_bought_with_relations_for_product(t_shirt.pk)

    # T-shirt should be related to hoodie parent product
    assert ProductCrossSell.objects.count() == 1
    t_shirt_relation = ProductCrossSell.objects.filter(product2=hoodie).first()
    assert t_shirt_relation
    assert t_shirt_relation.product1 == t_shirt

    add_bought_with_relations_for_product(hoodie.pk)
    assert ProductCrossSell.objects.count() == 2

    # Hoodie should be related to t-shirt parent product
    hoodie_relation = ProductCrossSell.objects.filter(product2=t_shirt).first()
    assert hoodie_relation
    assert hoodie_relation.product1 == hoodie
def test_product_relations_max_quantity(rf):
    shop = get_default_shop()
    supplier = get_default_supplier()
    product = create_product("simple-test-product", shop)
    quantities = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
    for i, quantity in enumerate(quantities):
        order = create_order_with_product(product, supplier, quantity=1, taxless_base_unit_price=6, shop=shop)
        add_product_to_order(
            order,
            supplier,
            create_product("product-%s" % i, shop),
            quantity=quantity,
            taxless_base_unit_price=6
        )

    assert ProductCrossSell.objects.count() == 0
    add_bought_with_relations_for_product(product.pk, max_quantity=5)
    assert ProductCrossSell.objects.count() == 5
    # Test that ordering is ok
    assert not ProductCrossSell.objects.filter(weight=1).exists()
    assert ProductCrossSell.objects.filter(weight=11).exists()
示例#9
0
    def handle(self, *args, **options):
        # Clear all existing ProductCrossSell objects
        ProductCrossSell.objects.filter(type=ProductCrossSellType.BOUGHT_WITH).delete()

        queryset = OrderLine.objects.filter(type=OrderLineType.PRODUCT)
        seen_product_ids = set()

        # Handle all ordered normal products
        ordered_normal_prduct_ids = set(
            queryset.filter(product__variation_parent__isnull=True).values_list("product_id", flat=True))

        # Handle all ordered variation parents
        ordered_variation_parent_ids = set(
            queryset.values_list("product__variation_parent_id", flat=True))

        for product_id in set(ordered_normal_prduct_ids).union(set(ordered_variation_parent_ids)):
            if product_id in seen_product_ids:
                continue

            seen_product_ids.add(product_id)
            add_bought_with_relations_for_product(product_id)
def test_computing_simple_product_relations(rf):
    shop = get_default_shop()
    supplier = get_default_supplier()
    product = create_product("simple-test-product", shop)
    related_product = create_product("simple-related-product", shop)
    quantities = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
    for quantity in quantities:
        order = create_order_with_product(product, supplier, quantity=1, taxless_base_unit_price=6, shop=shop)
        add_product_to_order(order, supplier, related_product, quantity=quantity, taxless_base_unit_price=6)

    assert ProductCrossSell.objects.count() == 0
    add_bought_with_relations_for_product(product.pk)
    assert ProductCrossSell.objects.count() == 1
    cross_sell_product = ProductCrossSell.objects.filter(product1=product).first()
    assert cross_sell_product.product2 == related_product
    assert cross_sell_product.weight == sum(quantities)

    add_bought_with_relations_for_product(related_product.id)
    assert ProductCrossSell.objects.count() == 2
    cross_sell_product = ProductCrossSell.objects.filter(product1=related_product).first()
    assert cross_sell_product.product2 == product
    assert cross_sell_product.weight == len(quantities)