mirror of
https://github.com/pretix/pretix.git
synced 2026-05-05 15:14:04 +00:00
Refs #105 -- Check quota before blocking it with a voucher
This commit is contained in:
@@ -3,7 +3,6 @@ import copy
|
||||
from django import forms
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from pretix.base.forms import I18nModelForm
|
||||
from pretix.base.models import Item, ItemVariation, Quota, Voucher
|
||||
|
||||
@@ -65,16 +64,30 @@ class VoucherForm(I18nModelForm):
|
||||
self.instance.item = Item.objects.get(pk=itemid, event=self.instance.event)
|
||||
if varid:
|
||||
self.instance.variation = ItemVariation.objects.get(pk=varid, item=self.instance.item)
|
||||
avail = self.instance.variation.check_quotas()
|
||||
else:
|
||||
self.instance.variation = None
|
||||
avail = self.instance.item.check_quotas()
|
||||
self.instance.quota = None
|
||||
else:
|
||||
self.instance.quota = Quota.objects.get(pk=quotaid, event=self.instance.event)
|
||||
self.instance.item = None
|
||||
self.instance.variation = None
|
||||
avail = self.instance.quota.availability()
|
||||
|
||||
if 'code' in data and not self.instance.pk and Voucher.objects.filter(code=data['code'], event=self.instance.event).exists():
|
||||
raise ValidationError(_('A voucher with this code already exists.'))
|
||||
if 'codes' in data:
|
||||
data['codes'] = [a.strip() for a in data.get('codes', '').strip().split("\n") if a]
|
||||
cnt = len(data['codes'])
|
||||
else:
|
||||
cnt = 1
|
||||
|
||||
if data.get('block_quota', False):
|
||||
if avail[0] != Quota.AVAILABILITY_OK or (avail[1] is not None and avail[1] < cnt):
|
||||
raise ValidationError(_('You cannot create a voucher that blocks quota as the selected product or quota is '
|
||||
'currently sold out or completely reserved.'))
|
||||
|
||||
if 'code' in data and not self.instance.pk and Voucher.objects.filter(code=data['code'], event=self.instance.event).exists():
|
||||
raise ValidationError(_('A voucher with this code already exists.'))
|
||||
|
||||
return data
|
||||
|
||||
@@ -103,7 +116,6 @@ class VoucherBulkForm(VoucherForm):
|
||||
|
||||
def clean(self):
|
||||
data = super().clean()
|
||||
data['codes'] = [a.strip() for a in data.get('codes', '').strip().split("\n") if a]
|
||||
|
||||
if Voucher.objects.filter(code__in=data['codes'], event=self.instance.event).exists():
|
||||
raise ValidationError(_('A voucher with one of this codes already exists.'))
|
||||
|
||||
@@ -172,6 +172,11 @@ class VoucherCreate(EventPermissionRequiredMixin, CreateView):
|
||||
form.instance.log_action('pretix.voucher.added', data=dict(form.cleaned_data), user=self.request.user)
|
||||
return ret
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
# TODO: Transform this into an asynchronous call?
|
||||
with request.event.lock():
|
||||
return super().post(request, *args, **kwargs)
|
||||
|
||||
|
||||
class VoucherBulkCreate(EventPermissionRequiredMixin, CreateView):
|
||||
model = Voucher
|
||||
@@ -208,3 +213,8 @@ class VoucherBulkCreate(EventPermissionRequiredMixin, CreateView):
|
||||
ctx = super().get_context_data(**kwargs)
|
||||
ctx['code_length'] = settings.ENTROPY['voucher_code']
|
||||
return ctx
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
# TODO: Transform this into an asynchronous call?
|
||||
with request.event.lock():
|
||||
return super().post(request, *args, **kwargs)
|
||||
|
||||
Reference in New Issue
Block a user