Fix #475 -- add to existing quota / add new quota from product form (#562)

* added fields and logic for adding quota in creation of product

* added section for selecting quota option

* logic for hiding quota selections when needed

* fixed logic for quota selection

* formatting, removed print statements

* styling

* tests for adding quotas in product form

* cleaned up

* added divs

* reworked to include translatable text, readable values for quota options

* quota_add_existing form assignment to str(q.pk)

* made changes for radio buttons, added in sliding animation

* moved string constants for quota options, changed quota options to RadioSelect from Select
This commit is contained in:
Clint
2017-07-26 04:41:23 -07:00
committed by Raphael Michel
parent f3b616e495
commit 4293ec3805
4 changed files with 143 additions and 0 deletions

View File

@@ -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__'