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: try_send_email_with_form( form.save, form, 'email', use_https=request.is_secure(), token_generator=default_token_generator, email_template_name='users/email/pw_reset.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 jingo.render(request, 'users/pw_reset_form.html', {'form': form})
def forgot_username(request): """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 jingo.render(request, 'users/forgot_username.html', {'form': form})
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: 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, email_template_name='users/email/pw_reset.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 jingo.render(request, template, {'form': form})
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})
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})
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') message = email_utils.render_email( 'users/email/already_activated.ltxt', email_kwargs) # TODO: Send HTML email here. form = try_send_email_with_form( mail.send_mail, form, 'email', subject, message, settings.DEFAULT_FROM_EMAIL, [user.email]) 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})
def resend_confirmation(request): """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, email_template='users/email/already_activated.ltxt', email_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')} message = render_to_string( 'users/email/already_activated.ltxt', email_kwargs) form = try_send_email_with_form( mail.send_mail, form, 'email', _('Account already activated'), message, settings.DEFAULT_FROM_EMAIL, [user.email]) 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 jingo.render(request, 'users/resend_confirmation_done.html', {'email': email}) else: form = EmailConfirmationForm() return jingo.render(request, 'users/resend_confirmation.html', {'form': form})
def resend_confirmation(request): """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, user__is_active=False) form = try_send_email_with_form( RegistrationProfile.objects.send_confirmation_email, form, 'email', reg_prof) except RegistrationProfile.DoesNotExist: # Don't leak existence of email addresses. pass # Form may now be invalid if email failed to send. if form.is_valid(): return jingo.render(request, 'users/resend_confirmation_done.html', {'email': email}) else: form = EmailConfirmationForm() return jingo.render(request, 'users/resend_confirmation.html', {'form': form})
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: 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, email_template_name='users/email/pw_reset.ltxt', 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})