Skip to content

Commit

Permalink
Ch22: Create Profile in UserCreationForm.
Browse files Browse the repository at this point in the history
  • Loading branch information
jambonrose committed Jul 30, 2015
1 parent 23e8196 commit 6b8d9ef
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions user/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import \
UserCreationForm as BaseUserCreationForm
from django.core.exceptions import ValidationError
from django.utils.text import slugify

from .models import Profile
from .utils import ActivationMailFormMixin

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -46,6 +49,23 @@ class Meta(BaseUserCreationForm.Meta):
model = get_user_model()
fields = ('username', 'email')

def clean_username(self):
username = self.cleaned_data['username']
disallowed = (
'activate',
'create',
'disable',
'login',
'logout',
'password',
'profile',
)
if username in disallowed:
raise ValidationError(
"A user with that username"
" already exists.")
return username

def save(self, **kwargs):
user = super().save(commit=False)
if not user.pk:
Expand All @@ -55,6 +75,12 @@ def save(self, **kwargs):
send_mail = False
user.save()
self.save_m2m()
Profile.objects.update_or_create(
user=user,
defaults={
'slug': slugify(
user.get_username()),
})
if send_mail:
self.send_mail(user=user, **kwargs)
return user

0 comments on commit 6b8d9ef

Please sign in to comment.