Seat-specific vouchers (#1486)

* Basic functionality

* API

* Do not delete seats with vouchers

* Show seat in list of seats

* Validate availability of seats

* Fix invalid logic in Seat.is_available

* Show voucher name in edit form
This commit is contained in:
Raphael Michel
2019-11-15 10:56:34 +01:00
committed by GitHub
parent f79df47b78
commit a2c1c69d7e
19 changed files with 474 additions and 42 deletions

View File

@@ -11,7 +11,7 @@ from django.utils.translation import pgettext_lazy, ugettext_lazy as _
from django_scopes import ScopedManager, scopes_disabled
from pretix.base.banlist import banned
from pretix.base.models import SeatCategoryMapping
from pretix.base.models import Seat, SeatCategoryMapping
from ..decimal import round_decimal
from .base import LoggedModel
@@ -171,6 +171,12 @@ class Voucher(LoggedModel):
"If enabled, the voucher is valid for any product affected by this quota."
)
)
seat = models.ForeignKey(
Seat, related_name='vouchers',
null=True, blank=True,
on_delete=models.PROTECT,
verbose_name=_("Specific seat"),
)
tag = models.CharField(
max_length=255,
verbose_name=_("Tag"),
@@ -335,6 +341,38 @@ class Voucher(LoggedModel):
if 'code' in data and Voucher.objects.filter(Q(code__iexact=data['code']) & Q(event=event) & ~Q(pk=pk)).exists():
raise ValidationError(_('A voucher with this code already exists.'))
@staticmethod
def clean_seat_id(data, item, event, pk):
try:
if event.has_subevents:
if not data.get('subevent'):
raise ValidationError(_('You need to choose a date if you select a seat.'))
seat = event.seats.get(seat_guid=data.get('seat'), subevent=data.get('subevent'))
else:
seat = event.seats.get(seat_guid=data.get('seat'))
except Seat.DoesNotExist:
raise ValidationError(_('The specified seat ID "{id}" does not exist for this event.').format(
id=data.get('seat')))
if not seat.is_available(ignore_voucher_id=pk, ignore_cart=True):
raise ValidationError(_('The seat "{id}" is currently unavailable (blocked, already sold or a '
'different voucher).').format(
id=seat.seat_guid))
if not item:
raise ValidationError(_('You need to choose a specific product if you select a seat.'))
if data.get('max_usages', 1) > 1:
raise ValidationError(_('Seat-specific vouchers can only be used once.'))
if seat.product != item:
raise ValidationError(_('You need to choose the product "{prod}" for this seat.').format(prod=seat.product))
if not seat.is_available(ignore_voucher_id=pk):
raise ValidationError(_('The seat "{id}" is already sold or currently blocked.').format(id=seat.seat_guid))
return seat
def save(self, *args, **kwargs):
self.code = self.code.upper()
super().save(*args, **kwargs)