def test_filter_caching(rf): request, shop, group = initialize_test(rf, False) price = shop.create_price product_price = "100" discount_percentage = "0.30" supplier = get_default_supplier() product = create_product(printable_gibberish(), shop=shop, supplier=supplier, default_price=product_price) product2 = create_product(printable_gibberish(), shop=shop, supplier=supplier, default_price=product_price) assert product.pk != product2.pk # ensure they're different # create catalog campaign catalog_filter = ProductFilter.objects.create() catalog_filter.products.add(product) catalog_campaign = CatalogCampaign.objects.create(shop=shop, active=True, name="test") catalog_campaign.filters.add(catalog_filter) assert CatalogFilterCachedShopProduct.objects.count() == 1 catalog_campaign.save() assert CatalogFilterCachedShopProduct.objects.count() == 1 entry = CatalogFilterCachedShopProduct.objects.first() assert entry.pk == get_matching_catalog_filters(product.get_shop_instance(shop))[0] # create another campaign catalog_filter2 = ProductFilter.objects.create() catalog_filter2.products.add(product2) catalog_campaign2 = CatalogCampaign.objects.create(shop=shop, active=True, name="test") catalog_campaign2.filters.add(catalog_filter2) assert CatalogFilterCachedShopProduct.objects.count() == 2 catalog_campaign2.save() assert CatalogFilterCachedShopProduct.objects.count() == 2 # new cache for this product was created entry = CatalogFilterCachedShopProduct.objects.last() assert entry.pk == get_matching_catalog_filters(product2.get_shop_instance(shop))[0] # third campaign catalog_filter3 = ProductFilter.objects.create() catalog_filter3.products.add(product2) catalog_campaign3 = CatalogCampaign.objects.create(shop=shop, active=True, name="test") catalog_campaign3.filters.add(catalog_filter3) assert CatalogFilterCachedShopProduct.objects.count() == 3 catalog_campaign3.save() assert CatalogFilterCachedShopProduct.objects.count() == 3 # new one for this filter again expected = get_matching_catalog_filters(product2.get_shop_instance(shop)) for id in expected: assert id in [catalog_filter2.pk, catalog_filter3.pk]
def get_matching(cls, context, shop_product): prod_ctx_cache_elements = dict(customer=context.customer.pk or 0, shop=context.shop.pk, product_id=shop_product.pk) namespace = CAMPAIGNS_CACHE_NAMESPACE sorted_items = dict( sorted(prod_ctx_cache_elements.items(), key=lambda item: item[0])) key = "%s:%s" % (namespace, hashlib.sha1( str(sorted_items).encode("utf-8")).hexdigest()) cached_matching = cache.get(key, None) if cached_matching is not None: return cached_matching from shuup.campaigns.models.matching import get_matching_catalog_filters, get_matching_context_conditions matching_context_conditions = get_matching_context_conditions(context) matching_catalog_filters = get_matching_catalog_filters(shop_product) if not (matching_context_conditions or matching_catalog_filters): return [] # Get all possible campaign id's for matching context_conditions campaigns_based_on_conditions = set( cls.objects.filter( active=True, shop=context.shop, conditions__id__in=matching_context_conditions).values_list( "pk", flat=True)) campaigns_based_on_catalog_filters = set() if hasattr(cls, "filters"): # Get all possible campaigns for matching catalog_filters campaigns_based_on_catalog_filters = set( cls.objects.filter( active=True, shop=context.shop, filters__id__in=matching_catalog_filters).values_list( "pk", flat=True)) all_possible_campaigns_ids = campaigns_based_on_conditions | campaigns_based_on_catalog_filters matching = [] for campaign in cls.objects.filter(id__in=all_possible_campaigns_ids): if campaign.rules_match(context, shop_product, matching_catalog_filters, matching_context_conditions): matching.append(campaign) cache.set(key, matching, timeout=None) return matching
def get_matching(cls, context, shop_product): prod_ctx_cache_elements = dict( customer=context.customer.pk or 0, shop=context.shop.pk, product_id=shop_product.pk) namespace = CAMPAIGNS_CACHE_NAMESPACE key = "%s:%s" % (namespace, hash(frozenset(prod_ctx_cache_elements.items()))) cached_matching = cache.get(key, None) if cached_matching is not None: return cached_matching from shuup.campaigns.models.matching import get_matching_context_conditions, get_matching_catalog_filters matching_context_conditions = get_matching_context_conditions(context) matching_catalog_filters = get_matching_catalog_filters(shop_product) if not (matching_context_conditions or matching_catalog_filters): return [] # Get all possible campaign id's for matching context_conditions campaigns_based_on_conditions = set( cls.objects.filter( active=True, shop=context.shop, conditions__id__in=matching_context_conditions ).values_list("pk", flat=True) ) campaigns_based_on_catalog_filters = set() if hasattr(cls, "filters"): # Get all possible campaigns for matching catalog_filters campaigns_based_on_catalog_filters = set( cls.objects.filter( active=True, shop=context.shop, filters__id__in=matching_catalog_filters ).values_list("pk", flat=True) ) all_possible_campaigns_ids = (campaigns_based_on_conditions | campaigns_based_on_catalog_filters) matching = [] for campaign in cls.objects.filter(id__in=all_possible_campaigns_ids): if campaign.rules_match(context, shop_product, matching_catalog_filters, matching_context_conditions): matching.append(campaign) cache.set(key, matching, timeout=None) return matching
def test_filter_caching(rf): request, shop, group = initialize_test(rf, False) price = shop.create_price product_price = "100" discount_percentage = "0.30" supplier = get_default_supplier() product = create_product(printable_gibberish(), shop=shop, supplier=supplier, default_price=product_price) product2 = create_product(printable_gibberish(), shop=shop, supplier=supplier, default_price=product_price) assert product.pk != product2.pk # ensure they're different # create catalog campaign catalog_filter = ProductFilter.objects.create() catalog_filter.products.add(product) catalog_campaign = CatalogCampaign.objects.create(shop=shop, active=True, name="test") catalog_campaign.filters.add(catalog_filter) assert CatalogFilterCachedShopProduct.objects.count() == 1 catalog_campaign.save() assert CatalogFilterCachedShopProduct.objects.count() == 1 entry = CatalogFilterCachedShopProduct.objects.first() assert entry.pk == get_matching_catalog_filters( product.get_shop_instance(shop))[0] # create another campaign catalog_filter2 = ProductFilter.objects.create() catalog_filter2.products.add(product2) catalog_campaign2 = CatalogCampaign.objects.create(shop=shop, active=True, name="test") catalog_campaign2.filters.add(catalog_filter2) assert CatalogFilterCachedShopProduct.objects.count() == 2 catalog_campaign2.save() assert CatalogFilterCachedShopProduct.objects.count( ) == 2 # new cache for this product was created entry = CatalogFilterCachedShopProduct.objects.last() assert entry.pk == get_matching_catalog_filters( product2.get_shop_instance(shop))[0] # third campaign catalog_filter3 = ProductFilter.objects.create() catalog_filter3.products.add(product2) catalog_campaign3 = CatalogCampaign.objects.create(shop=shop, active=True, name="test") catalog_campaign3.filters.add(catalog_filter3) assert CatalogFilterCachedShopProduct.objects.count() == 3 catalog_campaign3.save() assert CatalogFilterCachedShopProduct.objects.count( ) == 3 # new one for this filter again expected = get_matching_catalog_filters(product2.get_shop_instance(shop)) for id in expected: assert id in [catalog_filter2.pk, catalog_filter3.pk]