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

@@ -153,11 +153,12 @@ class AddOnsStep(CartMixin, AsyncAction, TemplateFlowStep):
@cached_property
def forms(self):
"""
A list of forms with one form for each cart position that has questions
the user can answer. All forms have a custom prefix, so that they can all be
submitted at once.
A list of forms with one form for each cart position that can have add-ons.
All forms have a custom prefix, so that they can all be submitted at once.
"""
formset = []
quota_cache = {}
item_cache = {}
for cartpos in get_cart(self.request).filter(addon_to__isnull=True).prefetch_related(
'item__addons', 'item__addons__addon_category', 'addons', 'addons__variation'
):
@@ -180,7 +181,9 @@ class AddOnsStep(CartMixin, AsyncAction, TemplateFlowStep):
prefix='{}_{}'.format(cartpos.pk, iao.addon_category.pk),
category=iao.addon_category,
initial=current_addon_products,
data=(self.request.POST if self.request.method == 'POST' else None)
data=(self.request.POST if self.request.method == 'POST' else None),
quota_cache=quota_cache,
item_cache=item_cache
)
}
@@ -324,7 +327,7 @@ class QuestionsStep(QuestionsViewMixin, CartMixin, TemplateFlowStep):
def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
ctx['forms'] = self.forms
ctx['formgroups'] = self.formdict.items()
ctx['contact_form'] = self.contact_form
ctx['invoice_form'] = self.invoice_form
return ctx

View File

@@ -59,8 +59,8 @@ class QuestionsForm(forms.Form):
:param cartpos: The cart position the form should be for
:param event: The event this belongs to
"""
cartpos = kwargs.pop('cartpos', None)
orderpos = kwargs.pop('orderpos', None)
cartpos = self.cartpos = kwargs.pop('cartpos', None)
orderpos = self.orderpos = kwargs.pop('orderpos', None)
item = cartpos.item if cartpos else orderpos.item
questions = list(item.questions.all())
event = kwargs.pop('event')
@@ -151,7 +151,7 @@ class AddOnsForm(forms.Form):
This form class is responsible for selecting add-ons to a product in the cart.
"""
def _label(self, event, item_or_variation):
def _label(self, event, item_or_variation, avail):
if isinstance(item_or_variation, ItemVariation):
variation = item_or_variation
item = item_or_variation.item
@@ -165,69 +165,89 @@ class AddOnsForm(forms.Form):
label = item.name
if not item.tax_rate or not price:
return '{name} (+ {currency} {price})'.format(
n = '{name} (+ {currency} {price})'.format(
name=label, currency=event.currency, price=number_format(price)
)
elif event.settings.display_net_prices:
return '{name} (+ {currency} {price} plus {taxes}% taxes)'.format(
n = '{name} (+ {currency} {price} plus {taxes}% taxes)'.format(
name=label, currency=event.currency, price=number_format(price_net),
taxes=number_format(item.tax_rate)
)
else:
return '{name} (+ {currency} {price} incl. {taxes}% taxes)'.format(
n = '{name} (+ {currency} {price} incl. {taxes}% taxes)'.format(
name=label, currency=event.currency, price=number_format(price),
taxes=number_format(item.tax_rate)
)
if avail[0] < 20:
n += ' {}'.format(_('SOLD OUT'))
elif avail[0] < 100:
n += ' {}'.format(_('Currently unavailable'))
return n
def __init__(self, *args, **kwargs):
"""
Takes additional keyword arguments:
:param category: The category to choose from
:param event: The event this belongs to
:param initial: The current set of add-ons
:param quota_cache: A shared dictionary for quota caching
:param item_cache: A shared dictionary for item/category caching
"""
category = kwargs.pop('category')
event = kwargs.pop('event')
current_addons = kwargs.pop('initial')
quota_cache = kwargs.pop('quota_cache')
item_cache = kwargs.pop('item_cache')
super().__init__(*args, **kwargs)
items = category.items.filter(
Q(active=True)
& Q(Q(available_from__isnull=True) | Q(available_from__lte=now()))
& Q(Q(available_until__isnull=True) | Q(available_until__gte=now()))
& Q(hide_without_voucher=False)
).prefetch_related(
'variations__quotas', # for .availability()
Prefetch('quotas', queryset=event.quotas.all()),
Prefetch('variations', to_attr='available_variations',
queryset=ItemVariation.objects.filter(active=True, quotas__isnull=False).distinct()),
).annotate(
quotac=Count('quotas'),
has_variations=Count('variations')
).filter(
quotac__gt=0
).order_by('category__position', 'category_id', 'position', 'name')
if category.pk not in item_cache:
# Get all items to possibly show
items = category.items.filter(
Q(active=True)
& Q(Q(available_from__isnull=True) | Q(available_from__lte=now()))
& Q(Q(available_until__isnull=True) | Q(available_until__gte=now()))
& Q(hide_without_voucher=False)
).prefetch_related(
'variations__quotas', # for .availability()
Prefetch('quotas', queryset=event.quotas.all()),
Prefetch('variations', to_attr='available_variations',
queryset=ItemVariation.objects.filter(active=True, quotas__isnull=False).distinct()),
).annotate(
quotac=Count('quotas'),
has_variations=Count('variations')
).filter(
quotac__gt=0
).order_by('category__position', 'category_id', 'position', 'name')
item_cache[category.pk] = items
else:
items = item_cache[category.pk]
for i in items:
if i.has_variations:
choices = [('', '')]
for v in i.available_variations:
cached_availability = v.check_quotas(_cache=quota_cache)
choices.append((v.pk, self._label(event, v, cached_availability)))
field = forms.ChoiceField(
choices=[('', '')] + [
(
v.pk,
self._label(event, v)
) for v in i.available_variations
],
choices=choices,
label=i.name,
required=False,
widget=forms.RadioSelect,
help_text=i.description,
initial=current_addons.get(i.pk)
)
else:
cached_availability = i.check_quotas(_cache=quota_cache)
field = forms.BooleanField(
label=self._label(event, i),
label=self._label(event, i, cached_availability),
required=False,
initial=i.pk in current_addons
initial=i.pk in current_addons,
help_text=i.description
)
self.fields['item_%s' % i.pk] = field

View File

@@ -33,6 +33,10 @@
{% blocktrans trimmed with min_count=c.min_count %}
You need to choose {{ min_count }} options from this category.
{% endblocktrans %}
{% elif c.min_count == 0 %}
{% blocktrans trimmed with max_count=c.max_count %}
You can to choose up to {{ max_count }} options from this category.
{% endblocktrans %}
{% else %}
{% blocktrans trimmed with min_count=c.min_count max_count=c.max_count %}
You can choose between {{ min_count }} and {{ max_count }} options from

View File

@@ -42,14 +42,14 @@
</div>
{% endif %}
{% for form in forms %}
{% for pos, forms in formgroups %}
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" href="#cp{{ form.pos.id }}">
<strong>{{ form.pos.item.name }}</strong>
{% if form.pos.variation %}
{{ form.pos.variation }}
<a data-toggle="collapse" href="#cp{{ pos.id }}">
<strong>{{ pos.item.name }}</strong>
{% if pos.variation %}
{{ pos.variation }}
{% endif %}
</a>
{% if forloop.counter > 1 %}
@@ -59,10 +59,16 @@
{% endif %}
</h4>
</div>
<div id="cp{{ form.pos.id }}"
<div id="cp{{ pos.id }}"
class="panel-collapse collapsed in">
<div class="panel-body" data-idx="{{ forloop.counter0 }}">
{% bootstrap_form form layout="horizontal" %}
{% for form in forms %}
{% if form.pos.item != pos.item %}
{# Add-Ons #}
<legend>+ {{ form.pos.item }}</legend>
{% endif %}
{% bootstrap_form form layout="horizontal" %}
{% endfor %}
</div>
</div>
</div>

View File

@@ -3,6 +3,9 @@
{% for line in cart.positions %}
<div class="row cart-row {% if download %}has-downloads{% endif %}">
<div class="product">
{% if line.addon_to %}
<span class="addon-signifier">+</span>
{% endif %}
<strong>{{ line.item.name }}</strong>
{% if line.variation %}
{{ line.variation }}
@@ -32,12 +35,23 @@
{% if download %}
<div class="download-desktop">
{% for b in download_buttons %}
<a href="{% eventurl event "presale:event.order.download" secret=order.secret order=order.code output=b.identifier position=line.id %}"
class="btn btn-default btn-sm" data-asyncdownload>
<span class="fa fa-download"></span> {{ b.text }}
</a>
{% endfor %}
{% if not line.addon_to or event.settings.ticket_download_addons %}
{% for b in download_buttons %}
<a href="{% eventurl event "presale:event.order.download" secret=order.secret order=order.code output=b.identifier position=line.id %}"
class="btn btn-default btn-sm" data-asyncdownload>
<span class="fa fa-download"></span> {{ b.text }}
</a>
{% endfor %}
{% endif %}
</div>
{% elif line.addon_to %}
<div class="count">&nbsp;</div>
<div class="singleprice price">
{% if event.settings.display_net_prices %}
{{ event.currency }} {{ line.net_price|floatformat:2 }}
{% else %}
{{ event.currency }} {{ line.price|floatformat:2 }}
{% endif %}
</div>
{% else %}
<div class="count">
@@ -67,7 +81,9 @@
<input type="hidden" name="price_{{ line.item.id }}"
value="{% if event.settings.display_net_prices %}{{ line.net_price }}{% else %}{{ line.price }}{% endif %}" />
{% endif %}
<button class="btn btn-mini btn-link"><i class="fa fa-plus"></i></button>
<button class="btn btn-mini btn-link">
<i class="fa fa-plus"></i>
</button>
</form>
{% endif %}
</div>
@@ -98,12 +114,14 @@
</div>
{% if download %}
<div class="download-mobile">
{% for b in download_buttons %}
<a href="{% eventurl event "presale:event.order.download" secret=order.secret order=order.code output=b.identifier position=line.id %}"
class="btn btn-default btn-sm" data-asyncdownload>
<span class="fa fa-download"></span> {{ b.text }}
</a>
{% endfor %}
{% if not line.addon_to or event.settings.ticket_download_addons %}
{% for b in download_buttons %}
<a href="{% eventurl event "presale:event.order.download" secret=order.secret order=order.code output=b.identifier position=line.id %}"
class="btn btn-default btn-sm" data-asyncdownload>
<span class="fa fa-download"></span> {{ b.text }}
</a>
{% endfor %}
{% endif %}
</div>
{% endif %}
<div class="clearfix"></div>

View File

@@ -36,23 +36,28 @@
</div>
</div>
{% endif %}
{% for form in forms %}
{% for pos, forms in formgroups %}
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" href="#cp{{ form.pos.id }}"
data-parent="#questions_accordion">
<strong>{{ form.pos.item }}</strong>
{% if form.pos.variation %}
{{ form.pos.variation }}
<a data-toggle="collapse" href="#cp{{ pos.id }}">
<strong>{{ pos.item.name }}</strong>
{% if pos.variation %}
{{ pos.variation }}
{% endif %}
</a>
</h4>
</div>
<div id="cp{{ form.pos.id }}"
class="panel-collapse collapsed {% if forloop.counter0 == 0 %}in{% endif %}">
<div id="cp{{ pos.id }}"
class="panel-collapse collapsed in">
<div class="panel-body">
{% bootstrap_form form layout="horizontal" %}
{% for form in forms %}
{% if form.pos.item != pos.item %}
{# Add-Ons #}
<legend>+ {{ form.pos.item }}</legend>
{% endif %}
{% bootstrap_form form layout="horizontal" %}
{% endfor %}
</div>
</div>
</div>

View File

@@ -18,7 +18,7 @@ event_patterns = [
url(r'^cart/clear$', pretix.presale.views.cart.CartClear.as_view(), name='event.cart.clear'),
url(r'^waitinglist', pretix.presale.views.waiting.WaitingView.as_view(), name='event.waitinglist'),
url(r'^checkout/start$', pretix.presale.views.checkout.CheckoutView.as_view(), name='event.checkout.start'),
url(r'^redeem$', pretix.presale.views.cart.RedeemView.as_view(),
url(r'^redeem/?$', pretix.presale.views.cart.RedeemView.as_view(),
name='event.redeem'),
url(r'^checkout/(?P<step>[^/]+)/$', pretix.presale.views.checkout.CheckoutView.as_view(),
name='event.checkout'),

View File

@@ -29,35 +29,45 @@ class CartMixin:
cartpos = queryset.order_by(
'item', 'variation'
).select_related(
'item', 'variation'
'item', 'variation', 'addon_to'
).prefetch_related(
*prefetch
)
else:
cartpos = self.positions
lcp = list(cartpos)
has_addons = {cp.addon_to.pk for cp in lcp if cp.addon_to}
# Group items of the same variation
# We do this by list manipulations instead of a GROUP BY query, as
# Django is unable to join related models in a .values() query
def keyfunc(pos):
if isinstance(pos, OrderPosition):
i = pos.positionid
if pos.addon_to:
i = pos.addon_to.positionid
else:
i = pos.positionid
else:
i = pos.pk
if downloads:
return i, pos.pk, 0, 0, 0, 0,
if pos.addon_to:
i = pos.addon_to.pk
else:
i = pos.pk
has_attendee_data = pos.item.admission and (
self.request.event.settings.attendee_names_asked
or self.request.event.settings.attendee_emails_asked
)
addon_penalty = 1 if pos.addon_to else 0
if downloads or pos.pk in has_addons or pos.addon_to:
return i, addon_penalty, pos.pk, 0, 0, 0, 0,
if answers and (has_attendee_data or pos.item.questions.all()):
return i, pos.pk, 0, 0, 0, 0,
return 0, 0, pos.item_id, pos.variation_id, pos.price, (pos.voucher_id or 0)
return i, addon_penalty, pos.pk, 0, 0, 0, 0,
return 0, addon_penalty, 0, pos.item_id, pos.variation_id, pos.price, (pos.voucher_id or 0)
positions = []
for k, g in groupby(sorted(list(cartpos), key=keyfunc), key=keyfunc):
for k, g in groupby(sorted(lcp, key=keyfunc), key=keyfunc):
g = list(g)
group = g[0]
group.count = len(g)

View File

@@ -171,6 +171,7 @@ class RedeemView(EventViewMixin, TemplateView):
Q(active=True)
& Q(Q(available_from__isnull=True) | Q(available_from__lte=now()))
& Q(Q(available_until__isnull=True) | Q(available_until__gte=now()))
& ~Q(category__is_addon=True)
)
vouchq = Q(hide_without_voucher=False)

View File

@@ -46,6 +46,7 @@ def get_grouped_items(event):
& Q(Q(available_from__isnull=True) | Q(available_from__lte=now()))
& Q(Q(available_until__isnull=True) | Q(available_until__gte=now()))
& Q(hide_without_voucher=False)
& ~Q(category__is_addon=True)
).select_related(
'category', # for re-grouping
).prefetch_related(

View File

@@ -3,7 +3,7 @@ from datetime import timedelta
from django.contrib import messages
from django.db import transaction
from django.db.models import Sum
from django.http import FileResponse, Http404, HttpResponse
from django.http import FileResponse, Http404, JsonResponse
from django.shortcuts import redirect, render
from django.utils.functional import cached_property
from django.utils.timezone import now
@@ -392,9 +392,7 @@ class OrderModify(EventViewMixin, OrderDetailMixin, QuestionsViewMixin, Template
@cached_property
def positions(self):
return list(self.order.positions.order_by(
'item', 'variation'
).select_related(
return list(self.order.positions.select_related(
'item', 'variation'
).prefetch_related(
'variation', 'item__questions', 'answers'
@@ -445,7 +443,7 @@ class OrderModify(EventViewMixin, OrderDetailMixin, QuestionsViewMixin, Template
def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
ctx['order'] = self.order
ctx['forms'] = self.forms
ctx['formgroups'] = self.formdict.items()
ctx['invoice_form'] = self.invoice_form
return ctx
@@ -501,6 +499,12 @@ class OrderCancelDo(EventViewMixin, OrderDetailMixin, AsyncAction, View):
class OrderDownload(EventViewMixin, OrderDetailMixin, View):
def get_self_url(self):
return eventreverse(self.request.event,
'presale:event.order.download' if 'position' in self.kwargs
else 'presale:event.order.download.combined',
kwargs=self.kwargs)
@cached_property
def output(self):
responses = register_ticket_outputs.send(self.request.event)
@@ -516,20 +520,30 @@ class OrderDownload(EventViewMixin, OrderDetailMixin, View):
except OrderPosition.DoesNotExist:
return None
def error(self, msg):
messages.error(self.request, msg)
if "ajax" in self.request.POST or "ajax" in self.request.GET:
return JsonResponse({
'ready': True,
'success': False,
'redirect': self.get_order_url(),
'message': msg,
})
return redirect(self.get_order_url())
def get(self, request, *args, **kwargs):
if not self.output or not self.output.is_enabled:
messages.error(request, _('You requested an invalid ticket output type.'))
return redirect(self.get_order_url())
return self.error(_('You requested an invalid ticket output type.'))
if not self.order or ('position' in kwargs and not self.order_position):
raise Http404(_('Unknown order code or not authorized to access this order.'))
if self.order.status != Order.STATUS_PAID:
messages.error(request, _('Order is not paid.'))
return redirect(self.get_order_url())
return self.error(_('Order is not paid.'))
if (not self.request.event.settings.ticket_download
or (self.request.event.settings.ticket_download_date is not None
and now() < self.request.event.settings.ticket_download_date)):
messages.error(request, _('Ticket download is not (yet) enabled.'))
return redirect(self.get_order_url())
return self.error(_('Ticket download is not (yet) enabled.'))
if 'position' in kwargs and (self.order_position.addon_to and not self.request.event.settings.ticket_download_addons):
return self.error(_('Ticket download is not enabled for add-on products.'))
if 'position' in kwargs:
return self._download_position()
@@ -555,7 +569,11 @@ class OrderDownload(EventViewMixin, OrderDetailMixin, View):
generate_order.apply_async(args=(self.order.id, self.output.identifier))
if 'ajax' in self.request.GET:
return HttpResponse('1' if ct and ct.file else '0')
return JsonResponse({
'ready': bool(ct and ct.file),
'success': False,
'redirect': self.get_self_url()
})
elif not ct.file:
return render(self.request, "pretixbase/cachedfiles/pending.html", {})
else:
@@ -584,7 +602,11 @@ class OrderDownload(EventViewMixin, OrderDetailMixin, View):
generate.apply_async(args=(self.order_position.id, self.output.identifier))
if 'ajax' in self.request.GET:
return HttpResponse('1' if ct and ct.file else '0')
return JsonResponse({
'ready': bool(ct and ct.file),
'success': False,
'redirect': self.get_self_url()
})
elif not ct.file:
return render(self.request, "pretixbase/cachedfiles/pending.html", {})
else:

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: