mirror of
https://github.com/pretix/pretix.git
synced 2026-06-12 01:35:16 +00:00
Refactor: Deduplicate get_affected_quotas code
This commit is contained in:
@@ -420,28 +420,35 @@ class Voucher(LoggedModel):
|
|||||||
|
|
||||||
return False
|
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
|
@staticmethod
|
||||||
def clean_quota_get_ignored(old_instance):
|
def clean_quota_get_ignored(old_instance):
|
||||||
quotas = set()
|
|
||||||
was_valid = old_instance and (
|
was_valid = old_instance and (
|
||||||
old_instance.valid_until is None or old_instance.valid_until >= now()
|
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 and old_instance.block_quota and was_valid:
|
||||||
if old_instance.quota:
|
return Voucher.get_affected_quotas(old_instance.quota, old_instance.item, old_instance.variation, old_instance.subevent)
|
||||||
quotas.add(old_instance.quota)
|
else:
|
||||||
elif old_instance.variation:
|
return set()
|
||||||
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
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def clean_quota_check(data, cnt, old_instance, event, quota, item, variation):
|
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'):
|
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.'))
|
raise ValidationError(_('If you want this voucher to block quota, you need to select a specific date.'))
|
||||||
|
|
||||||
if quota:
|
new_quotas = Voucher.get_affected_quotas(quota, item, variation, data.get('subevent'))
|
||||||
new_quotas = {quota}
|
if not new_quotas:
|
||||||
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:
|
|
||||||
raise ValidationError(_('You need to select a specific product or quota if this voucher should reserve '
|
raise ValidationError(_('You need to select a specific product or quota if this voucher should reserve '
|
||||||
'tickets.'))
|
'tickets.'))
|
||||||
|
|
||||||
|
|||||||
@@ -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])}
|
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:
|
for current in current_vouchers:
|
||||||
was_valid = current["valid_until"] is None or current["valid_until"] >= now()
|
|
||||||
|
|
||||||
# Get quotas that are currently used
|
# Get quotas that are currently used
|
||||||
if current["item"]:
|
if current["item"]:
|
||||||
current["item"] = item_cache[current["item"]]
|
current["item"] = item_cache[current["item"]]
|
||||||
@@ -334,22 +332,11 @@ class VoucherBulkEditForm(VoucherForm):
|
|||||||
if current["subevent"]:
|
if current["subevent"]:
|
||||||
current["subevent"] = subevent_cache[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 was_valid and current["block_quota"] and current["max_usages"] > current["redeemed"]:
|
||||||
if current["quota"]:
|
old_quotas = Voucher.get_affected_quotas(current["quota"], current["item"], current["variation"], current["subevent"])
|
||||||
old_quotas.add(current["quota"])
|
else:
|
||||||
elif current["variation"]:
|
old_quotas = set()
|
||||||
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_amount = max(current["max_usages"] - current["redeemed"], 0) * current["c"]
|
old_amount = max(current["max_usages"] - current["redeemed"], 0) * current["c"]
|
||||||
|
|
||||||
# Predict state after change
|
# Predict state after change
|
||||||
@@ -390,22 +377,10 @@ class VoucherBulkEditForm(VoucherForm):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
will_be_valid = after_change["valid_until"] is None or after_change["valid_until"] >= now()
|
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 will_be_valid and after_change["block_quota"] and after_change["max_usages"] > current["redeemed"]:
|
||||||
if after_change["quota"]:
|
new_quotas = Voucher.get_affected_quotas(after_change["quota"], after_change["item"], after_change["variation"], after_change["subevent"])
|
||||||
new_quotas.add(after_change["quota"])
|
else:
|
||||||
elif after_change["variation"]:
|
new_quotas = set()
|
||||||
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_amount = max(after_change["max_usages"] - after_change["redeemed"], 0) * current["c"]
|
new_amount = max(after_change["max_usages"] - after_change["redeemed"], 0) * current["c"]
|
||||||
if new_quotas != old_quotas or new_amount != old_amount:
|
if new_quotas != old_quotas or new_amount != old_amount:
|
||||||
|
|||||||
Reference in New Issue
Block a user