mirror of
https://github.com/pretix/pretix.git
synced 2026-05-06 15:24:02 +00:00
Added signals to VoucherBulkForm to ease extension
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import copy
|
||||
|
||||
from django import forms
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
@@ -107,3 +109,17 @@ class VoucherBulkForm(VoucherForm):
|
||||
raise ValidationError(_('A voucher with one of this codes already exists.'))
|
||||
|
||||
return data
|
||||
|
||||
def save(self, event, *args, **kwargs):
|
||||
objs = []
|
||||
for code in self.cleaned_data['codes']:
|
||||
obj = copy.copy(self.instance)
|
||||
obj.event = event
|
||||
obj.code = code
|
||||
data = dict(self.cleaned_data)
|
||||
data['code'] = code
|
||||
data['bulk'] = True
|
||||
del data['codes']
|
||||
obj.save()
|
||||
objs.append(obj)
|
||||
return objs
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
{% extends "pretixcontrol/items/base.html" %}
|
||||
{% load i18n %}
|
||||
{% load eventsignal %}
|
||||
{% load bootstrap3 %}
|
||||
{% block title %}{% trans "Voucher" %}{% endblock %}
|
||||
{% block inside %}
|
||||
@@ -48,6 +49,7 @@
|
||||
{% bootstrap_field form.tag layout="horizontal" %}
|
||||
{% bootstrap_field form.comment layout="horizontal" %}
|
||||
</fieldset>
|
||||
{% eventsignal request.event "pretix.control.signals.voucher_form_html" form=form %}
|
||||
<div class="form-group submit-group">
|
||||
<button type="submit" class="btn btn-primary btn-save">
|
||||
{% trans "Save" %}
|
||||
|
||||
@@ -144,7 +144,6 @@ class VoucherCreate(EventPermissionRequiredMixin, CreateView):
|
||||
|
||||
class VoucherBulkCreate(EventPermissionRequiredMixin, CreateView):
|
||||
model = Voucher
|
||||
form_class = VoucherBulkForm
|
||||
template_name = 'pretixcontrol/vouchers/bulk.html'
|
||||
permission = 'can_change_vouchers'
|
||||
context_object_name = 'voucher'
|
||||
@@ -162,16 +161,14 @@ class VoucherBulkCreate(EventPermissionRequiredMixin, CreateView):
|
||||
|
||||
@transaction.atomic()
|
||||
def form_valid(self, form):
|
||||
for code in form.cleaned_data['codes']:
|
||||
obj = copy.copy(form.instance)
|
||||
obj.event = self.request.event
|
||||
obj.code = code
|
||||
data = dict(form.cleaned_data)
|
||||
data['code'] = code
|
||||
data['bulk'] = True
|
||||
del data['codes']
|
||||
obj.save()
|
||||
obj.log_action('pretix.voucher.added', data=data, user=self.request.user)
|
||||
|
||||
for o in form.save(self.request.event):
|
||||
o.log_action('pretix.voucher.added', data=form.cleaned_data, user=self.request.user)
|
||||
messages.success(self.request, _('The new vouchers have been created.'))
|
||||
return HttpResponseRedirect(self.get_success_url())
|
||||
|
||||
def get_form_class(self):
|
||||
form_class = VoucherBulkForm
|
||||
for receiver, response in voucher_form_class.send(self.request.event, cls=form_class):
|
||||
if response:
|
||||
form_class = response
|
||||
return form_class
|
||||
|
||||
Reference in New Issue
Block a user