Use a simpler form for creating items

This commit is contained in:
Raphael Michel
2016-05-07 16:30:55 +02:00
parent 8edf9a7034
commit c9350dde40
4 changed files with 53 additions and 22 deletions

View File

@@ -97,7 +97,32 @@ class QuotaForm(I18nModelForm):
]
class ItemFormGeneral(I18nModelForm):
class ItemCreateForm(I18nModelForm):
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.'),
required=False)
def save(self, *args, **kwargs):
instance = super().save(*args, **kwargs)
if self.cleaned_data.get('has_variations'):
ItemVariation.objects.create(
item=instance, value=__('Standard')
)
return instance
class Meta:
model = Item
localized_fields = '__all__'
fields = [
'name',
'admission',
'default_price',
'tax_rate',
]
class ItemUpdateForm(I18nModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['category'].queryset = self.instance.event.categories.all()
@@ -120,21 +145,6 @@ class ItemFormGeneral(I18nModelForm):
]
class ItemCreateForm(ItemFormGeneral):
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.'),
required=False)
def save(self, *args, **kwargs):
instance = super().save(*args, **kwargs)
if self.cleaned_data.get('has_variations'):
ItemVariation.objects.create(
item=instance, value=__('Standard')
)
return instance
class ItemVariationForm(I18nModelForm):
class Meta:
model = ItemVariation

View File

@@ -0,0 +1,24 @@
{% extends "pretixcontrol/item/base.html" %}
{% load i18n %}
{% load bootstrap3 %}
{% block inside %}
<form action="" method="post" class="form-horizontal" enctype="multipart/form-data">
{% csrf_token %}
<fieldset>
<legend>{% trans "General information" %}</legend>
{% bootstrap_field form.name layout="horizontal" %}
{% bootstrap_field form.has_variations layout="horizontal" %}
{% bootstrap_field form.admission layout="horizontal" %}
</fieldset>
<fieldset>
<legend>{% trans "Price settings" %}</legend>
{% bootstrap_field form.default_price layout="horizontal" %}
{% bootstrap_field form.tax_rate layout="horizontal" %}
</fieldset>
<div class="form-group submit-group">
<button type="submit" class="btn btn-primary btn-save">
{% trans "Save and continue with more settings" %}
</button>
</div>
</form>
{% endblock %}

View File

@@ -8,9 +8,6 @@
<legend>{% trans "General information" %}</legend>
{% bootstrap_field form.name layout="horizontal" %}
{% bootstrap_field form.active layout="horizontal" %}
{% if form.has_variations %}
{% bootstrap_field form.has_variations layout="horizontal" %}
{% endif %}
{% bootstrap_field form.category layout="horizontal" %}
{% bootstrap_field form.admission layout="horizontal" %}
{% bootstrap_field form.description layout="horizontal" %}

View File

@@ -17,7 +17,7 @@ from pretix.base.models import (
Item, ItemCategory, ItemVariation, Question, QuestionOption, Quota,
)
from pretix.control.forms.item import (
CategoryForm, ItemCreateForm, ItemFormGeneral, ItemVariationForm,
CategoryForm, ItemCreateForm, ItemUpdateForm, ItemVariationForm,
QuestionForm, QuestionOptionForm, QuotaForm,
)
from pretix.control.permissions import (
@@ -568,7 +568,7 @@ class ItemDetailMixin(SingleObjectMixin):
class ItemCreate(EventPermissionRequiredMixin, CreateView):
form_class = ItemCreateForm
template_name = 'pretixcontrol/item/index.html'
template_name = 'pretixcontrol/item/create.html'
permission = 'can_change_items'
def get_success_url(self) -> str:
@@ -601,7 +601,7 @@ class ItemCreate(EventPermissionRequiredMixin, CreateView):
class ItemUpdateGeneral(ItemDetailMixin, EventPermissionRequiredMixin, UpdateView):
form_class = ItemFormGeneral
form_class = ItemUpdateForm
template_name = 'pretixcontrol/item/index.html'
permission = 'can_change_items'