Hide quota options during product creation for event series

This commit is contained in:
Raphael Michel
2017-07-27 15:14:28 +02:00
parent 5c443e2f93
commit f43d782b5c
2 changed files with 66 additions and 61 deletions

View File

@@ -145,39 +145,40 @@ class ItemCreateForm(I18nModelForm):
required=False required=False
) )
self.fields['quota_option'] = forms.ChoiceField( if not self.event.has_subevents:
label=_("Quota options"), self.fields['quota_option'] = forms.ChoiceField(
widget=forms.RadioSelect, label=_("Quota options"),
choices=( widget=forms.RadioSelect,
(self.NONE, _("Do not add to a quota now")), choices=(
(self.EXISTING, _("Add product to an existing quota")), (self.NONE, _("Do not add to a quota now")),
(self.NEW, _("Create a new quota for this product")) (self.EXISTING, _("Add product to an existing quota")),
), (self.NEW, _("Create a new quota for this product"))
initial=self.NONE, ),
required=False initial=self.NONE,
) required=False
)
self.fields['quota_add_existing'] = forms.ModelChoiceField( self.fields['quota_add_existing'] = forms.ModelChoiceField(
label=_("Add to existing quota"), label=_("Add to existing quota"),
widget=forms.Select(), widget=forms.Select(),
queryset=self.instance.event.quotas.all(), queryset=self.instance.event.quotas.all(),
required=False required=False
) )
self.fields['quota_add_new_name'] = forms.CharField( self.fields['quota_add_new_name'] = forms.CharField(
label=_("Name"), label=_("Name"),
max_length=200, max_length=200,
widget=forms.TextInput(attrs={'placeholder': _("New quota name")}), widget=forms.TextInput(attrs={'placeholder': _("New quota name")}),
required=False required=False
) )
self.fields['quota_add_new_size'] = forms.IntegerField( self.fields['quota_add_new_size'] = forms.IntegerField(
min_value=0, min_value=0,
label=_("Size"), label=_("Size"),
widget=forms.TextInput(attrs={'placeholder': _("New quota size")}), widget=forms.TextInput(attrs={'placeholder': _("New quota size")}),
help_text=_("Leave empty for an unlimited number of tickets."), help_text=_("Leave empty for an unlimited number of tickets."),
required=False required=False
) )
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
if self.cleaned_data.get('copy_from'): if self.cleaned_data.get('copy_from'):
@@ -194,17 +195,18 @@ class ItemCreateForm(I18nModelForm):
instance = super().save(*args, **kwargs) 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: if not self.event.has_subevents:
quota = self.cleaned_data.get('quota_add_existing') if self.cleaned_data.get('quota_option') == self.EXISTING and self.cleaned_data.get('quota_add_existing') is not None:
quota.items.add(self.instance) quota = self.cleaned_data.get('quota_add_existing')
elif self.cleaned_data.get('quota_option') == self.NEW: quota.items.add(self.instance)
quota_name = self.cleaned_data.get('quota_add_new_name') elif self.cleaned_data.get('quota_option') == self.NEW:
quota_size = self.cleaned_data.get('quota_add_new_size') quota_name = self.cleaned_data.get('quota_add_new_name')
quota_size = self.cleaned_data.get('quota_add_new_size')
quota = Quota.objects.create( quota = Quota.objects.create(
event=self.event, name=quota_name, size=quota_size event=self.event, name=quota_name, size=quota_size
) )
quota.items.add(self.instance) quota.items.add(self.instance)
if self.cleaned_data.get('has_variations'): if self.cleaned_data.get('has_variations'):
if self.cleaned_data.get('copy_from') and self.cleaned_data.get('copy_from').has_variations: if self.cleaned_data.get('copy_from') and self.cleaned_data.get('copy_from').has_variations:
@@ -225,16 +227,17 @@ class ItemCreateForm(I18nModelForm):
def clean(self): def clean(self):
cleaned_data = super().clean() cleaned_data = super().clean()
if cleaned_data.get('quota_option') == self.NEW: if not self.event.has_subevents:
if not self.cleaned_data.get('quota_add_new_name'): if cleaned_data.get('quota_option') == self.NEW:
raise forms.ValidationError( if not self.cleaned_data.get('quota_add_new_name'):
{'quota_add_new_name': [_("Quota name is required.")]} 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'): elif cleaned_data.get('quota_option') == self.EXISTING:
raise forms.ValidationError( if not self.cleaned_data.get('quota_add_existing'):
{'quota_add_existing': [_("Please select a quota.")]} raise forms.ValidationError(
) {'quota_add_existing': [_("Please select a quota.")]}
)
return cleaned_data return cleaned_data

View File

@@ -15,17 +15,19 @@
{% bootstrap_field form.category layout="horizontal" %} {% bootstrap_field form.category layout="horizontal" %}
{% bootstrap_field form.admission layout="horizontal" %} {% bootstrap_field form.admission layout="horizontal" %}
</fieldset> </fieldset>
<fieldset> {% if form.quota_option %}
<legend>{% trans "Quota settings" %}</legend> <fieldset>
{% bootstrap_field form.quota_option layout="horizontal" %} <legend>{% trans "Quota settings" %}</legend>
<div id="existing-quota-group"> {% bootstrap_field form.quota_option layout="horizontal" %}
{% bootstrap_field form.quota_add_existing layout="horizontal" %} <div id="existing-quota-group">
</div> {% bootstrap_field form.quota_add_existing layout="horizontal" %}
<div id="new-quota-group"> </div>
{% bootstrap_field form.quota_add_new_name layout="horizontal" %} <div id="new-quota-group">
{% bootstrap_field form.quota_add_new_size layout="horizontal" %} {% bootstrap_field form.quota_add_new_name layout="horizontal" %}
</div> {% bootstrap_field form.quota_add_new_size layout="horizontal" %}
</fieldset> </div>
</fieldset>
{% endif %}
<fieldset> <fieldset>
<legend>{% trans "Price settings" %}</legend> <legend>{% trans "Price settings" %}</legend>
{% bootstrap_field form.default_price layout="horizontal" %} {% bootstrap_field form.default_price layout="horizontal" %}