mirror of
https://github.com/pretix/pretix.git
synced 2026-05-06 15:24:02 +00:00
77 lines
2.8 KiB
Python
77 lines
2.8 KiB
Python
from django import forms
|
||
from django.utils.translation import ugettext_lazy as _
|
||
|
||
from pretix.base.forms import I18nModelForm
|
||
from pretix.base.models import Item, ItemVariation, Quota, Voucher
|
||
|
||
|
||
class VoucherForm(I18nModelForm):
|
||
itemvar = forms.ChoiceField(
|
||
label=_("Product"),
|
||
help_text=_(
|
||
"This product is added to the user's cart if the voucher is redeemed."
|
||
)
|
||
)
|
||
|
||
class Meta:
|
||
model = Voucher
|
||
localized_fields = '__all__'
|
||
fields = [
|
||
'code', 'valid_until', 'block_quota', 'allow_ignore_quota', 'price'
|
||
]
|
||
|
||
def __init__(self, *args, **kwargs):
|
||
instance = kwargs.get('instance')
|
||
initial = kwargs.get('initial')
|
||
if instance:
|
||
try:
|
||
if instance.variation:
|
||
initial['itemvar'] = '%d-%d' % (instance.item.pk, instance.variation.pk)
|
||
elif instance.item:
|
||
initial['itemvar'] = str(instance.item.pk)
|
||
elif instance.quota:
|
||
initial['itemvar'] = 'q-%d' % instance.quota.pk
|
||
except Item.DoesNotExist:
|
||
pass
|
||
super().__init__(*args, **kwargs)
|
||
choices = []
|
||
for i in self.instance.event.items.prefetch_related('variations').all():
|
||
variations = list(i.variations.all())
|
||
if variations:
|
||
choices.append((str(i.pk), _('{product} – Any variation').format(product=i.name)))
|
||
for v in variations:
|
||
choices.append(('%d-%d' % (i.pk, v.pk), '%s – %s' % (i.name, v.value)))
|
||
else:
|
||
choices.append((str(i.pk), i.name))
|
||
for q in self.instance.event.quotas.all():
|
||
choices.append(('q-%d' % q.pk, 'Any product in quota "{quota}"'.format(quota=q)))
|
||
self.fields['itemvar'].choices = choices
|
||
|
||
def clean(self):
|
||
data = super().clean()
|
||
itemid = quotaid = None
|
||
if self.data['itemvar'].startswith('q-'):
|
||
quotaid = self.data['itemvar'][2:]
|
||
elif '-' in self.data['itemvar']:
|
||
itemid, varid = self.data['itemvar'].split('-')
|
||
else:
|
||
itemid, varid = self.data['itemvar'], None
|
||
|
||
if itemid:
|
||
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)
|
||
else:
|
||
self.instance.variation = None
|
||
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
|
||
return data
|
||
|
||
def save(self, commit=True):
|
||
super().save(commit)
|
||
|
||
return ['item']
|