Fixed #86 -- Decide about variations at product creation

This commit is contained in:
Raphael Michel
2016-03-10 22:18:53 +01:00
parent 98e135d0c6
commit ad3eb59e6a
5 changed files with 37 additions and 5 deletions

View File

@@ -211,6 +211,10 @@ class Item(LoggedModel):
return min([q.availability() for q in self.quotas.all()],
key=lambda s: (s[0], s[1] if s[1] is not None else sys.maxsize))
@cached_property
def has_variations(self):
return self.variations.exists()
class ItemVariation(models.Model):
"""

View File

@@ -2,7 +2,7 @@ import copy
from django import forms
from django.forms import BooleanField, ModelMultipleChoiceField
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import ugettext as __, ugettext_lazy as _
from pretix.base.forms import I18nModelForm
from pretix.base.models import (
@@ -101,6 +101,20 @@ 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.'))
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

@@ -4,10 +4,20 @@
{% block content %}
{% if object.id %}
<h1>{% trans "Modify product:" %} {{ object.name }}</h1>
{% if object.has_variations %}
<ul class="nav nav-pills">
<li {% if "event.item" == url_name %}class="active"{% endif %}><a href="{% url 'control:event.item' organizer=request.event.organizer.slug event=request.event.slug item=object.id %}">{% trans "General information" %}</a></li>
<li {% if "event.item.variations" == url_name %}class="active"{% endif %}><a href="{% url 'control:event.item.variations' organizer=request.event.organizer.slug event=request.event.slug item=object.id %}">{% trans "Variations" %}</a></li>
<li {% if "event.item" == url_name %}class="active"{% endif %}>
<a href="{% url 'control:event.item' organizer=request.event.organizer.slug event=request.event.slug item=object.id %}">
{% trans "General information" %}
</a>
</li>
<li {% if "event.item.variations" == url_name %}class="active"{% endif %}>
<a href="{% url 'control:event.item.variations' organizer=request.event.organizer.slug event=request.event.slug item=object.id %}">
{% trans "Variations" %}
</a>
</li>
</ul>
{% endif %}
{% else %}
<h1>{% trans "Create product" %}</h1>
<p>{% blocktrans trimmed %}

View File

@@ -8,6 +8,9 @@
<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,8 @@ from pretix.base.models import (
Item, ItemCategory, ItemVariation, Question, Quota,
)
from pretix.control.forms.item import (
CategoryForm, ItemFormGeneral, ItemVariationForm, QuestionForm, QuotaForm,
CategoryForm, ItemCreateForm, ItemFormGeneral, ItemVariationForm,
QuestionForm, QuotaForm,
)
from pretix.control.permissions import (
EventPermissionRequiredMixin, event_permission_required,
@@ -491,7 +492,7 @@ class ItemDetailMixin(SingleObjectMixin):
class ItemCreate(EventPermissionRequiredMixin, CreateView):
form_class = ItemFormGeneral
form_class = ItemCreateForm
template_name = 'pretixcontrol/item/index.html'
permission = 'can_change_items'