Refactor: Deduplicate get_affected_quotas code

This commit is contained in:
Mira Weller
2026-05-28 13:26:34 +02:00
parent 912af33874
commit 777f0e0aca
2 changed files with 32 additions and 64 deletions

View File

@@ -420,28 +420,35 @@ class Voucher(LoggedModel):
return False
@staticmethod
def get_affected_quotas(quota, item, variation, subevent):
if quota:
return {quota}
elif item and variation:
return set(variation.quotas.filter(subevent=subevent))
elif item and not item.has_variations:
return set(item.quotas.filter(subevent=subevent))
elif item and item.has_variations:
return set(
Quota.objects.filter(
pk__in=Quota.variations.through.objects.filter(
itemvariation__item=item,
quota__subevent=subevent,
).values('quota_id')
)
)
else:
return set()
@staticmethod
def clean_quota_get_ignored(old_instance):
quotas = set()
was_valid = old_instance and (
old_instance.valid_until is None or old_instance.valid_until >= now()
)
if old_instance and old_instance.block_quota and was_valid:
if old_instance.quota:
quotas.add(old_instance.quota)
elif old_instance.variation:
quotas |= set(old_instance.variation.quotas.filter(subevent=old_instance.subevent))
elif old_instance.item:
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
return Voucher.get_affected_quotas(old_instance.quota, old_instance.item, old_instance.variation, old_instance.subevent)
else:
return set()
@staticmethod
def clean_quota_check(data, cnt, old_instance, event, quota, item, variation):
@@ -453,22 +460,8 @@ class Voucher(LoggedModel):
if event.has_subevents and data.get('block_quota') and not data.get('subevent'):
raise ValidationError(_('If you want this voucher to block quota, you need to select a specific date.'))
if quota:
new_quotas = {quota}
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=item,
quota__subevent=data.get('subevent'),
).values('quota_id')
)
)
else:
new_quotas = Voucher.get_affected_quotas(quota, item, variation, data.get('subevent'))
if not new_quotas:
raise ValidationError(_('You need to select a specific product or quota if this voucher should reserve '
'tickets.'))

View File

@@ -322,8 +322,6 @@ class VoucherBulkEditForm(VoucherForm):
subevent_cache = {s.pk: s for s in SubEvent.objects.filter(pk__in=[c["subevent"] for c in current_vouchers])}
for current in current_vouchers:
was_valid = current["valid_until"] is None or current["valid_until"] >= now()
# Get quotas that are currently used
if current["item"]:
current["item"] = item_cache[current["item"]]
@@ -334,22 +332,11 @@ class VoucherBulkEditForm(VoucherForm):
if current["subevent"]:
current["subevent"] = subevent_cache[current["subevent"]]
old_quotas = set()
was_valid = current["valid_until"] is None or current["valid_until"] >= now()
if was_valid and current["block_quota"] and current["max_usages"] > current["redeemed"]:
if current["quota"]:
old_quotas.add(current["quota"])
elif current["variation"]:
old_quotas |= set(current["variation"].quotas.filter(subevent=current["subevent"]))
elif current["item"]:
if current["item"].has_variations:
old_quotas |= set(
Quota.objects.filter(pk__in=Quota.variations.through.objects.filter(
itemvariation__item=current["item"],
quota__subevent=current["subevent"],
).values('quota_id'))
)
else:
old_quotas |= set(current["item"].quotas.filter(subevent=current["subevent"]))
old_quotas = Voucher.get_affected_quotas(current["quota"], current["item"], current["variation"], current["subevent"])
else:
old_quotas = set()
old_amount = max(current["max_usages"] - current["redeemed"], 0) * current["c"]
# Predict state after change
@@ -390,22 +377,10 @@ class VoucherBulkEditForm(VoucherForm):
continue
will_be_valid = after_change["valid_until"] is None or after_change["valid_until"] >= now()
new_quotas = set()
if will_be_valid and after_change["block_quota"] and after_change["max_usages"] > current["redeemed"]:
if after_change["quota"]:
new_quotas.add(after_change["quota"])
elif after_change["variation"]:
new_quotas |= set(after_change["variation"].quotas.filter(subevent=after_change["subevent"]))
elif after_change["item"]:
if after_change["item"].has_variations:
new_quotas |= set(
Quota.objects.filter(pk__in=Quota.variations.through.objects.filter(
itemvariation__item=after_change["item"],
quota__subevent=after_change["subevent"],
).values('quota_id'))
)
else:
new_quotas |= set(after_change["item"].quotas.filter(subevent=after_change["subevent"]))
new_quotas = Voucher.get_affected_quotas(after_change["quota"], after_change["item"], after_change["variation"], after_change["subevent"])
else:
new_quotas = set()
new_amount = max(after_change["max_usages"] - after_change["redeemed"], 0) * current["c"]
if new_quotas != old_quotas or new_amount != old_amount: