mirror of
https://github.com/pretix/pretix.git
synced 2026-05-06 15:24:02 +00:00
Allow for vouchers that are valid for multiple items
This commit is contained in:
@@ -189,27 +189,6 @@
|
||||
</section>
|
||||
{% endfor %}
|
||||
{% if event.presale_is_running %}
|
||||
{% if vouchers_exist %}
|
||||
<div class="row-fluid voucher-row">
|
||||
<div class="col-md-4 col-md-offset-8 col-xs-12">
|
||||
<div id="voucher-box">
|
||||
<label for="voucher">{% trans "Redeem a voucher" %}</label>
|
||||
|
||||
<div class="input-group">
|
||||
<span class="input-group-addon"><i class="fa fa-ticket fa-fw"></i></span>
|
||||
<input type="text" class="form-control" name="voucher" id="voucher"
|
||||
placeholder="{% trans "Voucher code" %}">
|
||||
</div>
|
||||
</div>
|
||||
<div id="voucher-toggle">
|
||||
<a href="javascript:void(0);">
|
||||
<span class="fa fa-ticket"></span> {% trans "Redeem a voucher" %}
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
<div class="clearfix"></div>
|
||||
</div>
|
||||
{% endif %}
|
||||
<div class="row-fluid checkout-button-row">
|
||||
<div class="col-md-4 col-md-offset-8 col-xs-12">
|
||||
<button class="btn btn-block btn-primary btn-lg" type="submit">
|
||||
@@ -221,4 +200,24 @@
|
||||
{% endif %}
|
||||
</form>
|
||||
{% endif %}
|
||||
{% if vouchers_exist %}
|
||||
<h2>{% trans "Redeem a voucher" %}</h2>
|
||||
<form method="get" action="{% eventurl event "presale:event.redeem" %}">
|
||||
<div class="row-fluid">
|
||||
<div class="col-md-8 col-sm-6 col-xs-12">
|
||||
<div class="input-group">
|
||||
<span class="input-group-addon"><i class="fa fa-ticket fa-fw"></i></span>
|
||||
<input type="text" class="form-control" name="voucher" id="voucher"
|
||||
placeholder="{% trans "Voucher code" %}">
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-4 col-sm-6 col-xs-12">
|
||||
<button class="btn btn-block btn-primary" type="submit">
|
||||
{% trans "Redeem voucher" %}
|
||||
</button>
|
||||
</div>
|
||||
<div class="clearfix"></div>
|
||||
</div>
|
||||
</form>
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
||||
154
src/pretix/presale/templates/pretixpresale/event/voucher.html
Normal file
154
src/pretix/presale/templates/pretixpresale/event/voucher.html
Normal file
@@ -0,0 +1,154 @@
|
||||
{% extends "pretixpresale/event/base.html" %}
|
||||
{% load i18n %}
|
||||
{% load eventurl %}
|
||||
{% load thumbnail %}
|
||||
{% block title %}{% trans "Voucher redemption" %}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<h2>{% trans "Voucher redemption" %}</h2>
|
||||
<p>
|
||||
{% blocktrans trimmed %}
|
||||
You entered a voucher code that allows you to buy one of the following products at the specified price:
|
||||
{% endblocktrans %}
|
||||
</p>
|
||||
{% if event.presale_is_running or event.settings.show_items_outside_presale_period %}
|
||||
<form method="post" data-asynctask
|
||||
action="{% eventurl request.event "presale:event.cart.add" %}?next={{ request.path|urlencode }}">
|
||||
{% csrf_token %}
|
||||
<input type="hidden" name="_voucher_code" value="{{ voucher.code }}">
|
||||
{% for tup in items_by_category %}
|
||||
<section>
|
||||
{% if tup.0 %}<h3>{{ tup.0.name }}</h3>{% endif %}
|
||||
{% for item in tup.1 %}
|
||||
{% if item.has_variations %}
|
||||
<div class="item-with-variations">
|
||||
<div class="row-fluid product-row headline">
|
||||
<div class="col-md-8 col-xs-12">
|
||||
{% if item.picture %}
|
||||
<a href="{{ item.picture.url }}" class="productpicture"
|
||||
data-title="{{ item.name }}"
|
||||
data-lightbox="{{ item.id }}">
|
||||
<img src="{{ item.picture|thumbnail_url:'productlist' }}"
|
||||
alt="{{ item.name }}"/>
|
||||
</a>
|
||||
{% endif %}
|
||||
<strong>{{ item.name }}</strong>
|
||||
{% if item.description %}<p>{{ item.description }}</p>{% endif %}
|
||||
</div>
|
||||
<div class="col-md-2 col-xs-6 price">
|
||||
{% if item.min_price != item.max_price or item.free_price %}
|
||||
{% blocktrans trimmed with minprice=item.min_price|floatformat:2 currency=event.currency %}
|
||||
from {{ currency }} {{ minprice }}
|
||||
{% endblocktrans %}
|
||||
{% else %}
|
||||
{{ event.currency }} {{ item.min_price|floatformat:2 }}
|
||||
{% endif %}
|
||||
</div>
|
||||
<div class="col-md-2 col-xs-6 availability-box">
|
||||
</div>
|
||||
<div class="clearfix"></div>
|
||||
</div>
|
||||
<div class="">
|
||||
{% for var in item.available_variations %}
|
||||
<div class="row-fluid product-row variation">
|
||||
<div class="col-md-8 col-xs-12">
|
||||
{{ var }}
|
||||
</div>
|
||||
<div class="col-md-2 col-xs-6 price">
|
||||
{% if item.free_price %}
|
||||
<div class="input-group input-group-price">
|
||||
<span class="input-group-addon">{{ event.currency }}</span>
|
||||
<input type="number" class="form-control input-item-price"
|
||||
placeholder="0"
|
||||
min="{{ var.price|stringformat:"0.2f" }}"
|
||||
name="price_{{ item.id }}_{{ var.id }}"
|
||||
step="0.01" value="{{ var.price|stringformat:"0.2f" }}">
|
||||
</div>
|
||||
{% else %}
|
||||
{{ event.currency }} {{ var.price|floatformat:2 }}
|
||||
{% endif %}
|
||||
{% if item.tax_rate %}
|
||||
<small>{% blocktrans trimmed with rate=item.tax_rate %}
|
||||
incl. {{ rate }}% taxes
|
||||
{% endblocktrans %}</small>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% if var.cached_availability.0 == 100 %}
|
||||
<div class="col-md-2 col-xs-6 availability-box available radio-box">
|
||||
<label>
|
||||
<input type="radio" name="_voucher_item"
|
||||
{% if options == 1 %}checked="checked"{% endif %}
|
||||
value="variation_{{ item.id }}_{{ var.id }}">
|
||||
</label>
|
||||
</div>
|
||||
{% else %}
|
||||
{% include "pretixpresale/event/fragment_availability.html" with avail=var.cached_availability.0 %}
|
||||
{% endif %}
|
||||
<div class="clearfix"></div>
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
{% else %}
|
||||
<div class="row-fluid product-row simple">
|
||||
<div class="col-md-8 col-xs-12">
|
||||
{% if item.picture %}
|
||||
<a href="{{ item.picture.url }}" class="productpicture"
|
||||
data-title="{{ item.name }}"
|
||||
data-lightbox="{{ item.id }}">
|
||||
<img src="{{ item.picture|thumbnail_url:'productlist' }}"
|
||||
alt="{{ item.name }}"/>
|
||||
</a>
|
||||
{% endif %}
|
||||
<strong>{{ item.name }}</strong>
|
||||
{% if item.description %}
|
||||
<p class="description">{{ item.description }}</p>{% endif %}
|
||||
</div>
|
||||
<div class="col-md-2 col-xs-6 price">
|
||||
{% if item.free_price %}
|
||||
<div class="input-group input-group-price">
|
||||
<span class="input-group-addon">{{ event.currency }}</span>
|
||||
<input type="number" class="form-control input-item-price" placeholder="0"
|
||||
min="{{ item.price|stringformat:"0.2f" }}"
|
||||
name="price_{{ item.id }}"
|
||||
step="0.01" value="{{ item.price|stringformat:"0.2f" }}">
|
||||
</div>
|
||||
{% else %}
|
||||
{{ event.currency }} {{ item.price|floatformat:2 }}
|
||||
{% endif %}
|
||||
{% if item.tax_rate %}
|
||||
<small>{% blocktrans trimmed with rate=item.tax_rate %}
|
||||
incl. {{ rate }}% taxes
|
||||
{% endblocktrans %}</small>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% if item.cached_availability.0 == 100 %}
|
||||
<div class="col-md-2 col-xs-6 availability-box available radio-box">
|
||||
<label>
|
||||
<input type="radio" name="_voucher_item"
|
||||
{% if options == 1 %}checked="checked"{% endif %}
|
||||
value="item_{{ item.id }}">
|
||||
</label>
|
||||
</div>
|
||||
{% else %}
|
||||
{% include "pretixpresale/event/fragment_availability.html" with avail=item.cached_availability.0 %}
|
||||
{% endif %}
|
||||
<div class="clearfix"></div>
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</section>
|
||||
{% endfor %}
|
||||
{% if event.presale_is_running %}
|
||||
<div class="row-fluid checkout-button-row">
|
||||
<div class="col-md-4 col-md-offset-8 col-xs-12">
|
||||
<button class="btn btn-block btn-primary btn-lg" type="submit">
|
||||
<i class="fa fa-shopping-cart"></i> {% trans "Add to cart" %}
|
||||
</button>
|
||||
</div>
|
||||
<div class="clearfix"></div>
|
||||
</div>
|
||||
{% endif %}
|
||||
</form>
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
@@ -14,6 +14,8 @@ event_patterns = [
|
||||
url(r'^cart/add$', pretix.presale.views.cart.CartAdd.as_view(), name='event.cart.add'),
|
||||
url(r'^cart/remove$', pretix.presale.views.cart.CartRemove.as_view(), name='event.cart.remove'),
|
||||
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(),
|
||||
name='event.redeem'),
|
||||
url(r'^checkout/(?P<step>[^/]+)/$', pretix.presale.views.checkout.CheckoutView.as_view(),
|
||||
name='event.checkout'),
|
||||
url(r'^order/(?P<order>[^/]+)/(?P<secret>[A-Za-z0-9]+)/$', pretix.presale.views.order.OrderDetails.as_view(),
|
||||
|
||||
@@ -1,15 +1,19 @@
|
||||
from django.contrib import messages
|
||||
from django.db.models import Count, Q
|
||||
from django.http import JsonResponse
|
||||
from django.shortcuts import redirect
|
||||
from django.utils.timezone import now
|
||||
from django.utils.translation import ugettext as _
|
||||
from django.views.generic import View
|
||||
from django.views.generic import TemplateView, View
|
||||
|
||||
from pretix.base.models import Quota, Voucher
|
||||
from pretix.base.services.cart import (
|
||||
CartError, add_items_to_cart, remove_items_from_cart,
|
||||
)
|
||||
from pretix.multidomain.urlreverse import eventreverse
|
||||
from pretix.presale.views import EventViewMixin
|
||||
from pretix.presale.views.async import AsyncAction
|
||||
from pretix.presale.views.event import item_group_by_category
|
||||
|
||||
|
||||
class CartActionMixin:
|
||||
@@ -26,30 +30,58 @@ class CartActionMixin:
|
||||
def get_error_url(self):
|
||||
return self.get_next_url()
|
||||
|
||||
def _items_from_post_data(self, warn=True):
|
||||
def _items_from_post_data(self):
|
||||
"""
|
||||
Parses the POST data and returns a list of tuples in the
|
||||
form (item id, variation id or None, number)
|
||||
"""
|
||||
|
||||
# Compatibility patch that makes the frontend code a lot easier
|
||||
req_items = list(self.request.POST.items())
|
||||
if '_voucher_item' in self.request.POST and '_voucher_code' in self.request.POST:
|
||||
req_items.append((
|
||||
'%s_voucher' % self.request.POST['_voucher_item'], self.request.POST['_voucher_code']
|
||||
))
|
||||
pass
|
||||
|
||||
items = []
|
||||
for key, value in self.request.POST.items():
|
||||
for key, value in req_items:
|
||||
if value.strip() == '' or '_' not in key:
|
||||
continue
|
||||
|
||||
price = self.request.POST.get('price_' + key.split("_", 1)[1], "")
|
||||
parts = key.split("_")
|
||||
if parts[-1] == "voucher":
|
||||
voucher = value
|
||||
value = 1
|
||||
parts = parts[:-1]
|
||||
else:
|
||||
voucher = None
|
||||
price = self.request.POST.get('price_' + "_".join(parts[1:]), "")
|
||||
if key.startswith('item_'):
|
||||
try:
|
||||
items.append((int(key.split("_")[1]), None, int(value), price))
|
||||
items.append({
|
||||
'item': int(parts[1]),
|
||||
'variation': None,
|
||||
'count': int(value),
|
||||
'price': price,
|
||||
'voucher': voucher
|
||||
})
|
||||
except ValueError:
|
||||
messages.error(self.request, _('Please enter numbers only.'))
|
||||
return []
|
||||
elif key.startswith('variation_'):
|
||||
try:
|
||||
items.append((int(key.split("_")[1]), int(key.split("_")[2]), int(value), price))
|
||||
items.append({
|
||||
'item': int(parts[1]),
|
||||
'variation': int(parts[2]),
|
||||
'count': int(value),
|
||||
'price': price,
|
||||
'voucher': voucher
|
||||
})
|
||||
except ValueError:
|
||||
messages.error(self.request, _('Please enter numbers only.'))
|
||||
return []
|
||||
if len(items) == 0 and warn:
|
||||
if len(items) == 0:
|
||||
messages.warning(self.request, _('You did not select any products.'))
|
||||
return []
|
||||
return items
|
||||
@@ -95,11 +127,9 @@ class CartAdd(EventViewMixin, CartActionMixin, AsyncAction, View):
|
||||
return super().get_error_message(exception)
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
voucher = self.request.POST.get('voucher')
|
||||
items = self._items_from_post_data(warn=not voucher)
|
||||
if items or voucher:
|
||||
return self.do(self.request.event.id, items, self.request.session.session_key,
|
||||
voucher)
|
||||
items = self._items_from_post_data()
|
||||
if items:
|
||||
return self.do(self.request.event.id, items, self.request.session.session_key)
|
||||
else:
|
||||
if 'ajax' in self.request.GET or 'ajax' in self.request.POST:
|
||||
return JsonResponse({
|
||||
@@ -107,3 +137,100 @@ class CartAdd(EventViewMixin, CartActionMixin, AsyncAction, View):
|
||||
})
|
||||
else:
|
||||
return redirect(self.get_error_url())
|
||||
|
||||
|
||||
class RedeemView(EventViewMixin, TemplateView):
|
||||
template_name = "pretixpresale/event/voucher.html"
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
|
||||
context['voucher'] = self.voucher
|
||||
|
||||
# Fetch all items
|
||||
items = self.request.event.items.all().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()))
|
||||
)
|
||||
|
||||
if self.voucher.item_id:
|
||||
items = items.filter(pk=self.voucher.item_id)
|
||||
elif self.voucher.quota_id:
|
||||
items = items.filter(quotas__in=[self.voucher.quota_id])
|
||||
|
||||
items = items.select_related(
|
||||
'category', # for re-grouping
|
||||
).prefetch_related(
|
||||
'quotas', 'variations__quotas', 'quotas__event' # for .availability()
|
||||
).annotate(quotac=Count('quotas')).filter(
|
||||
quotac__gt=0
|
||||
).distinct().order_by('category__position', 'category_id', 'position', 'name')
|
||||
|
||||
for item in items:
|
||||
item.available_variations = list(item.variations.filter(active=True, quotas__isnull=False).distinct())
|
||||
if self.voucher.item_id and self.voucher.variation_id:
|
||||
item.available_variations = [v for v in item.available_variations if v.pk == self.voucher.variation_id]
|
||||
|
||||
item.has_variations = item.variations.exists()
|
||||
if not item.has_variations:
|
||||
if self.voucher.allow_ignore_quota or self.voucher.block_quota:
|
||||
item.cached_availability = (Quota.AVAILABILITY_OK, 1)
|
||||
else:
|
||||
item.cached_availability = item.check_quotas()
|
||||
if self.voucher.price is not None:
|
||||
item.price = self.voucher.price
|
||||
else:
|
||||
item.price = item.default_price
|
||||
else:
|
||||
for var in item.available_variations:
|
||||
if self.voucher.allow_ignore_quota or self.voucher.block_quota:
|
||||
var.cached_availability = (Quota.AVAILABILITY_OK, 1)
|
||||
else:
|
||||
var.cached_availability = list(var.check_quotas())
|
||||
if self.voucher.price is not None:
|
||||
var.price = self.voucher.price
|
||||
else:
|
||||
var.price = var.default_price if var.default_price is not None else item.default_price
|
||||
|
||||
if len(item.available_variations) > 0:
|
||||
item.min_price = min([v.price for v in item.available_variations])
|
||||
item.max_price = max([v.price for v in item.available_variations])
|
||||
|
||||
items = [item for item in items if len(item.available_variations) > 0 or not item.has_variations]
|
||||
context['options'] = sum([(len(item.available_variations) if item.has_variations else 1)
|
||||
for item in items])
|
||||
|
||||
# Regroup those by category
|
||||
context['items_by_category'] = item_group_by_category(items)
|
||||
|
||||
return context
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
from pretix.base.services.cart import error_messages
|
||||
|
||||
err = None
|
||||
v = request.GET.get('voucher')
|
||||
|
||||
if v:
|
||||
try:
|
||||
self.voucher = Voucher.objects.get(code=v, event=request.event)
|
||||
if self.voucher.redeemed:
|
||||
err = error_messages['voucher_redeemed']
|
||||
if self.voucher.valid_until is not None and self.voucher.valid_until < now():
|
||||
err = error_messages['voucher_expired']
|
||||
except Voucher.DoesNotExist:
|
||||
err = error_messages['voucher_invalid']
|
||||
else:
|
||||
return redirect(eventreverse(request.event, 'presale:event.index'))
|
||||
|
||||
if request.event.presale_start and now() < request.event.presale_start:
|
||||
err = error_messages['not_started']
|
||||
if request.event.presale_end and now() > request.event.presale_end:
|
||||
err = error_messages['ended']
|
||||
|
||||
if err:
|
||||
messages.error(request, err)
|
||||
return redirect(eventreverse(request.event, 'presale:event.index'))
|
||||
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
|
||||
@@ -4,7 +4,21 @@ from django.db.models import Count, Q
|
||||
from django.utils.timezone import now
|
||||
from django.views.generic import TemplateView
|
||||
|
||||
from pretix.presale.views import CartMixin, EventViewMixin
|
||||
from . import CartMixin, EventViewMixin
|
||||
|
||||
|
||||
def item_group_by_category(items):
|
||||
return sorted(
|
||||
[
|
||||
# a group is a tuple of a category and a list of items
|
||||
(cat, [i for i in items if i.category == cat])
|
||||
for cat in set([i.category for i in items])
|
||||
# insert categories into a set for uniqueness
|
||||
# a set is unsorted, so sort again by category
|
||||
],
|
||||
key=lambda group: (group[0].position, group[0].id) if (
|
||||
group[0] is not None and group[0].id is not None) else (0, 0)
|
||||
)
|
||||
|
||||
|
||||
class EventIndex(EventViewMixin, CartMixin, TemplateView):
|
||||
@@ -48,17 +62,7 @@ class EventIndex(EventViewMixin, CartMixin, TemplateView):
|
||||
items = [item for item in items if len(item.available_variations) > 0 or not item.has_variations]
|
||||
|
||||
# Regroup those by category
|
||||
context['items_by_category'] = sorted(
|
||||
[
|
||||
# a group is a tuple of a category and a list of items
|
||||
(cat, [i for i in items if i.category == cat])
|
||||
for cat in set([i.category for i in items])
|
||||
# insert categories into a set for uniqueness
|
||||
# a set is unsorted, so sort again by category
|
||||
],
|
||||
key=lambda group: (group[0].position, group[0].id) if (
|
||||
group[0] is not None and group[0].id is not None) else (0, 0)
|
||||
)
|
||||
context['items_by_category'] = item_group_by_category(items)
|
||||
|
||||
vouchers_exist = self.request.event.get_cache().get('vouchers_exist')
|
||||
if vouchers_exist is None:
|
||||
|
||||
Reference in New Issue
Block a user