Add Subevent-Filter for Voucher-Tags (#1407)

* Add Subevent-Filter for Voucher-Tags

* Filter Subevent Voucher-Tags with proper Filter

* Apply filter before annotating totals and usage
This commit is contained in:
Martin Gross
2019-09-16 14:08:23 +02:00
committed by Raphael Michel
parent a884a25d2b
commit cf14dcf889
3 changed files with 77 additions and 6 deletions
+37
View File
@@ -902,6 +902,43 @@ class VoucherFilterForm(FilterForm):
return qs return qs
class VoucherTagFilterForm(FilterForm):
subevent = forms.ModelChoiceField(
label=pgettext_lazy('subevent', 'Date'),
queryset=SubEvent.objects.none(),
required=False,
empty_label=pgettext_lazy('subevent', 'All dates')
)
def __init__(self, *args, **kwargs):
self.event = kwargs.pop('event')
super().__init__(*args, **kwargs)
if self.event.has_subevents:
self.fields['subevent'].queryset = self.event.subevents.all()
self.fields['subevent'].widget = Select2(
attrs={
'data-model-select2': 'event',
'data-select2-url': reverse('control:event.subevents.select2', kwargs={
'event': self.event.slug,
'organizer': self.event.organizer.slug,
}),
'data-placeholder': pgettext_lazy('subevent', 'All dates')
}
)
self.fields['subevent'].widget.choices = self.fields['subevent'].choices
elif 'subevent':
del self.fields['subevent']
def filter_qs(self, qs):
fdata = self.cleaned_data
if fdata.get('subevent'):
qs = qs.filter(subevent_id=fdata.get('subevent').pk)
return qs
class RefundFilterForm(FilterForm): class RefundFilterForm(FilterForm):
provider = forms.ChoiceField( provider = forms.ChoiceField(
label=_('Payment provider'), label=_('Payment provider'),
@@ -1,5 +1,6 @@
{% extends "pretixcontrol/event/base.html" %} {% extends "pretixcontrol/event/base.html" %}
{% load i18n %} {% load i18n %}
{% load bootstrap3 %}
{% block title %}{% trans "Voucher tags" %}{% endblock %} {% block title %}{% trans "Voucher tags" %}{% endblock %}
{% block content %} {% block content %}
<h1>{% trans "Voucher tags" %}</h1> <h1>{% trans "Voucher tags" %}</h1>
@@ -8,6 +9,23 @@
If you add a "tag" to a voucher, you can here see statistics on their usage. If you add a "tag" to a voucher, you can here see statistics on their usage.
{% endblocktrans %} {% endblocktrans %}
</p> </p>
{% if request.event.has_subevents %}
<div class="row filter-form">
<form class="" action="" method="get">
<div class="col-lg-2 col-sm-3 col-xs-6">
{% bootstrap_field filter_form.subevent layout='inline' %}
</div>
<div class="col-lg-1 col-sm-6 col-xs-6">
<button class="btn btn-primary btn-block" type="submit">
<span class="fa fa-filter"></span>
<span class="hidden-md">
{% trans "Filter" %}
</span>
</button>
</div>
</form>
</div>
{% endif %}
{% if tags|length == 0 %} {% if tags|length == 0 %}
<div class="empty-collection"> <div class="empty-collection">
<p> <p>
+22 -6
View File
@@ -19,7 +19,7 @@ from django.views.generic import (
from pretix.base.models import CartPosition, LogEntry, OrderPosition, Voucher from pretix.base.models import CartPosition, LogEntry, OrderPosition, Voucher
from pretix.base.models.vouchers import _generate_random_code from pretix.base.models.vouchers import _generate_random_code
from pretix.control.forms.filter import VoucherFilterForm from pretix.control.forms.filter import VoucherFilterForm, VoucherTagFilterForm
from pretix.control.forms.vouchers import VoucherBulkForm, VoucherForm from pretix.control.forms.vouchers import VoucherBulkForm, VoucherForm
from pretix.control.permissions import EventPermissionRequiredMixin from pretix.control.permissions import EventPermissionRequiredMixin
from pretix.control.signals import voucher_form_class from pretix.control.signals import voucher_form_class
@@ -94,16 +94,27 @@ class VoucherTags(EventPermissionRequiredMixin, TemplateView):
template_name = 'pretixcontrol/vouchers/tags.html' template_name = 'pretixcontrol/vouchers/tags.html'
permission = 'can_view_vouchers' permission = 'can_view_vouchers'
def get_context_data(self, **kwargs): def get_queryset(self):
ctx = super().get_context_data(**kwargs) qs = self.request.event.vouchers.order_by('tag').filter(
tags = self.request.event.vouchers.order_by('tag').filter(
tag__isnull=False, tag__isnull=False,
waitinglistentries__isnull=True waitinglistentries__isnull=True
).values('tag').annotate( )
if self.filter_form.is_valid():
qs = self.filter_form.filter_qs(qs)
qs = qs.values('tag').annotate(
total=Sum('max_usages'), total=Sum('max_usages'),
redeemed=Sum('redeemed') redeemed=Sum('redeemed')
) )
return qs.distinct()
def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
tags = self.get_queryset()
for t in tags: for t in tags:
if t['total'] == 0: if t['total'] == 0:
t['percentage'] = 0 t['percentage'] = 0
@@ -111,8 +122,13 @@ class VoucherTags(EventPermissionRequiredMixin, TemplateView):
t['percentage'] = int((t['redeemed'] / t['total']) * 100) t['percentage'] = int((t['redeemed'] / t['total']) * 100)
ctx['tags'] = tags ctx['tags'] = tags
ctx['filter_form'] = self.filter_form
return ctx return ctx
@cached_property
def filter_form(self):
return VoucherTagFilterForm(data=self.request.GET, event=self.request.event)
class VoucherDelete(EventPermissionRequiredMixin, DeleteView): class VoucherDelete(EventPermissionRequiredMixin, DeleteView):
model = Voucher model = Voucher