def clean_email(self):
     """
     Validate that the supplied email address is unique for the
     site.
     
     """
     if User.get_user_by_email(self.cleaned_data['email'], is_active=None):
         raise forms.ValidationError(_("This email address is already in use. Please supply a different email address."))
     return self.cleaned_data['email']
    def clean_username(self):
        """
        Validate that the username is alphanumeric and is not already
        in use.

        """
        user = User.get_user(self.cleaned_data['username'], is_active=None)
        if user is None:
            return self.cleaned_data['username']
        raise forms.ValidationError(_("A user with that username already exists."))
 def register(self, request, **kwargs):
     """
     Create and immediately log in a new user.
     
     """
     username, email, password = kwargs['username'], kwargs['email'], kwargs['password1']
     user = User()
     user.username = username
     user.email = email
     user.set_password(password)
     user.save()
     
     # authenticate() always has to be called before login(), and
     # will return the user we just created.
     new_user = authenticate(username=username, password=password)
     login(request, new_user)
     return new_user