| 1 |
from django.contrib.auth.forms import AuthenticationForm, PasswordChangeForm |
| 2 |
from django.core.exceptions import ValidationError |
| 3 |
from django.utils.translation import gettext_lazy as _ |
| 4 |
|
| 5 |
|
| 6 |
class AdminAuthenticationForm(AuthenticationForm): |
| 7 |
""" |
| 8 |
A custom authentication form used in the admin app. |
| 9 |
""" |
| 10 |
|
| 11 |
error_messages = { |
| 12 |
**AuthenticationForm.error_messages, |
| 13 |
"invalid_login": _( |
| 14 |
"Please enter the correct %(username)s and password for a staff " |
| 15 |
"account. Note that both fields may be case-sensitive." |
| 16 |
), |
| 17 |
} |
| 18 |
required_css_class = "required" |
| 19 |
|
| 20 |
def confirm_login_allowed(self, user): |
| 21 |
super().confirm_login_allowed(user) |
| 22 |
if not user.is_staff: |
| 23 |
raise ValidationError( |
| 24 |
self.error_messages["invalid_login"], |
| 25 |
code="invalid_login", |
| 26 |
params={"username": self.username_field.verbose_name}, |
| 27 |
) |
| 28 |
|
| 29 |
|
| 30 |
class AdminPasswordChangeForm(PasswordChangeForm): |
| 31 |
required_css_class = "required" |