From ad3eb59e6a5a5111e2e91821cda225f919523e83 Mon Sep 17 00:00:00 2001 From: Raphael Michel Date: Thu, 10 Mar 2016 22:18:53 +0100 Subject: [PATCH] Fixed #86 -- Decide about variations at product creation --- src/pretix/base/models/items.py | 4 ++++ src/pretix/control/forms/item.py | 16 +++++++++++++++- .../templates/pretixcontrol/item/base.html | 14 ++++++++++++-- .../templates/pretixcontrol/item/index.html | 3 +++ src/pretix/control/views/item.py | 5 +++-- 5 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/pretix/base/models/items.py b/src/pretix/base/models/items.py index 5049c08de..b204adb69 100644 --- a/src/pretix/base/models/items.py +++ b/src/pretix/base/models/items.py @@ -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): """ diff --git a/src/pretix/control/forms/item.py b/src/pretix/control/forms/item.py index 8c898687b..ce6a50886 100644 --- a/src/pretix/control/forms/item.py +++ b/src/pretix/control/forms/item.py @@ -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 diff --git a/src/pretix/control/templates/pretixcontrol/item/base.html b/src/pretix/control/templates/pretixcontrol/item/base.html index 7e41d29cb..a89963b55 100644 --- a/src/pretix/control/templates/pretixcontrol/item/base.html +++ b/src/pretix/control/templates/pretixcontrol/item/base.html @@ -4,10 +4,20 @@ {% block content %} {% if object.id %}

{% trans "Modify product:" %} {{ object.name }}

+ {% if object.has_variations %} + {% endif %} {% else %}

{% trans "Create product" %}

{% blocktrans trimmed %} diff --git a/src/pretix/control/templates/pretixcontrol/item/index.html b/src/pretix/control/templates/pretixcontrol/item/index.html index 1aa8fc1a6..078026ef4 100644 --- a/src/pretix/control/templates/pretixcontrol/item/index.html +++ b/src/pretix/control/templates/pretixcontrol/item/index.html @@ -8,6 +8,9 @@ {% trans "General information" %} {% 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" %} diff --git a/src/pretix/control/views/item.py b/src/pretix/control/views/item.py index f7950c858..954bd7235 100644 --- a/src/pretix/control/views/item.py +++ b/src/pretix/control/views/item.py @@ -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'