Improve add-on products

This commit is contained in:
Raphael Michel
2017-03-19 14:33:45 +01:00
parent 5bcfb958f0
commit b52f2f5a9e
36 changed files with 802 additions and 131 deletions

View File

@@ -1,3 +1,5 @@
from collections import defaultdict
from django import forms
from django.utils.functional import cached_property
@@ -8,8 +10,18 @@ from pretix.presale.views import get_cart
class QuestionsViewMixin:
@staticmethod
def _keyfunc(pos):
# Sort addons after the item they are an addon to
if isinstance(pos, OrderPosition):
i = pos.addon_to.positionid if pos.addon_to else pos.positionid
else:
i = pos.addon_to.pk if pos.addon_to else pos.pk
addon_penalty = 1 if pos.addon_to else 0
return i, addon_penalty, pos.pk
def _positions_for_questions(self):
return get_cart(self.request)
return sorted(get_cart(self.request), key=self._keyfunc)
@cached_property
def forms(self):
@@ -32,6 +44,17 @@ class QuestionsViewMixin:
formlist.append(form)
return formlist
@cached_property
def formdict(self):
storage = defaultdict(list)
for f in self.forms:
pos = f.cartpos or f.orderpos
if pos.addon_to_id:
storage[pos.addon_to].append(f)
else:
storage[pos].append(f)
return storage
def save(self):
failed = False
for form in self.forms: