Atomic transaction on registering users
This commit is contained in:
parent
1a4f9a8eae
commit
5b42a3b105
|
@ -1,5 +1,6 @@
|
||||||
from django import forms
|
from django import forms
|
||||||
from django.contrib.auth import authenticate
|
from django.contrib.auth import authenticate
|
||||||
|
from django.db import transaction
|
||||||
from django.contrib.auth.forms import (
|
from django.contrib.auth.forms import (
|
||||||
PasswordChangeForm as DjangoPasswordChangeForm
|
PasswordChangeForm as DjangoPasswordChangeForm
|
||||||
)
|
)
|
||||||
|
@ -51,7 +52,8 @@ class RegistrationForm(forms.ModelForm):
|
||||||
"""
|
"""
|
||||||
Custom for for registering an user
|
Custom for for registering an user
|
||||||
"""
|
"""
|
||||||
password1 = forms.CharField(label=_('Password'), widget=forms.PasswordInput)
|
password1 = forms.CharField(label=_('Password'),
|
||||||
|
widget=forms.PasswordInput)
|
||||||
password2 = forms.CharField(label=_('Repeat password'),
|
password2 = forms.CharField(label=_('Repeat password'),
|
||||||
widget=forms.PasswordInput)
|
widget=forms.PasswordInput)
|
||||||
access_code = forms.CharField(label=_('Invitation code'), required=True)
|
access_code = forms.CharField(label=_('Invitation code'), required=True)
|
||||||
|
@ -84,10 +86,11 @@ class RegistrationForm(forms.ModelForm):
|
||||||
|
|
||||||
def save(self, commit=True):
|
def save(self, commit=True):
|
||||||
# Save the provided password in hashed format
|
# Save the provided password in hashed format
|
||||||
user = super(UserCreationForm, self).save(commit=False)
|
with transaction.atomic():
|
||||||
access_code = self.get_access_code()
|
user = super(RegistrationForm, self).save(commit=False)
|
||||||
user.access_code = access_code
|
access_code = self.get_access_code()
|
||||||
user.set_password(self.cleaned_data["password1"])
|
user.access_code = access_code
|
||||||
if commit:
|
user.set_password(self.cleaned_data["password1"])
|
||||||
user.save()
|
if commit:
|
||||||
return user
|
user.save()
|
||||||
|
return user
|
||||||
|
|
Reference in New Issue