예제 #1
0
def forgot_password(request):
    """If the user forgot his password, he can have a new one."""

    if request.method == "POST":
        form = ForgotPasswordForm(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 = _(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 = ForgotPasswordForm()
    return render(request, "member/forgot_password/index.html", {"form": form})
예제 #2
0
 def test_unknow_name_forgot_password_form(self):
     data = {
         'username': "******"
     }
     form = ForgotPasswordForm(data=data)
     self.assertFalse(form.is_valid())
예제 #3
0
 def test_empty_name_forgot_password_form(self):
     data = {
         'username': ''
     }
     form = ForgotPasswordForm(data=data)
     self.assertFalse(form.is_valid())
예제 #4
0
 def test_valid_forgot_password_form(self):
     data = {
         'username': self.user1.user.username
     }
     form = ForgotPasswordForm(data=data)
     self.assertTrue(form.is_valid())
예제 #5
0
 def test_unknow_name_forgot_password_form(self):
     data = {"username": "******"}
     form = ForgotPasswordForm(data=data)
     self.assertFalse(form.is_valid())
예제 #6
0
 def test_empty_name_forgot_password_form(self):
     data = {"username": ""}
     form = ForgotPasswordForm(data=data)
     self.assertFalse(form.is_valid())
예제 #7
0
 def test_valid_forgot_password_form(self):
     data = {"username": self.user1.user.username}
     form = ForgotPasswordForm(data=data)
     self.assertTrue(form.is_valid())
예제 #8
0
 def test_unknow_email_forgot_password_form(self):
     data = {'email': '*****@*****.**', 'username': ''}
     form = ForgotPasswordForm(data=data)
     self.assertFalse(form.is_valid())
예제 #9
0
 def test_unknow_username_forgot_password_form(self):
     data = {'username': '******', 'email': ''}
     form = ForgotPasswordForm(data=data)
     self.assertFalse(form.is_valid())
예제 #10
0
 def test_full_forgot_password_form(self):
     data = {'username': '******', 'email': '*****@*****.**'}
     form = ForgotPasswordForm(data=data)
     self.assertFalse(form.is_valid())
예제 #11
0
 def test_non_valid_non_ascii_email_forgot_password_form(self):
     data = {'username': '', 'email': self.userNonAscii.user.email}
     form = ForgotPasswordForm(data=data)
     self.assertTrue(form.is_valid())