Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added logic to show an error page/msg if email cannot be sent as repo… #44

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions docs/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ What you get

* Password reset links that expire in two days (configurable).

* Optionally invoke a captcha in the password reset page if django-simple-captcha (0.5.1 or higher) is installed and configured.

What you can do
---------------

Expand Down
2 changes: 1 addition & 1 deletion password_reset/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.8.1'
__version__ = '0.8.2'
40 changes: 27 additions & 13 deletions password_reset/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,29 @@
from django.utils.translation import ugettext_lazy as _
from django.conf import settings


try:
if 'captcha' in settings.INSTALLED_APPS:
from captcha.fields import CaptchaField # uses django-simple-captcha
except ImportError:
CaptchaField = None
pass

from .utils import get_user_model

error_messages = {
'not_found': _("Sorry, this user doesn't exist."),
'password_mismatch': _("The two passwords didn't match."),
}


class PasswordRecoveryForm(forms.Form):
username_or_email = forms.CharField()

error_messages = {
'not_found': _("Sorry, this user doesn't exist."),
}
try:
if CaptchaField and settings.CAPTCHA_CHALLENGE_FUNCT: # defined
captcha = CaptchaField(label=_('Captcha')) # Optional Captcha
except:
pass

def __init__(self, *args, **kwargs):
self.case_sensitive = kwargs.pop('case_sensitive', True)
Expand Down Expand Up @@ -63,7 +77,7 @@ def get_user_by_username(self, username):
try:
user = User._default_manager.get(**{key: username})
except User.DoesNotExist:
raise forms.ValidationError(self.error_messages['not_found'],
raise forms.ValidationError(error_messages['not_found'],
code='not_found')
return user

Expand All @@ -74,20 +88,24 @@ def get_user_by_email(self, email):
try:
user = User._default_manager.get(**{key: email})
except User.DoesNotExist:
raise forms.ValidationError(self.error_messages['not_found'],
raise forms.ValidationError(error_messages['not_found'],
code='not_found')
return user

def get_user_by_both(self, username):
key = '__%sexact'
key = key % '' if self.case_sensitive else key % 'i'
f = lambda field: Q(**{field + key: username})
# f = lambda field: Q(**{field + key: username})

def f(field): # to satisfy lint in Travis auto build on Github
return Q(**{field + key: username})

filters = f('username') | f('email')
User = get_user_model()
try:
user = User._default_manager.get(filters)
except User.DoesNotExist:
raise forms.ValidationError(self.error_messages['not_found'],
raise forms.ValidationError(error_messages['not_found'],
code='not_found')
except User.MultipleObjectsReturned:
raise forms.ValidationError(_("Unable to find user."))
Expand All @@ -105,10 +123,6 @@ class PasswordResetForm(forms.Form):
widget=forms.PasswordInput,
)

error_messages = {
'password_mismatch': _("The two passwords didn't match."),
}

def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user')
super(PasswordResetForm, self).__init__(*args, **kwargs)
Expand All @@ -118,7 +132,7 @@ def clean_password2(self):
password2 = self.cleaned_data['password2']
if not password1 == password2:
raise forms.ValidationError(
self.error_messages['password_mismatch'],
error_messages['password_mismatch'],
code='password_mismatch')
return password2

Expand Down
1 change: 1 addition & 0 deletions password_reset/locale/zh
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2014-01-23 10:01+0800\n"
"POT-Creation-Date: 2016-02-05 16:48+0800\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand All @@ -17,35 +17,43 @@ msgstr ""
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: forms.py:26
#: forms.py:14
msgid "Captcha"
msgstr "认证码"

#: forms.py:17
msgid "Sorry, this user doesn't exist."
msgstr "抱歉,用户名不存在."

#: forms.py:34
msgid "Username"
msgstr "用户名"

#: forms.py:27
#: forms.py:35
msgid "Email"
msgstr "邮箱"

#: forms.py:28
#: forms.py:36
msgid "Username or Email"
msgstr "用户名或者邮箱"

#: forms.py:48 forms.py:58 forms.py:70
msgid "Sorry, this user doesn't exist."
msgstr "抱歉,用户名不存在."
#: forms.py:58
msgid "Sorry, inactive users can't recover their password."
msgstr "非常抱歉,非活跃用户无法重置密码。"

#: forms.py:72
#: forms.py:96
msgid "Unable to find user."
msgstr "找不到指定用户"

#: forms.py:78
#: forms.py:103
msgid "New password"
msgstr "新密码"

#: forms.py:82
#: forms.py:107
msgid "New password (confirm)"
msgstr "新密码(确认)"
msgstr "新密码(确认)"

#: forms.py:94
#: forms.py:112
msgid "The two passwords didn't match."
msgstr "两次输入的密码不一致"

Expand All @@ -57,19 +65,19 @@ msgstr "设置新密码"
msgid ""
"Your password has successfully been reset. You can use it right now on the "
"login page."
msgstr "您的密码已经重设成功,现在可以登录了."
msgstr "您的密码已经重设成功现在可以登录了"

#: templates/password_reset/recovery_email.txt:1
#, python-format
msgid "Dear %(username)s,"
msgstr "您好 %(username)s,"
msgstr "%(username)s,您好:"

#: templates/password_reset/recovery_email.txt:3
#, python-format
msgid ""
"You -- or someone pretending to be you -- has requested a password reset on "
"%(domain)s."
msgstr "您或者其它的人在 %(domain)s 申请了密码重置."
msgstr "您或某人在 %(domain)s 申请了密码重置"

#: templates/password_reset/recovery_email.txt:5
msgid "You can set your new password by following this link:"
Expand All @@ -79,18 +87,30 @@ msgstr "您可以通过以下链接重置密码:"
msgid ""
"If you don't want to reset your password, simply ignore this email and it "
"will stay unchanged."
msgstr "如果您不想重设密码,可以忽略此邮箱."
msgstr "若不想重设密码,可以忽略此邮件。"

#: templates/password_reset/recovery_email.txt:11
msgid "Yours Truly"
msgstr "感谢您的惠顾"

#: templates/password_reset/recovery_email.txt:12
msgid "Administrator"
msgstr "管理者"

#: templates/password_reset/recovery_email_subject.txt:1
#, python-format
msgid "Password recovery on %(domain)s"
msgstr " %(domain)s 上重设密码"
msgstr "重设 %(domain)s 密码"

#: templates/password_reset/recovery_form.html:5
msgid "Password recovery"
msgstr "重新设置密码"

#: templates/password_reset/recovery_form.html:11
#: templates/password_reset/recovery_form.html:8
msgid "Recover Password"
msgstr "重设密码"

#: templates/password_reset/recovery_form.html:60
msgid "Recover my password"
msgstr "重设密码"

Expand All @@ -99,7 +119,8 @@ msgstr "重设密码"
msgid ""
"Sorry, this password reset link is invalid. You can still <a href="
"\"%(recovery_url)s\">request a new one</a>."
msgstr "抱歉,链接已经失效,您可以在 <a href=""\"%(recovery_url)s\">请求一个新链接</a>."
msgstr ""
"抱歉,链接已经失效,您可以在 <a href=\"%(recovery_url)s\">请求一个新链接</a>."

#: templates/password_reset/reset.html:7
#, python-format
Expand All @@ -114,9 +135,26 @@ msgstr "设置新密码"
msgid "Password recovery sent"
msgstr "发送重设密码"

#: templates/password_reset/reset_sent.html:7
#: templates/password_reset/reset_sent.html:8
#, python-format
msgid ""
"An email was sent to <strong>%(email)s</strong> %(ago)s ago. Use the link in "
"it to set a new password."
msgstr "已经在%(ago)s以前向您的邮箱 <strong>%(email)s</strong>发送了邮件,根据邮件提示重新设置您的新密码."
msgstr ""
"已经在%(ago)s以前向您的邮箱 <strong>%(email)s</strong> 发送了邮件,请根据邮件"
"提示重新设置您的新密码."

#: templates/password_reset/reset_sent.html:11
#: templates/password_reset/reset_sent_failed.html:9
msgid "Return to Main Page"
msgstr "回主页"

#: templates/password_reset/reset_sent_failed.html:4
msgid "Password recovery NOT successful"
msgstr "发送重设密码"

#: templates/password_reset/reset_sent_failed.html:7
msgid ""
"The email for this registered user is invalid, password reset information "
"cannot be sent. Please contact our administrator for assistance."
msgstr "此用户电邮地址无法接收密码重置信息。欢迎联系我们的客服部门。"
Binary file removed password_reset/locale/zh/LC_MESSAGES/django.mo
Binary file not shown.
1 change: 1 addition & 0 deletions password_reset/locale/zh_Hans
Empty file added password_reset/models.py
Empty file.
4 changes: 4 additions & 0 deletions password_reset/templates/password_reset/recovery_email.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@
http{% if secure %}s{% endif %}://{{ site.domain }}{% url "password_reset_reset" token %}

{% trans "If you don't want to reset your password, simply ignore this email and it will stay unchanged." %}

{% trans "Yours Truly" %},
{% trans "Administrator" %}
{{ site.domain }}
45 changes: 39 additions & 6 deletions password_reset/templates/password_reset/recovery_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,42 @@
{% block title %}{% trans "Password recovery" %}{% endblock %}

{% block content %}
<form method="post" action="{{ url }}">
{% csrf_token %}
{{ form.as_p }}
<p><input type="submit" value="{% trans "Recover my password" %}"></p>
</form>
{% endblock %}
<h1>{% trans "Recover Password" %}</h1>
<form method="post" action="{{ url }}">
{% csrf_token %}
{% for field in form %}
{% if field.name != "captcha" %}
<div class="input-group">
<span class="input-group-addon" id="sizing-addon2">{{ field.label_tag }}</span>
<input type="text" class="form-control" id="{{ field.id_for_label }}"
name="{{ field.html_name }}" placeholder="{{ field.help_text }}" aria-describedby="sizing-addon2">
</div>
{% else %}
<p>
<div class="input-group">
<span class="input-group-btn" >
<button class="btn btn-wmk js-captcha-refresh" type="button" id="refresh">
<span class="glyphicon glyphicon-refresh" aria-hidden="true"></span>
{{ field.label_tag }}
</button>
</span>
&nbsp; {{ field }}
</div><!-- /input-group -->
{% endif %}
<div class="field-error">
{{ field.errors }}
</div>
{% endfor %}

<div class="None" style="text-align:center;margin-top:20px;">
<div class="btn-group" role="group" aria-label="Reset Password">
<button id="reset_password" name="reset_password" value="reset_password" type="submit" class="btn btn-wmk" onclick="
">
<span class="glyphicon glyphicon-user" aria-hidden="true"></span>
{% trans "Recover my password" %}
</button>
</div>
</div>

</form>
{% endblock content %}
6 changes: 6 additions & 0 deletions password_reset/templates/password_reset/reset_sent.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@
{% block title %}{% trans "Password recovery sent" %}{% endblock %}

{% block content %}
<div id="search_msg1" class="alert alert-success" role="alert">
<p>{% blocktrans with ago=timestamp|timesince %}An email was sent to <strong>{{ email }}</strong> {{ ago }} ago. Use the link in it to set a new password.{% endblocktrans %}</p>
</div>

<a class="url_button" href="/">{% trans "Return to Main Page" %}</a>


{% endblock %}
11 changes: 11 additions & 0 deletions password_reset/templates/password_reset/reset_sent_failed.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends "password_reset/base.html" %}
{% load i18n %}

{% block title %}{% trans "Password recovery NOT successful" %}{% endblock %}

{% block content %}
<p>{% trans "The email for this registered user is invalid, password reset information cannot be sent. Please contact our administrator for assistance." %} </p>

<a class="url_button" href="/">{% trans "Return to Main Page" %}</a>

{% endblock %}
4 changes: 2 additions & 2 deletions password_reset/tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
},
}

INSTALLED_APPS = (
INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.contenttypes',
'password_reset',
'password_reset.tests',
)
]

MIGRATION_MODULES = {
'auth': 'django.contrib.auth.tests.migrations',
Expand Down
Loading