From 777a616921aa07686bbb42a61361bf00494f611a Mon Sep 17 00:00:00 2001 From: Luca Date: Sun, 24 Apr 2022 03:25:05 +0200 Subject: [PATCH] Wrap helper and token creation in transaction --- shiftregister/app/views.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/shiftregister/app/views.py b/shiftregister/app/views.py index c577d48..da806dd 100644 --- a/shiftregister/app/views.py +++ b/shiftregister/app/views.py @@ -1,5 +1,6 @@ from django.shortcuts import render, redirect, get_object_or_404 from .models import Shift, LoginToken, Helper, ShiftRegistration +from django.db import transaction from django.db.models import F, Count, Q, ExpressionWrapper from .forms import RegisterForm, EmptyForm from django.db.models.fields import DateTimeField @@ -118,8 +119,10 @@ def register(request): helper = Helper( name=form.cleaned_data["name"], phone=form.cleaned_data["phone"] ) - helper.save() - token = helper.send_confirmation() + with transaction.atomic(): + helper.save() + token = helper.send_confirmation() + request.session["token"] = token.pk if settings.DEBUG: messages.add_message(