Skip to content

Commit

Permalink
Allow to create blocking vouchers for items with unspecified variation (
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelm committed Feb 29, 2024
1 parent 5deb1a8 commit 6bf23b0
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 17 deletions.
25 changes: 18 additions & 7 deletions src/pretix/base/models/vouchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,6 @@ def clean_item_properties(data, event, quota, item, variation, block_quota=False
'variations.'))
if variation and not item.variations.filter(pk=variation.pk).exists():
raise ValidationError(_('This variation does not belong to this product.'))
if item.has_variations and not variation and data.get('block_quota'):
raise ValidationError(_('You can only block quota if you specify a specific product variation. '
'Otherwise it might be unclear which quotas to block.'))
if item.category and item.category.is_addon:
raise ValidationError(_('It is currently not possible to create vouchers for add-on products.'))
elif block_quota:
Expand Down Expand Up @@ -431,7 +428,15 @@ def clean_quota_get_ignored(old_instance):
elif old_instance.variation:
quotas |= set(old_instance.variation.quotas.filter(subevent=old_instance.subevent))
elif old_instance.item:
quotas |= set(old_instance.item.quotas.filter(subevent=old_instance.subevent))
if old_instance.item.has_variations:
quotas |= set(
Quota.objects.filter(pk__in=Quota.variations.through.objects.filter(
itemvariation__item=old_instance.item,
quota__subevent=old_instance.subevent,
).values('quota_id'))
)
else:
quotas |= set(old_instance.item.quotas.filter(subevent=old_instance.subevent))
return quotas

@staticmethod
Expand All @@ -446,13 +451,19 @@ def clean_quota_check(data, cnt, old_instance, event, quota, item, variation):

if quota:
new_quotas = {quota}
elif item and item.has_variations and not variation:
raise ValidationError(_('You can only block quota if you specify a specific product variation. '
'Otherwise it might be unclear which quotas to block.'))
elif item and variation:
new_quotas = set(variation.quotas.filter(subevent=data.get('subevent')))
elif item and not item.has_variations:
new_quotas = set(item.quotas.filter(subevent=data.get('subevent')))
elif item and item.has_variations:
new_quotas = set(
Quota.objects.filter(
pk__in=Quota.variations.through.objects.filter(
itemvariation__item=old_instance.item,
quota__subevent=data.get('subevent'),
).values('quota_id')
)
)
else:
raise ValidationError(_('You need to select a specific product or quota if this voucher should reserve '
'tickets.'))
Expand Down
18 changes: 12 additions & 6 deletions src/pretix/base/services/quotas.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ def __init__(self, count_waitinglist=True, ignore_closed=False, full_results=Fal
self._count_waitinglist = count_waitinglist
self._ignore_closed = ignore_closed
self._full_results = full_results
self._item_to_quotas = defaultdict(list)
self._var_to_quotas = defaultdict(list)
self._item_to_quotas = defaultdict(set)
self._var_to_quotas = defaultdict(set)
self._early_out = early_out
self._quota_objects = {}
self.results = {}
Expand Down Expand Up @@ -243,13 +243,16 @@ def _compute(self, quotas, now_dt):
quota_id__in=[q.pk for q in quotas]
).values('quota_id', 'item_id')
for m in q_items:
self._item_to_quotas[m['item_id']].append(self._quota_objects[m['quota_id']])
self._item_to_quotas[m['item_id']].add(self._quota_objects[m['quota_id']])

q_vars = Quota.variations.through.objects.filter(
quota_id__in=[q.pk for q in quotas]
).values('quota_id', 'itemvariation_id')
).values('quota_id', 'itemvariation_id', 'itemvariation__item_id')
for m in q_vars:
self._var_to_quotas[m['itemvariation_id']].append(self._quota_objects[m['quota_id']])
self._var_to_quotas[m['itemvariation_id']].add(self._quota_objects[m['quota_id']])
# We can't be 100% certain that a quota, when it is connected to a variation, is also always connected to
# the parent item, so we double-check here just to be sure.
self._item_to_quotas[m['itemvariation__item_id']].add(self._quota_objects[m['quota_id']])

self._compute_orders(quotas, q_items, q_vars, size_left)

Expand Down Expand Up @@ -378,7 +381,10 @@ def _compute_vouchers(self, quotas, q_items, q_vars, size_left, now_dt):
Q(
Q(
Q(variation_id__isnull=True) &
Q(item_id__in={i['item_id'] for i in q_items if i['quota_id'] in quota_ids})
Q(item_id__in=(
{i['item_id'] for i in q_items if i['quota_id'] in quota_ids} |
{i['itemvariation__item_id'] for i in q_vars if i['quota_id'] in quota_ids}
))
) | Q(
variation_id__in={i['itemvariation_id'] for i in q_vars if i['quota_id'] in quota_ids}
) | Q(
Expand Down
28 changes: 25 additions & 3 deletions src/tests/base/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,29 @@ def test_voucher_variation(self):
v.save()
self.assertEqual(self.var1.check_quotas(), (Quota.AVAILABILITY_ORDERED, 0))

@classscope(attr='o')
def test_voucher_all_variations(self):
self.quota.variations.add(self.var1)
self.quota.size = 1
self.quota.save()

self.quota2 = Quota.objects.create(name="Test", size=2, event=self.event)
self.quota2.variations.add(self.var2)

self.quota3 = Quota.objects.create(name="Test", size=2, event=self.event)
self.quota3.variations.add(self.var3)

v = Voucher.objects.create(item=self.item2, event=self.event)
self.assertEqual(self.var1.check_quotas(), (Quota.AVAILABILITY_OK, 1))
self.assertEqual(self.var2.check_quotas(), (Quota.AVAILABILITY_OK, 2))
self.assertEqual(self.var3.check_quotas(), (Quota.AVAILABILITY_OK, 2))

v.block_quota = True
v.save()
self.assertEqual(self.var1.check_quotas(), (Quota.AVAILABILITY_ORDERED, 0))
self.assertEqual(self.var2.check_quotas(), (Quota.AVAILABILITY_OK, 1))
self.assertEqual(self.var3.check_quotas(), (Quota.AVAILABILITY_OK, 2))

@classscope(attr='o')
def test_voucher_quota(self):
self.quota.variations.add(self.var1)
Expand Down Expand Up @@ -979,9 +1002,8 @@ def test_voucher_item_does_not_match_variation(self):

@classscope(attr='o')
def test_voucher_specify_variation_for_block_quota(self):
with self.assertRaises(ValidationError):
v = Voucher(item=self.item2, block_quota=True, event=self.event)
v.clean()
v = Voucher(item=self.item2, block_quota=True, event=self.event)
v.clean()

@classscope(attr='o')
def test_voucher_no_item_but_variation(self):
Expand Down
2 changes: 1 addition & 1 deletion src/tests/control/test_vouchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def test_create_blocking_item_voucher_quota_full(self):
self._create_voucher({
'itemvar': '%d' % self.shirt.pk,
'block_quota': 'on'
}, expected_failure=True)
})

def test_create_blocking_item_voucher_quota_full_invalid(self):
self.quota_shirts.size = 0
Expand Down

0 comments on commit 6bf23b0

Please sign in to comment.