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:
parent
711e73a8b2
commit
e57eafaf9e
|
@ -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.decorators import login_required
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.contrib.auth.forms import UserCreationForm
|
from django.contrib.auth.forms import UserCreationForm
|
||||||
|
from django.db import transaction
|
||||||
from webshop.models import (Article, Category, ArticleStatus, Person,
|
from webshop.models import (Article, Category, ArticleStatus, Person,
|
||||||
City, Picture, CartPosition, ShoppingCart)
|
City, Picture, CartPosition, ShoppingCart)
|
||||||
from webshop.forms import RegistrationForm, AddToCartForm
|
from webshop.forms import RegistrationForm, AddToCartForm
|
||||||
|
@ -200,6 +201,7 @@ def registration(request):
|
||||||
profile_form = RegistrationForm(request.POST)
|
profile_form = RegistrationForm(request.POST)
|
||||||
user_form = UserCreationForm(request.POST)
|
user_form = UserCreationForm(request.POST)
|
||||||
if (profile_form.is_valid() and user_form.is_valid()):
|
if (profile_form.is_valid() and user_form.is_valid()):
|
||||||
|
with transaction.atomic():
|
||||||
pf = profile_form.cleaned_data
|
pf = profile_form.cleaned_data
|
||||||
uf = user_form.cleaned_data
|
uf = user_form.cleaned_data
|
||||||
user = User.objects.create_user(uf['username'],
|
user = User.objects.create_user(uf['username'],
|
||||||
|
|
Loading…
Reference in New Issue