Voucher creation: Auto-select products based on seat

This commit is contained in:
Raphael Michel
2019-11-22 13:15:33 +01:00
parent ee4f75c2fb
commit 84fb25e4d9
3 changed files with 29 additions and 14 deletions

View File

@@ -217,11 +217,12 @@ class Voucher(LoggedModel):
self.event,
self.quota,
self.item,
self.variation
self.variation,
seats_given=bool(self.seat)
)
@staticmethod
def clean_item_properties(data, event, quota, item, variation):
def clean_item_properties(data, event, quota, item, variation, seats_given=False):
if quota:
if quota.event != event:
raise ValidationError(_('You cannot select a quota that belongs to a different event.'))
@@ -240,7 +241,7 @@ class Voucher(LoggedModel):
'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.'))
else:
elif not seats_given:
raise ValidationError(_('You need to specify either a quota or a product.'))
@staticmethod
@@ -342,14 +343,18 @@ class Voucher(LoggedModel):
raise ValidationError(_('A voucher with this code already exists.'))
@staticmethod
def clean_seat_id(data, item, event, pk):
def clean_seat_id(data, item, quota, 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'))
seat = event.seats.select_related('product').get(
seat_guid=data.get('seat'), subevent=data.get('subevent')
)
else:
seat = event.seats.get(seat_guid=data.get('seat'))
seat = event.seats.select_related('product').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')))
@@ -359,13 +364,13 @@ class Voucher(LoggedModel):
'different voucher).').format(
id=seat.seat_guid))
if not item:
if quota:
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:
if item and 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):