forked from CGM_Public/pretix_original
Improve performance of voucher bulk creation
This commit is contained in:
@@ -36,7 +36,7 @@ def cached_file_delete(sender, instance, **kwargs):
|
||||
|
||||
class LoggingMixin:
|
||||
|
||||
def log_action(self, action, data=None, user=None, api_token=None):
|
||||
def log_action(self, action, data=None, user=None, api_token=None, save=True):
|
||||
"""
|
||||
Create a LogEntry object that is related to this object.
|
||||
See the LogEntry documentation for details.
|
||||
@@ -60,10 +60,12 @@ class LoggingMixin:
|
||||
logentry = LogEntry(content_object=self, user=user, action_type=action, event=event, api_token=api_token)
|
||||
if data:
|
||||
logentry.data = json.dumps(data, cls=CustomJSONEncoder)
|
||||
logentry.save()
|
||||
if save:
|
||||
logentry.save()
|
||||
|
||||
if action in get_all_notification_types():
|
||||
notify.apply_async(args=(logentry.pk,))
|
||||
if action in get_all_notification_types():
|
||||
notify.apply_async(args=(logentry.pk,))
|
||||
return logentry
|
||||
|
||||
|
||||
class LoggedModel(models.Model, LoggingMixin):
|
||||
|
||||
@@ -179,6 +179,6 @@ class VoucherBulkForm(VoucherForm):
|
||||
data['code'] = code
|
||||
data['bulk'] = True
|
||||
del data['codes']
|
||||
obj.save()
|
||||
objs.append(obj)
|
||||
Voucher.objects.bulk_create(objs)
|
||||
return objs
|
||||
|
||||
@@ -16,7 +16,7 @@ from django.views.generic import (
|
||||
CreateView, DeleteView, ListView, TemplateView, UpdateView, View,
|
||||
)
|
||||
|
||||
from pretix.base.models import Voucher
|
||||
from pretix.base.models import LogEntry, Voucher
|
||||
from pretix.base.models.vouchers import _generate_random_code
|
||||
from pretix.control.forms.filter import VoucherFilterForm
|
||||
from pretix.control.forms.vouchers import VoucherBulkForm, VoucherForm
|
||||
@@ -244,8 +244,15 @@ class VoucherBulkCreate(EventPermissionRequiredMixin, CreateView):
|
||||
|
||||
@transaction.atomic
|
||||
def form_valid(self, form):
|
||||
for o in form.save(self.request.event):
|
||||
o.log_action('pretix.voucher.added', data=form.cleaned_data, user=self.request.user)
|
||||
log_entries = []
|
||||
form.save(self.request.event)
|
||||
# We need to query them again as form.save() uses bulk_create which does not fill in .pk values on databases
|
||||
# other than PostgreSQL
|
||||
for v in self.request.event.vouchers.filter(code__in=form.cleaned_data['codes']):
|
||||
log_entries.append(
|
||||
v.log_action('pretix.voucher.added', data=form.cleaned_data, user=self.request.user, save=False)
|
||||
)
|
||||
LogEntry.objects.bulk_create(log_entries)
|
||||
messages.success(self.request, _('The new vouchers have been created.'))
|
||||
return HttpResponseRedirect(self.get_success_url())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user