Exemplo n.º 1
0
def forgot_username(request, template):
    """Forgot username form page.

    On POST, this view sends an email with the username.
    """
    if request.method == "POST":
        form = ForgotUsernameForm(request.POST)
        was_valid = form.is_valid()
        if was_valid:
            try_send_email_with_form(form.save, form, "email", use_https=request.is_secure())

        # Form may now be invalid if email failed to send.
        # ForgotUsernameForm is invalid iff there is no user with the entered
        # email address.
        # The condition below ensures we don't leak existence of email address
        # _unless_ sending an email fails.
        if form.is_valid() or not was_valid:
            # Don't leak existence of email addresses.
            messages.add_message(
                request,
                messages.INFO,
                _(u"We've sent an email with the username to any account" " using {email}.").format(
                    email=form.data["email"]
                ),
            )

            return HttpResponseRedirect(reverse("users.login"))
    else:
        form = ForgotUsernameForm()

    return render(request, template, {"form": form})
Exemplo n.º 2
0
def forgot_username(request, template):
    """Forgot username form page.

    On POST, this view sends an email with the username.
    """
    if request.method == "POST":
        form = ForgotUsernameForm(request.POST)
        was_valid = form.is_valid()
        if was_valid:
            try_send_email_with_form(form.save,
                                     form,
                                     'email',
                                     use_https=request.is_secure())

        # Form may now be invalid if email failed to send.
        # ForgotUsernameForm is invalid iff there is no user with the entered
        # email address.
        # The condition below ensures we don't leak existence of email address
        # _unless_ sending an email fails.
        if form.is_valid() or not was_valid:
            # Don't leak existence of email addresses.
            messages.add_message(
                request, messages.INFO,
                _(u"We've sent an email with the username to any account"
                  u" using {email}.").format(email=form.data['email']))

            return HttpResponseRedirect(reverse('users.login'))
    else:
        form = ForgotUsernameForm()

    return render(request, template, {'form': form})
Exemplo n.º 3
0
def resend_confirmation(request, template):
    """Resend confirmation email."""
    if request.method == 'POST':
        form = EmailConfirmationForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data['email']
            try:
                reg_prof = RegistrationProfile.objects.get(user__email=email)
                if not reg_prof.user.is_active:
                    form = try_send_email_with_form(
                        RegistrationProfile.objects.send_confirmation_email,
                        form, 'email', reg_prof)
                else:
                    form = try_send_email_with_form(
                        RegistrationProfile.objects.send_confirmation_email,
                        form,
                        'email',
                        reg_prof,
                        text_template='users/email/already_activated.ltxt',
                        html_template='users/email/already_activated.html',
                        subject=_('Account already activated'))
            except RegistrationProfile.DoesNotExist:
                # Send already active email if user exists
                try:
                    user = User.objects.get(email=email, is_active=True)

                    current_site = Site.objects.get_current()
                    email_kwargs = {
                        'domain': current_site.domain,
                        'login_url': reverse('users.login')
                    }

                    subject = _('Account already activated')

                    @email_utils.safe_translation
                    def _make_mail(locale):
                        mail = email_utils.make_mail(
                            subject=subject,
                            text_template='users/email/already_activated.ltxt',
                            html_template='users/email/already_activated.html',
                            context_vars=email_kwargs,
                            from_email=settings.DEFAULT_FROM_EMAIL,
                            to_email=user.email)

                        return mail

                    email_utils.send_messages(
                        [_make_mail(request.LANGUAGE_CODE)])
                except User.DoesNotExist:
                    # Don't leak existence of email addresses.
                    pass
            # Form may now be invalid if email failed to send.
            if form.is_valid():
                return render(request,
                              template + 'resend_confirmation_done.html',
                              {'email': email})
    else:
        form = EmailConfirmationForm()
    return render(request, template + 'resend_confirmation.html',
                  {'form': form})
