def forgot_password(request): """If the user forgot his password, he can have a new one.""" if request.method == "POST": form = UsernameAndEmailForm(request.POST) if form.is_valid(): # Get data from form data = form.data username = data["username"] email = data["email"] # Fetch the user, we need his email adress usr = None if username: usr = get_object_or_404(User, Q(username=username)) if email: usr = get_object_or_404(User, Q(email=email)) # Generate a valid token during one hour. uuid_token = str(uuid.uuid4()) date_end = datetime.now() + timedelta( days=0, hours=1, minutes=0, seconds=0) token = TokenForgotPassword(user=usr, token=uuid_token, date_end=date_end) token.save() # send email subject = _(u"{} - Mot de passe oublié").format( settings.ZDS_APP['site']['litteral_name']) from_email = "{} <{}>".format( settings.ZDS_APP['site']['litteral_name'], settings.ZDS_APP['site']['email_noreply']) context = { "username": usr.username, "site_name": settings.ZDS_APP['site']['litteral_name'], "site_url": settings.ZDS_APP['site']['url'], "url": settings.ZDS_APP['site']['url'] + token.get_absolute_url() } message_html = render_to_string( "email/member/confirm_forgot_password.html", context) message_txt = render_to_string( "email/member/confirm_forgot_password.txt", context) msg = EmailMultiAlternatives(subject, message_txt, from_email, [usr.email]) msg.attach_alternative(message_html, "text/html") msg.send() return render(request, "member/forgot_password/success.html") else: return render(request, "member/forgot_password/index.html", {"form": form}) form = UsernameAndEmailForm() return render(request, "member/forgot_password/index.html", {"form": form})
def forgot_password(request): """If the user has forgotten his password, they can get a new one.""" if request.method == 'POST': form = UsernameAndEmailForm(request.POST) if form.is_valid(): # Get data from form data = form.data username = data['username'] email = data['email'] # Fetch the user, we need his email address usr = None if username: usr = get_object_or_404(User, Q(username=username)) if email: usr = get_object_or_404(User, Q(email=email)) # Generate a valid token during one hour uuid_token = str(uuid.uuid4()) date_end = datetime.now() + timedelta(days=0, hours=1, minutes=0, seconds=0) token = TokenForgotPassword(user=usr, token=uuid_token, date_end=date_end) token.save() # Send email subject = _('{} - Mot de passe oublié').format(settings.ZDS_APP['site']['literal_name']) from_email = '{} <{}>'.format(settings.ZDS_APP['site']['literal_name'], settings.ZDS_APP['site']['email_noreply']) context = { 'username': usr.username, 'site_name': settings.ZDS_APP['site']['literal_name'], 'site_url': settings.ZDS_APP['site']['url'], 'url': settings.ZDS_APP['site']['url'] + token.get_absolute_url() } message_html = render_to_string('email/member/confirm_forgot_password.html', context) message_txt = render_to_string('email/member/confirm_forgot_password.txt', context) msg = EmailMultiAlternatives(subject, message_txt, from_email, [usr.email]) msg.attach_alternative(message_html, 'text/html') msg.send() return render(request, 'member/forgot_password/success.html') else: return render(request, 'member/forgot_password/index.html', {'form': form}) form = UsernameAndEmailForm() return render(request, 'member/forgot_password/index.html', {'form': form})