Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Marco Marinello
django-password-reset
Commits
eaa07078
Commit
eaa07078
authored
May 26, 2014
by
Bruno Renié
Browse files
Fix #21 -- allow customization of form error messages
parent
fa05dc69
Changes
3
Hide whitespace changes
Inline
Side-by-side
docs/index.rst
View file @
eaa07078
...
...
@@ -28,6 +28,11 @@ Contents:
Changelog
---------
* 0.8 (not released yet):
* Allow customizing form error message via the ``error_messages`` attribute
on form classes.
* 0.7 (2014-02-18):
* Return user instance in ``PasswordResetForm.save()``, add ``commit``
...
...
docs/views.rst
View file @
eaa07078
...
...
@@ -31,7 +31,8 @@ Attributes
data. Default: ``True``.
* ``form_class``: the form to use for validating the user. Default:
``password_reset.forms.PasswordRecoveryForm``.
``password_reset.forms.PasswordRecoveryForm``. To customize form error
messages, subclass the form and override the ``error_messages`` attribute.
* ``template_name``: defaults to ``password_reset/recovery_form.html``.
...
...
@@ -80,7 +81,9 @@ Reset
Attributes
``````````
* ``form_class``: defaults to ``password_reset.forms.PasswordResetForm``.
* ``form_class``: defaults to ``password_reset.forms.PasswordResetForm``. To
customize form error messages, subclass the form and override the
``error_messages`` attribute.
* ``token_expires``: expiration time (in seconds) of the password reset token.
Default is two days.
...
...
password_reset/forms.py
View file @
eaa07078
...
...
@@ -9,6 +9,10 @@ from .utils import get_user_model
class
PasswordRecoveryForm
(
forms
.
Form
):
username_or_email
=
forms
.
CharField
()
error_messages
=
{
'not_found'
:
_
(
"Sorry, this user doesn't exist."
),
}
def
__init__
(
self
,
*
args
,
**
kwargs
):
self
.
case_sensitive
=
kwargs
.
pop
(
'case_sensitive'
,
True
)
search_fields
=
kwargs
.
pop
(
'search_fields'
,
(
'username'
,
'email'
))
...
...
@@ -48,7 +52,8 @@ class PasswordRecoveryForm(forms.Form):
try
:
user
=
User
.
_default_manager
.
get
(
**
{
key
:
username
})
except
User
.
DoesNotExist
:
raise
forms
.
ValidationError
(
_
(
"Sorry, this user doesn't exist."
))
raise
forms
.
ValidationError
(
self
.
error_messages
[
'not_found'
],
code
=
'not_found'
)
return
user
def
get_user_by_email
(
self
,
email
):
...
...
@@ -58,7 +63,8 @@ class PasswordRecoveryForm(forms.Form):
try
:
user
=
User
.
_default_manager
.
get
(
**
{
key
:
email
})
except
User
.
DoesNotExist
:
raise
forms
.
ValidationError
(
_
(
"Sorry, this user doesn't exist."
))
raise
forms
.
ValidationError
(
self
.
error_messages
[
'not_found'
],
code
=
'not_found'
)
return
user
def
get_user_by_both
(
self
,
username
):
...
...
@@ -70,7 +76,8 @@ class PasswordRecoveryForm(forms.Form):
try
:
user
=
User
.
_default_manager
.
get
(
filters
)
except
User
.
DoesNotExist
:
raise
forms
.
ValidationError
(
_
(
"Sorry, this user doesn't exist."
))
raise
forms
.
ValidationError
(
self
.
error_messages
[
'not_found'
],
code
=
'not_found'
)
except
User
.
MultipleObjectsReturned
:
raise
forms
.
ValidationError
(
_
(
"Unable to find user."
))
return
user
...
...
@@ -86,6 +93,10 @@ 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
)
...
...
@@ -94,7 +105,9 @@ class PasswordResetForm(forms.Form):
password1
=
self
.
cleaned_data
.
get
(
'password1'
,
''
)
password2
=
self
.
cleaned_data
[
'password2'
]
if
not
password1
==
password2
:
raise
forms
.
ValidationError
(
_
(
"The two passwords didn't match."
))
raise
forms
.
ValidationError
(
self
.
error_messages
[
'password_mismatch'
],
code
=
'password_mismatch'
)
return
password2
def
save
(
self
,
commit
=
True
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment