2016-05-26 17:14:39 +00:00
from django import forms
2016-05-07 23:31:16 +00:00
from django . contrib . auth import authenticate
from django . contrib . auth . forms import AuthenticationForm
from django . contrib . auth . forms import UserCreationForm , UserChangeForm
2016-05-26 17:14:39 +00:00
from django . core . exceptions import ValidationError
2018-05-10 19:23:23 +00:00
from django . urls import reverse_lazy
2013-05-22 17:12:09 +00:00
2020-09-26 21:21:43 +00:00
from django . utils . translation import gettext_lazy as _
2013-05-31 06:49:24 +00:00
2013-07-07 08:15:22 +00:00
# Translators : %(username)s will be replaced by the username_field name
# (default : username, but could be email, or something else)
2016-05-19 16:50:56 +00:00
ERROR_MESSAGE = _ (
2016-05-07 20:59:15 +00:00
" Please enter the correct %(username)s and password "
" for a staff account. Note that both fields may be case-sensitive. "
)
2013-05-31 06:49:24 +00:00
class AdminAuthenticationForm ( AuthenticationForm ) :
"""
A custom authentication form used in the admin app .
Liberally copied from django . contrib . admin . forms . AdminAuthenticationForm
"""
2013-07-06 07:37:25 +00:00
error_messages = {
2016-05-19 16:50:56 +00:00
' required ' : _ ( " Please log in again, because your session has expired. " ) ,
2013-07-06 07:37:25 +00:00
}
2016-05-26 17:14:39 +00:00
this_is_the_login_form = forms . BooleanField (
widget = forms . HiddenInput ,
2016-05-07 20:59:15 +00:00
initial = 1 ,
error_messages = error_messages
)
2013-05-31 06:49:24 +00:00
def clean ( self ) :
username = self . cleaned_data . get ( ' username ' )
password = self . cleaned_data . get ( ' password ' )
message = ERROR_MESSAGE
if username and password :
self . user_cache = authenticate ( username = username , password = password )
if self . user_cache is None :
2016-05-26 17:14:39 +00:00
raise ValidationError ( message % {
2013-05-31 06:49:24 +00:00
' username ' : self . username_field . verbose_name
} )
elif not self . user_cache . is_active or not self . user_cache . is_staff :
2016-05-26 17:14:39 +00:00
raise ValidationError ( message % {
2013-05-31 06:49:24 +00:00
' username ' : self . username_field . verbose_name
} )
return self . cleaned_data
2013-06-01 16:02:01 +00:00
2016-05-26 17:14:39 +00:00
class Admin2UserCreationForm ( UserCreationForm ) :
pass
2016-05-19 16:50:56 +00:00
class Admin2UserChangeForm ( UserChangeForm ) :
def __init__ ( self , * args , * * kwargs ) :
2021-10-17 09:38:55 +00:00
super ( ) . __init__ ( * args , * * kwargs )
2016-05-19 16:50:56 +00:00
print ( self . fields [ ' password ' ] . help_text )
2016-05-19 17:21:56 +00:00
self . fields [ ' password ' ] . help_text = _ ( " Raw passwords are not stored, so there is no way to see this user ' s password, but you can change the password using <a href= \" %s \" >this form</a>. " % self . get_update_password_url ( ) )
2016-05-19 16:50:56 +00:00
def get_update_password_url ( self ) :
2016-05-19 17:21:56 +00:00
if self . instance and self . instance . pk :
return reverse_lazy ( ' admin2:password_change ' , args = [ self . instance . pk ] )
return ' password/ '