<s> from django import forms <EOL> from django . core . validators import validate_email <EOL> from django . db . models import Q <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . conf import settings <EOL> from . utils import get_user_model <EOL> class PasswordRecoveryForm ( forms . Form ) : <EOL> username_or_email = forms . CharField ( ) <EOL> error_messages = { <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" ) , <EOL> } <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . case_sensitive = kwargs . pop ( '<STR_LIT>' , True ) <EOL> search_fields = kwargs . pop ( '<STR_LIT>' , ( '<STR_LIT:username>' , '<STR_LIT:email>' ) ) <EOL> super ( PasswordRecoveryForm , self ) . __init__ ( * args , ** kwargs ) <EOL> message = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if len ( search_fields ) not in ( <NUM_LIT:1> , <NUM_LIT:2> ) : <EOL> raise ValueError ( message ) <EOL> for field in search_fields : <EOL> if field not in [ '<STR_LIT:username>' , '<STR_LIT:email>' ] : <EOL> raise ValueError ( message ) <EOL> labels = { <EOL> '<STR_LIT:username>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT:email>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> } <EOL> User = get_user_model ( ) <EOL> if getattr ( User , '<STR_LIT>' , '<STR_LIT:username>' ) == '<STR_LIT:email>' : <EOL> self . label_key = '<STR_LIT:email>' <EOL> elif len ( search_fields ) == <NUM_LIT:1> : <EOL> self . label_key = search_fields [ <NUM_LIT:0> ] <EOL> else : <EOL> self . label_key = '<STR_LIT>' <EOL> self . fields [ '<STR_LIT>' ] . label = labels [ self . label_key ] <EOL> def clean_username_or_email ( self ) : <EOL> username = self . cleaned_data [ '<STR_LIT>' ] <EOL> cleaner = getattr ( self , '<STR_LIT>' % self . label_key ) <EOL> self . cleaned_data [ '<STR_LIT:user>' ] = user = cleaner ( username ) <EOL> user_is_active = getattr ( user , '<STR_LIT>' , True ) <EOL> recovery_only_active_users = getattr ( settings , <EOL> '<STR_LIT>' , <EOL> False ) <EOL> if recovery_only_active_users and not user_is_active : <EOL> raise forms . ValidationError ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> return username <EOL> def get_user_by_username ( self , username ) : <EOL> key = '<STR_LIT>' % ( '<STR_LIT>' if self . case_sensitive else '<STR_LIT:i>' ) <EOL> User = get_user_model ( ) <EOL> try : <EOL> user = User . _default_manager . get ( ** { key : username } ) <EOL> except User . DoesNotExist : <EOL> raise forms . ValidationError ( self . error_messages [ '<STR_LIT>' ] , <EOL> code = '<STR_LIT>' ) <EOL> return user <EOL> def get_user_by_email ( self , email ) : <EOL> validate_email ( email ) <EOL> key = '<STR_LIT>' % ( '<STR_LIT>' if self . case_sensitive else '<STR_LIT:i>' ) <EOL> User = get_user_model ( ) <EOL> try : <EOL> user = User . _default_manager . get ( ** { key : email } ) <EOL> except User . DoesNotExist : <EOL> raise forms . ValidationError ( self . error_messages [ '<STR_LIT>' ] , <EOL> code = '<STR_LIT>' ) <EOL> return user <EOL> def get_user_by_both ( self , username ) : <EOL> key = '<STR_LIT>' <EOL> key = key % '<STR_LIT>' if self . case_sensitive else key % '<STR_LIT:i>' <EOL> f = lambda field : Q ( ** { field + key : username } ) <EOL> filters = f ( '<STR_LIT:username>' ) | f ( '<STR_LIT:email>' ) <EOL> User = get_user_model ( ) <EOL> try : <EOL> user = User . _default_manager . get ( filters ) <EOL> except User . DoesNotExist : <EOL> raise forms . ValidationError ( self . error_messages [ '<STR_LIT>' ] , <EOL> code = '<STR_LIT>' ) <EOL> except User . MultipleObjectsReturned : <EOL> raise forms . ValidationError ( _ ( "<STR_LIT>" ) ) <EOL> return user <EOL> class PasswordResetForm ( forms . Form ) : <EOL> password1 = forms . CharField ( <EOL> label = _ ( '<STR_LIT>' ) , <EOL> widget = forms . PasswordInput , <EOL> ) <EOL> password2 = forms . CharField ( <EOL> label = _ ( '<STR_LIT>' ) , <EOL> widget = forms . PasswordInput , <EOL> ) <EOL> error_messages = { <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" ) , <EOL> } <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . user = kwargs . pop ( '<STR_LIT:user>' ) <EOL> super ( PasswordResetForm , self ) . __init__ ( * args , ** kwargs ) <EOL> def clean_password2 ( self ) : <EOL> password1 = self . cleaned_data . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> password2 = self . cleaned_data [ '<STR_LIT>' ] <EOL> if not password1 == password2 : <EOL> raise forms . ValidationError ( <EOL> self . error_messages [ '<STR_LIT>' ] , <EOL> code = '<STR_LIT>' ) <EOL> return password2 <EOL> def save ( self , commit = True ) : <EOL> self . user . set_password ( self . cleaned_data [ '<STR_LIT>' ] ) <EOL> if commit : <EOL> get_user_model ( ) . _default_manager . filter ( pk = self . user . pk ) . update ( <EOL> password = self . user . password , <EOL> ) <EOL> return self . user </s>
