diff --git a/src/pretix/control/forms/item.py b/src/pretix/control/forms/item.py index 03cf209c8..ca22fd08a 100644 --- a/src/pretix/control/forms/item.py +++ b/src/pretix/control/forms/item.py @@ -124,6 +124,9 @@ class QuotaForm(I18nModelForm): class ItemCreateForm(I18nModelForm): + NONE = 'none' + EXISTING = 'existing' + NEW = 'new' has_variations = forms.BooleanField(label=_('The product should exist in multiple variations'), help_text=_('Select this option e.g. for t-shirts that come in multiple sizes. ' 'You can select the variations in the next step.'), @@ -132,6 +135,7 @@ class ItemCreateForm(I18nModelForm): def __init__(self, *args, **kwargs): self.event = kwargs['event'] super().__init__(*args, **kwargs) + self.fields['category'].queryset = self.instance.event.categories.all() self.fields['copy_from'] = forms.ModelChoiceField( label=_("Copy product information"), @@ -141,6 +145,40 @@ class ItemCreateForm(I18nModelForm): required=False ) + self.fields['quota_option'] = forms.ChoiceField( + label=_("Quota options"), + widget=forms.RadioSelect, + choices=( + (self.NONE, _("Do not add to a quota now")), + (self.EXISTING, _("Add product to an existing quota")), + (self.NEW, _("Create a new quota for this product")) + ), + initial=self.NONE, + required=False + ) + + self.fields['quota_add_existing'] = forms.ModelChoiceField( + label=_("Add to existing quota"), + widget=forms.Select(), + queryset=self.instance.event.quotas.all(), + required=False + ) + + self.fields['quota_add_new_name'] = forms.CharField( + label=_("Name"), + max_length=200, + widget=forms.TextInput(attrs={'placeholder': _("New quota name")}), + required=False + ) + + self.fields['quota_add_new_size'] = forms.IntegerField( + min_value=0, + label=_("Size"), + widget=forms.TextInput(attrs={'placeholder': _("New quota size")}), + help_text=_("Leave empty for an unlimited number of tickets."), + required=False + ) + def save(self, *args, **kwargs): if self.cleaned_data.get('copy_from'): self.instance.description = self.cleaned_data['copy_from'].description @@ -156,6 +194,18 @@ class ItemCreateForm(I18nModelForm): instance = super().save(*args, **kwargs) + if self.cleaned_data.get('quota_option') == self.EXISTING and self.cleaned_data.get('quota_add_existing') is not None: + quota = self.cleaned_data.get('quota_add_existing') + quota.items.add(self.instance) + elif self.cleaned_data.get('quota_option') == self.NEW: + quota_name = self.cleaned_data.get('quota_add_new_name') + quota_size = self.cleaned_data.get('quota_add_new_size') + + quota = Quota.objects.create( + event=self.event, name=quota_name, size=quota_size + ) + quota.items.add(self.instance) + if self.cleaned_data.get('has_variations'): if self.cleaned_data.get('copy_from') and self.cleaned_data.get('copy_from').has_variations: for variation in self.cleaned_data['copy_from'].variations.all(): @@ -172,6 +222,22 @@ class ItemCreateForm(I18nModelForm): return instance + def clean(self): + cleaned_data = super().clean() + + if cleaned_data.get('quota_option') == self.NEW: + if not self.cleaned_data.get('quota_add_new_name'): + raise forms.ValidationError( + {'quota_add_new_name': [_("Quota name is required.")]} + ) + elif cleaned_data.get('quota_option') == self.EXISTING: + if not self.cleaned_data.get('quota_add_existing'): + raise forms.ValidationError( + {'quota_add_existing': [_("Please select a quota.")]} + ) + + return cleaned_data + class Meta: model = Item localized_fields = '__all__' diff --git a/src/pretix/control/templates/pretixcontrol/item/create.html b/src/pretix/control/templates/pretixcontrol/item/create.html index 5ec7fd4d6..ef7ae1ba2 100644 --- a/src/pretix/control/templates/pretixcontrol/item/create.html +++ b/src/pretix/control/templates/pretixcontrol/item/create.html @@ -2,6 +2,9 @@ {% load i18n %} {% load bootstrap3 %} {% block inside %} +{% load static %} + +