mirror of
https://github.com/pretix/pretix.git
synced 2026-05-06 15:24:02 +00:00
Added voucher list download as CSV
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
import csv
|
||||
import io
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib import messages
|
||||
from django.core.urlresolvers import resolve, reverse
|
||||
from django.db import transaction
|
||||
from django.db.models import Count, Q, Sum
|
||||
from django.http import Http404, HttpResponseRedirect
|
||||
from django.http import Http404, HttpResponse, HttpResponseRedirect
|
||||
from django.utils.formats import date_format
|
||||
from django.utils.timezone import now
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.views.generic import (
|
||||
@@ -41,6 +45,45 @@ class VoucherList(EventPermissionRequiredMixin, ListView):
|
||||
qs = qs.filter(Q(valid_until__isnull=False) & Q(valid_until__lt=now())).filter(redeemed=False)
|
||||
return qs
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
if request.GET.get("download", "") == "yes":
|
||||
return self._download_csv()
|
||||
return super().get(request, *args, **kwargs)
|
||||
|
||||
def _download_csv(self):
|
||||
output = io.StringIO()
|
||||
writer = csv.writer(output, quoting=csv.QUOTE_NONNUMERIC, delimiter=",")
|
||||
|
||||
headers = [
|
||||
_('Voucher code'), _('Valid until'), _('Product'), _('Reserve quota'), _('Bypass quota'),
|
||||
_('Price'), _('Tag'), _('Redeemed')
|
||||
]
|
||||
writer.writerow(headers)
|
||||
|
||||
for v in self.get_queryset():
|
||||
if v.item:
|
||||
if v.variation:
|
||||
prod = '%s – %s' % (str(v.item.name), str(v.variation.name))
|
||||
else:
|
||||
prod = '%s' % str(v.item.name)
|
||||
elif v.quota:
|
||||
prod = _('Any product in quota "{quota}"').format(quota=str(v.quota.name))
|
||||
row = [
|
||||
v.code,
|
||||
v.valid_until.isoformat() if v.valid_until else "",
|
||||
prod,
|
||||
_("Yes") if v.block_quota else _("No"),
|
||||
_("Yes") if v.allow_ignore_quota else _("No"),
|
||||
str(v.price) if v.price else "",
|
||||
v.tag,
|
||||
_("Yes") if v.redeemed else _("No"),
|
||||
]
|
||||
writer.writerow(row)
|
||||
|
||||
r = HttpResponse(output.getvalue().encode("utf-8"), content_type='text/csv')
|
||||
r['Content-Disposition'] = 'attachment; filename="vouchers.csv"'
|
||||
return r
|
||||
|
||||
|
||||
class VoucherTags(EventPermissionRequiredMixin, TemplateView):
|
||||
template_name = 'pretixcontrol/vouchers/tags.html'
|
||||
|
||||
Reference in New Issue
Block a user