mirror of
https://github.com/pretix/pretix.git
synced 2026-05-09 15:54:03 +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 import forms
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
from django.utils.translation import ugettext_lazy as _
|
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.'))
|
raise ValidationError(_('A voucher with one of this codes already exists.'))
|
||||||
|
|
||||||
return data
|
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" %}
|
{% extends "pretixcontrol/items/base.html" %}
|
||||||
{% load i18n %}
|
{% load i18n %}
|
||||||
|
{% load eventsignal %}
|
||||||
{% load bootstrap3 %}
|
{% load bootstrap3 %}
|
||||||
{% block title %}{% trans "Voucher" %}{% endblock %}
|
{% block title %}{% trans "Voucher" %}{% endblock %}
|
||||||
{% block inside %}
|
{% block inside %}
|
||||||
@@ -48,6 +49,7 @@
|
|||||||
{% bootstrap_field form.tag layout="horizontal" %}
|
{% bootstrap_field form.tag layout="horizontal" %}
|
||||||
{% bootstrap_field form.comment layout="horizontal" %}
|
{% bootstrap_field form.comment layout="horizontal" %}
|
||||||
</fieldset>
|
</fieldset>
|
||||||
|
{% eventsignal request.event "pretix.control.signals.voucher_form_html" form=form %}
|
||||||
<div class="form-group submit-group">
|
<div class="form-group submit-group">
|
||||||
<button type="submit" class="btn btn-primary btn-save">
|
<button type="submit" class="btn btn-primary btn-save">
|
||||||
{% trans "Save" %}
|
{% trans "Save" %}
|
||||||
|
|||||||
@@ -144,7 +144,6 @@ class VoucherCreate(EventPermissionRequiredMixin, CreateView):
|
|||||||
|
|
||||||
class VoucherBulkCreate(EventPermissionRequiredMixin, CreateView):
|
class VoucherBulkCreate(EventPermissionRequiredMixin, CreateView):
|
||||||
model = Voucher
|
model = Voucher
|
||||||
form_class = VoucherBulkForm
|
|
||||||
template_name = 'pretixcontrol/vouchers/bulk.html'
|
template_name = 'pretixcontrol/vouchers/bulk.html'
|
||||||
permission = 'can_change_vouchers'
|
permission = 'can_change_vouchers'
|
||||||
context_object_name = 'voucher'
|
context_object_name = 'voucher'
|
||||||
@@ -162,16 +161,14 @@ class VoucherBulkCreate(EventPermissionRequiredMixin, CreateView):
|
|||||||
|
|
||||||
@transaction.atomic()
|
@transaction.atomic()
|
||||||
def form_valid(self, form):
|
def form_valid(self, form):
|
||||||
for code in form.cleaned_data['codes']:
|
for o in form.save(self.request.event):
|
||||||
obj = copy.copy(form.instance)
|
o.log_action('pretix.voucher.added', data=form.cleaned_data, user=self.request.user)
|
||||||
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)
|
|
||||||
|
|
||||||
messages.success(self.request, _('The new vouchers have been created.'))
|
messages.success(self.request, _('The new vouchers have been created.'))
|
||||||
return HttpResponseRedirect(self.get_success_url())
|
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