wrap the registration in a transaction

Sometimes when the creation of the person object fails the user get's
created anyway resulting in a user without a user profile.
By wrapping both the user and profile creation in a transaction the
user gets only created if the person profile worked as well.
This commit is contained in:
Andreas Zweili 2018-02-05 21:25:18 +01:00
parent 711e73a8b2
commit e57eafaf9e
1 changed files with 17 additions and 15 deletions

View File

@ -3,6 +3,7 @@ from django.shortcuts import get_object_or_404, render
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from django.db import transaction
from webshop.models import (Article, Category, ArticleStatus, Person,
City, Picture, CartPosition, ShoppingCart)
from webshop.forms import RegistrationForm, AddToCartForm
@ -200,6 +201,7 @@ def registration(request):
profile_form = RegistrationForm(request.POST)
user_form = UserCreationForm(request.POST)
if (profile_form.is_valid() and user_form.is_valid()):
with transaction.atomic():
pf = profile_form.cleaned_data
uf = user_form.cleaned_data
user = User.objects.create_user(uf['username'],