Exemplo n.º 4
0
def resend_confirmation(request, template):
    """Resend confirmation email."""
    if request.method == 'POST':
        form = EmailConfirmationForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data['email']
            try:
                reg_prof = RegistrationProfile.objects.get(
                    user__email=email)
                if not reg_prof.user.is_active:
                    form = try_send_email_with_form(
                        RegistrationProfile.objects.send_confirmation_email,
                        form, 'email',
                        reg_prof)
                else:
                    form = try_send_email_with_form(
                        RegistrationProfile.objects.send_confirmation_email,
                        form, 'email',
                        reg_prof,
                        text_template='users/email/already_activated.ltxt',
                        html_template='users/email/already_activated.html',
                        subject=_('Account already activated'))
            except RegistrationProfile.DoesNotExist:
                # Send already active email if user exists
                try:
                    user = User.objects.get(email=email, is_active=True)

                    current_site = Site.objects.get_current()
                    email_kwargs = {'domain': current_site.domain,
                                    'login_url': reverse('users.login')}

                    subject = _('Account already activated')

                    @email_utils.safe_translation
                    def _make_mail(locale):
                        mail = email_utils.make_mail(
                            subject=subject,
                            text_template='users/email/already_activated.ltxt',
                            html_template='users/email/already_activated.html',
                            context_vars=email_kwargs,
                            from_email=settings.DEFAULT_FROM_EMAIL,
                            to_email=user.email)

                        return mail

                    email_utils.send_messages(
                        [_make_mail(request.LANGUAGE_CODE)])
                except User.DoesNotExist:
                    # Don't leak existence of email addresses.
                    pass
            # Form may now be invalid if email failed to send.
            if form.is_valid():
                return render(
                    request, template + 'resend_confirmation_done.html',
                    {'email': email})
    else:
        form = EmailConfirmationForm()
    return render(request, template + 'resend_confirmation.html', {
        'form': form})
Exemplo n.º 5
0
def password_reset(request, template):
    """Password reset form.

    Based on django.contrib.auth.views. This view sends the email.

    """
    if request.method == "POST":
        form = PasswordResetForm(request.POST)
        was_valid = form.is_valid()
        if was_valid:
            # TODO: We aren't using Jingo anymore, but I'm not sure what
            # to do with the below.
            #
            # TODO: Since we're using Jingo in a way that doesn't
            # override the Django template loader, the pw_reset.ltxt
            # email template must be a Django template and not a Jinja
            # template.
            #
            # After we switch all the rendering everywhere, we can
            # probably change this back. Until then, I'm pretty sure
            # this won't get translated.
            try_send_email_with_form(
                form.save,
                form,
                "email",
                use_https=request.is_secure(),
                token_generator=default_token_generator,
                text_template="users/email/pw_reset.ltxt",
                html_template="users/email/pw_reset.html",
                subject_template_name="users/email/pw_reset_subject.ltxt",
            )
        # Form may now be invalid if email failed to send.
        # PasswordResetForm is invalid iff there is no user with the entered
        # email address.
        # The condition below ensures we don't leak existence of email address
        # _unless_ sending an email fails.
        if form.is_valid() or not was_valid:
            # Don't leak existence of email addresses.
            return HttpResponseRedirect(reverse("users.pw_reset_sent"))
    else:
        form = PasswordResetForm()

    return render(request, template, {"form": form})
Exemplo n.º 6
0
def password_reset(request):
    """Password reset form.

    Based on django.contrib.auth.views. This view sends the email.

    """
    if request.method == "POST":
        form = PasswordResetForm(request.POST)
        was_valid = form.is_valid()
        if was_valid:
            # TODO: We aren't using Jingo anymore, but I'm not sure what
            # to do with the below.
            #
            # TODO: Since we're using Jingo in a way that doesn't
            # override the Django template loader, the pw_reset.ltxt
            # email template must be a Django template and not a Jinja
            # template.
            #
            # After we switch all the rendering everywhere, we can
            # probably change this back. Until then, I'm pretty sure
            # this won't get translated.
            try_send_email_with_form(
                form.save,
                form,
                'email',
                use_https=request.is_secure(),
                token_generator=default_token_generator,
                text_template='users/email/pw_reset.ltxt',
                html_template='users/email/pw_reset.html',
                subject_template_name='users/email/pw_reset_subject.ltxt')
        # Form may now be invalid if email failed to send.
        # PasswordResetForm is invalid iff there is no user with the entered
        # email address.
        # The condition below ensures we don't leak existence of email address
        # _unless_ sending an email fails.
        if form.is_valid() or not was_valid:
            # Don't leak existence of email addresses.
            return HttpResponseRedirect(reverse('users.pw_reset_sent'))
    else:
        form = PasswordResetForm()

    return render(request, 'users/pw_reset_form.html', {'form': form})