<s> from __future__ import unicode_literals <EOL> import warnings <EOL> from django import forms <EOL> from django . forms . util import flatatt <EOL> from django . template import loader <EOL> from django . utils . datastructures import SortedDict <EOL> from django . utils . encoding import force_bytes <EOL> from django . utils . html import format_html , format_html_join <EOL> from django . utils . http import urlsafe_base64_encode <EOL> from django . utils . safestring import mark_safe <EOL> from django . utils . text import capfirst <EOL> from django . utils . translation import ugettext , ugettext_lazy as _ <EOL> from django . contrib . auth import authenticate , get_user_model <EOL> from django . contrib . auth . models import User <EOL> from django . contrib . auth . hashers import UNUSABLE_PASSWORD_PREFIX , identify_hasher <EOL> from django . contrib . auth . tokens import default_token_generator <EOL> from django . contrib . sites . models import get_current_site <EOL> UNMASKED_DIGITS_TO_SHOW = <NUM_LIT:6> <EOL> mask_password = lambda p : "<STR_LIT>" % ( p [ : UNMASKED_DIGITS_TO_SHOW ] , "<STR_LIT:*>" * max ( len ( p ) - UNMASKED_DIGITS_TO_SHOW , <NUM_LIT:0> ) ) <EOL> class ReadOnlyPasswordHashWidget ( forms . Widget ) : <EOL> def render ( self , name , value , attrs ) : <EOL> encoded = value <EOL> final_attrs = self . build_attrs ( attrs ) <EOL> if not encoded or encoded . startswith ( UNUSABLE_PASSWORD_PREFIX ) : <EOL> summary = mark_safe ( "<STR_LIT>" % ugettext ( "<STR_LIT>" ) ) <EOL> else : <EOL> try : <EOL> hasher = identify_hasher ( encoded ) <EOL> except ValueError : <EOL> summary = mark_safe ( "<STR_LIT>" % ugettext ( <EOL> "<STR_LIT>" ) ) <EOL> else : <EOL> summary = format_html_join ( '<STR_LIT>' , <EOL> "<STR_LIT>" , <EOL> ( ( ugettext ( key ) , value ) <EOL> for key , value in hasher . safe_summary ( encoded ) . items ( ) ) <EOL> ) <EOL> return format_html ( "<STR_LIT>" , flatatt ( final_attrs ) , summary ) <EOL> class ReadOnlyPasswordHashField ( forms . Field ) : <EOL> widget = ReadOnlyPasswordHashWidget <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> kwargs . setdefault ( "<STR_LIT>" , False ) <EOL> super ( ReadOnlyPasswordHashField , self ) . __init__ ( * args , ** kwargs ) <EOL> def bound_data ( self , data , initial ) : <EOL> return initial <EOL> def _has_changed ( self , initial , data ) : <EOL> return False <EOL> class UserCreationForm ( forms . ModelForm ) : <EOL> """<STR_LIT>""" <EOL> error_messages = { <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" ) , <EOL> } <EOL> username = forms . RegexField ( label = _ ( "<STR_LIT>" ) , max_length = <NUM_LIT:30> , <EOL> regex = r'<STR_LIT>' , <EOL> help_text = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> error_messages = { <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) } ) <EOL> password1 = forms . CharField ( label = _ ( "<STR_LIT>" ) , <EOL> widget = forms . PasswordInput ) <EOL> password2 = forms . CharField ( label = _ ( "<STR_LIT>" ) , <EOL> widget = forms . PasswordInput , <EOL> help_text = _ ( "<STR_LIT>" ) ) <EOL> class Meta : <EOL> model = User <EOL> fields = ( "<STR_LIT:username>" , ) <EOL> def clean_username ( self ) : <EOL> username = self . cleaned_data [ "<STR_LIT:username>" ] <EOL> try : <EOL> User . _default_manager . get ( username = username ) <EOL> except User . DoesNotExist : <EOL> return username <EOL> raise forms . ValidationError ( <EOL> self . error_messages [ '<STR_LIT>' ] , <EOL> code = '<STR_LIT>' , <EOL> ) <EOL> def clean_password2 ( self ) : <EOL> password1 = self . cleaned_data . get ( "<STR_LIT>" ) <EOL> password2 = self . cleaned_data . get ( "<STR_LIT>" ) <EOL> if password1 and password2 and password1 != password2 : <EOL> raise forms . ValidationError ( <EOL> self . error_messages [ '<STR_LIT>' ] , <EOL> code = '<STR_LIT>' , <EOL> ) <EOL> return password2 <EOL> def save ( self , commit = True ) : <EOL> user = super ( UserCreationForm , self ) . save ( commit = False ) <EOL> user . set_password ( self . cleaned_data [ "<STR_LIT>" ] ) <EOL> if commit : <EOL> user . save ( ) <EOL> return user <EOL> class UserChangeForm ( forms . ModelForm ) : <EOL> username = forms . RegexField ( <EOL> label = _ ( "<STR_LIT>" ) , max_length = <NUM_LIT:30> , regex = r"<STR_LIT>" , <EOL> help_text = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> error_messages = { <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) } ) <EOL> password = ReadOnlyPasswordHashField ( label = _ ( "<STR_LIT>" ) , <EOL> help_text = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> class Meta : <EOL> model = User <EOL> fields = '<STR_LIT>' <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( UserChangeForm , self ) . __init__ ( * args , ** kwargs ) <EOL> f = self . fields . get ( '<STR_LIT>' , None ) <EOL> if f is not None : <EOL> f . queryset = f . queryset . select_related ( '<STR_LIT>' ) <EOL> def clean_password ( self ) : <EOL> return self . initial [ "<STR_LIT:password>" ] <EOL> class AuthenticationForm ( forms . Form ) : <EOL> """<STR_LIT>""" <EOL> username = forms . CharField ( max_length = <NUM_LIT> ) <EOL> password = forms . CharField ( label = _ ( "<STR_LIT>" ) , widget = forms . PasswordInput ) <EOL> error_messages = { <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" ) , <EOL> } <EOL> def __init__ ( self , request = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . request = request <EOL> self . user_cache = None <EOL> super ( AuthenticationForm , self ) . __init__ ( * args , ** kwargs ) <EOL> UserModel = get_user_model ( ) <EOL> self . username_field = UserModel . _meta . get_field ( UserModel . USERNAME_FIELD ) <EOL> if self . fields [ '<STR_LIT:username>' ] . label is None : <EOL> self . fields [ '<STR_LIT:username>' ] . label = capfirst ( self . username_field . verbose_name ) <EOL> def clean ( self ) : <EOL> username = self . cleaned_data . get ( '<STR_LIT:username>' ) <EOL> password = self . cleaned_data . get ( '<STR_LIT:password>' ) <EOL> if username and password : <EOL> self . user_cache = authenticate ( username = username , <EOL> password = password ) <EOL> if self . user_cache is None : <EOL> raise forms . ValidationError ( <EOL> self . error_messages [ '<STR_LIT>' ] , <EOL> code = '<STR_LIT>' , <EOL> params = { '<STR_LIT:username>' : self . username_field . verbose_name } , <EOL> ) <EOL> elif not self . user_cache . is_active : <EOL> raise forms . ValidationError ( <EOL> self . error_messages [ '<STR_LIT>' ] , <EOL> code = '<STR_LIT>' , <EOL> ) <EOL> return self . cleaned_data <EOL> def check_for_test_cookie ( self ) : <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" , DeprecationWarning ) <EOL> def get_user_id ( self ) : <EOL> if self . user_cache : <EOL> return self . user_cache . id <EOL> return None <EOL> def get_user ( self ) : <EOL> return self . user_cache <EOL> class PasswordResetForm ( forms . Form ) : <EOL> email = forms . EmailField ( label = _ ( "<STR_LIT>" ) , max_length = <NUM_LIT> ) <EOL> def save ( self , domain_override = None , <EOL> subject_template_name = '<STR_LIT>' , <EOL> email_template_name = '<STR_LIT>' , <EOL> use_https = False , token_generator = default_token_generator , <EOL> from_email = None , request = None ) : <EOL> """<STR_LIT>""" <EOL> from django . core . mail import send_mail <EOL> UserModel = get_user_model ( ) <EOL> email = self . cleaned_data [ "<STR_LIT:email>" ] <EOL> active_users = UserModel . _default_manager . filter ( <EOL> email__iexact = email , is_active = True ) <EOL> for user in active_users : <EOL> if not user . has_usable_password ( ) : <EOL> continue <EOL> if not domain_override : <EOL> current_site = get_current_site ( request ) <EOL> site_name = current_site . name <EOL> domain = current_site . domain <EOL> else : <EOL> site_name = domain = domain_override <EOL> c = { <EOL> '<STR_LIT:email>' : user . email , <EOL> '<STR_LIT>' : domain , <EOL> '<STR_LIT>' : site_name , <EOL> '<STR_LIT>' : urlsafe_base64_encode ( force_bytes ( user . pk ) ) , <EOL> '<STR_LIT:user>' : user , <EOL> '<STR_LIT>' : token_generator . make_token ( user ) , <EOL> '<STR_LIT>' : '<STR_LIT>' if use_https else '<STR_LIT:http>' , <EOL> } <EOL> subject = loader . render_to_string ( subject_template_name , c ) <EOL> subject = '<STR_LIT>' . join ( subject . splitlines ( ) ) <EOL> email = loader . render_to_string ( email_template_name , c ) <EOL> send_mail ( subject , email , from_email , [ user . email ] ) <EOL> class SetPasswordForm ( forms . Form ) : <EOL> """<STR_LIT>""" <EOL> error_messages = { <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" ) , <EOL> } <EOL> new_password1 = forms . CharField ( label = _ ( "<STR_LIT>" ) , <EOL> widget = forms . PasswordInput ) <EOL> new_password2 = forms . CharField ( label = _ ( "<STR_LIT>" ) , <EOL> widget = forms . PasswordInput ) <EOL> def __init__ ( self , user , * args , ** kwargs ) : <EOL> self . user = user <EOL> super ( SetPasswordForm , self ) . __init__ ( * args , ** kwargs ) <EOL> def clean_new_password2 ( self ) : <EOL> password1 = self . cleaned_data . get ( '<STR_LIT>' ) <EOL> password2 = self . cleaned_data . get ( '<STR_LIT>' ) <EOL> if password1 and password2 : <EOL> if password1 != password2 : <EOL> raise forms . ValidationError ( <EOL> self . error_messages [ '<STR_LIT>' ] , <EOL> code = '<STR_LIT>' , <EOL> ) <EOL> return password2 <EOL> def save ( self , commit = True ) : <EOL> self . user . set_password ( self . cleaned_data [ '<STR_LIT>' ] ) <EOL> if commit : <EOL> self . user . save ( ) <EOL> return self . user <EOL> class PasswordChangeForm ( SetPasswordForm ) : <EOL> """<STR_LIT>""" <EOL> error_messages = dict ( SetPasswordForm . error_messages , ** { <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> } ) <EOL> old_password = forms . CharField ( label = _ ( "<STR_LIT>" ) , <EOL> widget = forms . PasswordInput ) <EOL> def clean_old_password ( self ) : <EOL> """<STR_LIT>""" <EOL> old_password = self . cleaned_data [ "<STR_LIT>" ] <EOL> if not self . user . check_password ( old_password ) : <EOL> raise forms . ValidationError ( <EOL> self . error_messages [ '<STR_LIT>' ] , <EOL> code = '<STR_LIT>' , <EOL> ) <EOL> return old_password <EOL> PasswordChangeForm . base_fields = SortedDict ( [ <EOL> ( k , PasswordChangeForm . base_fields [ k ] ) <EOL> for k in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> ] ) <EOL> class AdminPasswordChangeForm ( forms . Form ) : <EOL> """<STR_LIT>""" <EOL> error_messages = { <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" ) , <EOL> } <EOL> password1 = forms . CharField ( label = _ ( "<STR_LIT>" ) , <EOL> widget = forms . PasswordInput ) <EOL> password2 = forms . CharField ( label = _ ( "<STR_LIT>" ) , <EOL> widget = forms . PasswordInput ) <EOL> def __init__ ( self , user , * args , ** kwargs ) : <EOL> self . user = user <EOL> super ( AdminPasswordChangeForm , self ) . __init__ ( * args , ** kwargs ) <EOL> def clean_password2 ( self ) : <EOL> password1 = self . cleaned_data . get ( '<STR_LIT>' ) <EOL> password2 = self . cleaned_data . get ( '<STR_LIT>' ) <EOL> if password1 and password2 : <EOL> if password1 != password2 : <EOL> raise forms . ValidationError ( <EOL> self . error_messages [ '<STR_LIT>' ] , <EOL> code = '<STR_LIT>' , <EOL> ) <EOL> return password2 <EOL> def save ( self , commit = True ) : <EOL> """<STR_LIT>""" <EOL> self . user . set_password ( self . cleaned_data [ "<STR_LIT>" ] ) <EOL> if commit : <EOL> self . user . save ( ) <EOL> return self . user <EOL> def _get_changed_data ( self ) : <EOL> data = super ( AdminPasswordChangeForm , self ) . changed_data <EOL> for name in self . fields . keys ( ) : <EOL> if name not in data : <EOL> return [ ] <EOL> return [ '<STR_LIT:password>' ] <EOL> changed_data = property ( _get_changed_data ) </s>
