mirror of
https://github.com/pretix/pretix.git
synced 2026-05-05 15:14:04 +00:00
Allow for vouchers that are valid for multiple items
This commit is contained in:
@@ -513,10 +513,9 @@ class Quota(LoggedModel):
|
||||
def count_blocking_vouchers(self) -> int:
|
||||
from pretix.base.models import Voucher
|
||||
return Voucher.objects.filter(
|
||||
Q(item__quotas__in=[self]) &
|
||||
Q(block_quota=True) &
|
||||
Q(redeemed=False) &
|
||||
self._position_lookup
|
||||
Q(Q(self._position_lookup) | Q(quota=self))
|
||||
).distinct().count()
|
||||
|
||||
def count_in_cart(self) -> int:
|
||||
@@ -546,8 +545,8 @@ class Quota(LoggedModel):
|
||||
def _position_lookup(self) -> Q:
|
||||
return (
|
||||
( # Orders for items which do not have any variations
|
||||
Q(variation__isnull=True)
|
||||
& Q(item__quotas__in=[self])
|
||||
Q(variation__isnull=True) &
|
||||
Q(item__quotas__in=[self])
|
||||
) | ( # Orders for items which do have any variations
|
||||
Q(variation__quotas__in=[self])
|
||||
)
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
import random
|
||||
from decimal import Decimal
|
||||
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db import models
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from .base import LoggedModel
|
||||
from .event import Event
|
||||
from .items import Item, ItemVariation
|
||||
from .items import Item, ItemVariation, Quota
|
||||
from .orders import CartPosition, OrderPosition
|
||||
|
||||
|
||||
@@ -59,6 +61,7 @@ class Voucher(LoggedModel):
|
||||
item = models.ForeignKey(
|
||||
Item, related_name='vouchers',
|
||||
verbose_name=_("Product"),
|
||||
null=True, blank=True,
|
||||
help_text=_(
|
||||
"This product is added to the user's cart if the voucher is redeemed."
|
||||
)
|
||||
@@ -71,6 +74,14 @@ class Voucher(LoggedModel):
|
||||
"This variation of the product select above is being used."
|
||||
)
|
||||
)
|
||||
quota = models.ForeignKey(
|
||||
Quota, related_name='quota',
|
||||
null=True, blank=True,
|
||||
verbose_name=_("Quota"),
|
||||
help_text=_(
|
||||
"If enabled, the voucher is valid for any product affected by this quota."
|
||||
)
|
||||
)
|
||||
|
||||
class Meta:
|
||||
verbose_name = _("Voucher")
|
||||
@@ -80,6 +91,21 @@ class Voucher(LoggedModel):
|
||||
def __str__(self):
|
||||
return self.code
|
||||
|
||||
def clean(self):
|
||||
super().clean()
|
||||
if self.quota:
|
||||
if self.item:
|
||||
raise ValidationError(_('You cannot select a quota and a specific product at the same time.'))
|
||||
elif self.item:
|
||||
if self.variation and (not self.item or not self.item.has_variations):
|
||||
raise ValidationError(_('You cannot select a variation without having selected a product that provides '
|
||||
'variations.'))
|
||||
if self.item.has_variations and not self.variation and self.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.'))
|
||||
else:
|
||||
raise ValidationError(_('You need to specify either a quota or a product.'))
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
self.code = self.code.upper()
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
Reference in New Issue
Block a user