def clean_email(self): """ Validate that the e-mail address is unique. """ if User.objects.filter(email__iexact=self.cleaned_data['email']): if get_signup_model().objects.filter(user__email__iexact=self.cleaned_data['email']).exclude(activation_key=userena_settings.USERENA_ACTIVATED): raise forms.ValidationError(_('This email is already in use but not confirmed. Please check you email for verification steps.')) raise forms.ValidationError(_('This email is already in use. Please supply a different email.')) return self.cleaned_data['email']
def save(self): """ Creates a new user and account. Returns the newly created user. """ username, email, password = (self.cleaned_data['username'], self.cleaned_data['email'], self.cleaned_data['password1']) new_user = get_signup_model().objects.create_user(username, email, password, not userena_settings.USERENA_ACTIVATION_REQUIRED, userena_settings.USERENA_ACTIVATION_REQUIRED) return new_user
def activate(request, activation_key, template_name='userena/activate_fail.html', success_url=None, extra_context=None): """ Activate a user with an activation key. The key is a SHA1 string. When the SHA1 is found with an :class:`UserenaSignup`, the :class:`User` of that account will be activated. After a successful activation the view will redirect to ``success_url``. If the SHA1 is not found, the user will be shown the ``template_name`` template displaying a fail message. :param activation_key: String of a SHA1 string of 40 characters long. A SHA1 is always 160bit long, with 4 bits per character this makes it --160/4-- 40 characters long. :param template_name: String containing the template name that is used when the ``activation_key`` is invalid and the activation fails. Defaults to ``userena/activation_fail.html``. :param success_url: String containing the URL where the user should be redirected to after a successful activation. Will replace ``%(username)s`` with string formatting if supplied. If ``success_url`` is left empty, will direct to ``userena_profile_detail`` view. :param extra_context: Dictionary containing variables which could be added to the template context. Default to an empty dictionary. """ user = get_signup_model().objects.activate_user(activation_key) if user: # Sign the user in. auth_user = authenticate(identification=user.email, check_password=False) login(request, auth_user) if userena_settings.USERENA_USE_MESSAGES: messages.success(request, _('Your account has been activated and you have been signed in.'), fail_silently=True) if success_url: redirect_to = success_url % {'username': user.username } else: redirect_to = reverse('userena_profile_detail', kwargs={'username': user.username}) return redirect(redirect_to) else: if not extra_context: extra_context = dict() return ExtraContextTemplateView.as_view(template_name=template_name, extra_context=extra_context)(request)
def email_confirm(request, confirmation_key, template_name='userena/email_confirm_fail.html', success_url=None, extra_context=None): """ Confirms an email address with a confirmation key. Confirms a new email address by running :func:`User.objects.confirm_email` method. If the method returns an :class:`User` the user will have his new e-mail address set and redirected to ``success_url``. If no ``User`` is returned the user will be represented with a fail message from ``template_name``. :param confirmation_key: String with a SHA1 representing the confirmation key used to verify a new email address. :param template_name: String containing the template name which should be rendered when confirmation fails. When confirmation is successful, no template is needed because the user will be redirected to ``success_url``. :param success_url: String containing the URL which is redirected to after a successful confirmation. Supplied argument must be able to be rendered by ``reverse`` function. :param extra_context: Dictionary of variables that are passed on to the template supplied by ``template_name``. """ user = get_signup_model().objects.confirm_email(confirmation_key) if user: if userena_settings.USERENA_USE_MESSAGES: messages.success(request, _('Your email address has been changed.'), fail_silently=True) if success_url: redirect_to = success_url else: redirect_to = reverse('userena_email_confirm_complete', kwargs={'username': user.username}) return redirect(redirect_to) else: if not extra_context: extra_context = dict() return ExtraContextTemplateView.as_view(template_name=template_name, extra_context=extra_context)(request)
def clean_username(self): """ Validate that the username is alphanumeric and is not already in use. Also validates that the username is not listed in ``USERENA_FORBIDDEN_USERNAMES`` list. """ try: user = User.objects.get(username__iexact=self.cleaned_data['username']) except User.DoesNotExist: pass else: if get_signup_model().objects.filter(user__username__iexact=self.cleaned_data['username']).exclude(activation_key=userena_settings.USERENA_ACTIVATED): raise forms.ValidationError(_('This username is already taken but not confirmed. Please check you email for verification steps.')) raise forms.ValidationError(_('This username is already taken.')) if self.cleaned_data['username'].lower() in userena_settings.USERENA_FORBIDDEN_USERNAMES: raise forms.ValidationError(_('This username is not allowed.')) return self.cleaned_data['username']