forked from CGM_Public/pretix_original
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 import forms
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
from pretix.base.forms import I18nModelForm
|
from pretix.base.forms import I18nModelForm
|
||||||
from pretix.base.models import Item, ItemVariation, Quota, Voucher
|
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)
|
self.instance.item = Item.objects.get(pk=itemid, event=self.instance.event)
|
||||||
if varid:
|
if varid:
|
||||||
self.instance.variation = ItemVariation.objects.get(pk=varid, item=self.instance.item)
|
self.instance.variation = ItemVariation.objects.get(pk=varid, item=self.instance.item)
|
||||||
|
avail = self.instance.variation.check_quotas()
|
||||||
else:
|
else:
|
||||||
self.instance.variation = None
|
self.instance.variation = None
|
||||||
|
avail = self.instance.item.check_quotas()
|
||||||
self.instance.quota = None
|
self.instance.quota = None
|
||||||
else:
|
else:
|
||||||
self.instance.quota = Quota.objects.get(pk=quotaid, event=self.instance.event)
|
self.instance.quota = Quota.objects.get(pk=quotaid, event=self.instance.event)
|
||||||
self.instance.item = None
|
self.instance.item = None
|
||||||
self.instance.variation = 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():
|
if 'codes' in data:
|
||||||
raise ValidationError(_('A voucher with this code already exists.'))
|
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
|
return data
|
||||||
|
|
||||||
@@ -103,7 +116,6 @@ class VoucherBulkForm(VoucherForm):
|
|||||||
|
|
||||||
def clean(self):
|
def clean(self):
|
||||||
data = super().clean()
|
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():
|
if Voucher.objects.filter(code__in=data['codes'], event=self.instance.event).exists():
|
||||||
raise ValidationError(_('A voucher with one of this codes already 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)
|
form.instance.log_action('pretix.voucher.added', data=dict(form.cleaned_data), user=self.request.user)
|
||||||
return ret
|
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):
|
class VoucherBulkCreate(EventPermissionRequiredMixin, CreateView):
|
||||||
model = Voucher
|
model = Voucher
|
||||||
@@ -208,3 +213,8 @@ class VoucherBulkCreate(EventPermissionRequiredMixin, CreateView):
|
|||||||
ctx = super().get_context_data(**kwargs)
|
ctx = super().get_context_data(**kwargs)
|
||||||
ctx['code_length'] = settings.ENTROPY['voucher_code']
|
ctx['code_length'] = settings.ENTROPY['voucher_code']
|
||||||
return ctx
|
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