Beispiel #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})
Beispiel #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})
Beispiel #3
0
 def test_valid_email(self):
     """"If an account with email exists, form is valid."""
     u = user(save=True, email='*****@*****.**', is_active=True)
     form = ForgotUsernameForm({'email': u.email})
     assert form.is_valid()
Beispiel #4
0
 def test_email_doesnt_exist(self):
     """If no account with email exists, form isn't valid."""
     form = ForgotUsernameForm({'email': '*****@*****.**'})
     assert not form.is_valid()
Beispiel #5
0
 def test_valid_email(self):
     """"If an account with email exists, form is valid."""
     u = user(save=True, email='*****@*****.**', is_active=True)
     form = ForgotUsernameForm({'email': u.email})
     assert form.is_valid()
Beispiel #6
0
 def test_email_doesnt_exist(self):
     """If no account with email exists, form isn't valid."""
     form = ForgotUsernameForm({'email': '*****@*****.**'})
     assert not form.is_valid()