Allow to filter items with query parameters on event page

This commit is contained in:
Raphael Michel
2020-08-21 15:18:37 +02:00
parent 6759506838
commit 41dd71879e
2 changed files with 23 additions and 3 deletions

View File

@@ -59,7 +59,7 @@ def item_group_by_category(items):
def get_grouped_items(event, subevent=None, voucher=None, channel='web', require_seat=0, base_qs=None, allow_addons=False, def get_grouped_items(event, subevent=None, voucher=None, channel='web', require_seat=0, base_qs=None, allow_addons=False,
quota_cache=None): quota_cache=None, filter_items=None, filter_categories=None):
base_qs = base_qs if base_qs is not None else event.items base_qs = base_qs if base_qs is not None else event.items
items = base_qs.using(settings.DATABASE_REPLICA).filter_available(channel=channel, voucher=voucher, allow_addons=allow_addons).select_related( items = base_qs.using(settings.DATABASE_REPLICA).filter_available(channel=channel, voucher=voucher, allow_addons=allow_addons).select_related(
'category', 'tax_rule', # for re-grouping 'category', 'tax_rule', # for re-grouping
@@ -124,6 +124,12 @@ def get_grouped_items(event, subevent=None, voucher=None, channel='web', require
items = items.filter(requires_seat__gt=0) items = items.filter(requires_seat__gt=0)
else: else:
items = items.filter(requires_seat=0) items = items.filter(requires_seat=0)
if filter_items:
items = items.filter(pk__in=[a for a in filter_items if a.isdigit()])
if filter_categories:
items = items.filter(category_id__in=[a for a in filter_categories if a.isdigit()])
display_add_to_cart = False display_add_to_cart = False
external_quota_cache = quota_cache or event.cache.get('item_quota_cache') external_quota_cache = quota_cache or event.cache.get('item_quota_cache')
quota_cache = external_quota_cache or {} quota_cache = external_quota_cache or {}
@@ -359,8 +365,12 @@ class EventIndex(EventViewMixin, EventListMixin, CartMixin, TemplateView):
if not self.request.event.has_subevents or self.subevent: if not self.request.event.has_subevents or self.subevent:
# Fetch all items # Fetch all items
items, display_add_to_cart = get_grouped_items(self.request.event, self.subevent, items, display_add_to_cart = get_grouped_items(
channel=self.request.sales_channel.identifier) self.request.event, self.subevent,
filter_items=self.request.GET.getlist('item'),
filter_categories=self.request.GET.getlist('category'),
channel=self.request.sales_channel.identifier
)
context['itemnum'] = len(items) context['itemnum'] = len(items)
context['allfree'] = all( context['allfree'] = all(
item.display_price.gross == Decimal('0.00') for item in items if not item.has_variations item.display_price.gross == Decimal('0.00') for item in items if not item.has_variations

View File

@@ -93,6 +93,16 @@ class ItemDisplayTest(EventTestMixin, SoupTest):
self.assertNotIn("Early-bird", html) self.assertNotIn("Early-bird", html)
self.assertNotIn("btn-add-to-cart", html) self.assertNotIn("btn-add-to-cart", html)
def test_filter_by_url(self):
with scopes_disabled():
q = Quota.objects.create(event=self.event, name='Quota', size=2)
item = Item.objects.create(event=self.event, name='Early-bird ticket', default_price=0, active=True)
q.items.add(item)
doc = self.get_doc('/%s/%s/?item=%d' % (self.orga.slug, self.event.slug, item.pk))
self.assertIn("Early-bird", doc.select("section .product-row")[0].text)
doc = self.get_doc('/%s/%s/?item=%d' % (self.orga.slug, self.event.slug, item.pk + 1))
assert not doc.select("section .product-row")
def test_without_category(self): def test_without_category(self):
with scopes_disabled(): with scopes_disabled():
q = Quota.objects.create(event=self.event, name='Quota', size=2) q = Quota.objects.create(event=self.event, name='Quota', size=2)