<s> """<STR_LIT>""" <EOL> import operator <EOL> import random <EOL> from django . conf import settings <EOL> from django . contrib . auth import forms as auth_forms <EOL> import floppyforms . __future__ as forms <EOL> from registration import forms as registration_forms <EOL> from moztrap import model <EOL> def check_password ( pw ) : <EOL> """<STR_LIT>""" <EOL> if len ( pw ) < settings . MINIMUM_PASSWORD_CHARS : <EOL> raise forms . ValidationError ( <EOL> "<STR_LIT>" . format ( <EOL> settings . MINIMUM_PASSWORD_CHARS ) <EOL> ) <EOL> if ( settings . PASSWORD_REQUIRE_ALPHA_NUMERIC and <EOL> not ( any ( c . isdigit ( ) for c in pw ) and any ( c . isalpha ( ) for c in pw ) ) <EOL> ) : <EOL> raise forms . ValidationError ( <EOL> "<STR_LIT>" ) <EOL> if pw in settings . FORBIDDEN_PASSWORDS : <EOL> raise forms . ValidationError ( <EOL> "<STR_LIT>" ) <EOL> return pw <EOL> class SetPasswordFormMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> super ( SetPasswordFormMixin , self ) . __init__ ( * args , ** kwargs ) <EOL> self . fields [ "<STR_LIT>" ] . label = "<STR_LIT>" <EOL> def clean_new_password1 ( self ) : <EOL> """<STR_LIT>""" <EOL> return check_password ( self . cleaned_data [ "<STR_LIT>" ] ) <EOL> class SetPasswordForm ( SetPasswordFormMixin , auth_forms . SetPasswordForm ) : <EOL> pass <EOL> class ChangePasswordForm ( SetPasswordFormMixin , auth_forms . PasswordChangeForm ) : <EOL> pass <EOL> class RegistrationForm ( registration_forms . RegistrationForm ) : <EOL> """<STR_LIT>""" <EOL> def clean_password1 ( self ) : <EOL> """<STR_LIT>""" <EOL> return check_password ( self . cleaned_data [ "<STR_LIT>" ] ) <EOL> def clean_email ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> model . User . objects . get ( email = self . cleaned_data [ "<STR_LIT:email>" ] ) <EOL> except model . User . DoesNotExist : <EOL> return self . cleaned_data [ "<STR_LIT:email>" ] <EOL> raise forms . ValidationError ( u"<STR_LIT>" ) <EOL> class PasswordResetForm ( auth_forms . PasswordResetForm ) : <EOL> """<STR_LIT>""" <EOL> def clean_email ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . cleaned_data [ "<STR_LIT:email>" ] <EOL> def save ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> email = self . cleaned_data [ "<STR_LIT:email>" ] <EOL> self . users_cache = model . User . objects . filter ( <EOL> email__iexact = email , is_active = True ) <EOL> return super ( PasswordResetForm , self ) . save ( * args , ** kwargs ) <EOL> class SetUsernameForm ( forms . ModelForm ) : <EOL> """<STR_LIT>""" <EOL> username = forms . RegexField ( <EOL> regex = r'<STR_LIT>' , <EOL> max_length = <NUM_LIT:30> , <EOL> label = "<STR_LIT>" , <EOL> error_messages = { <EOL> "<STR_LIT>" : ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> } , <EOL> ) <EOL> class Meta : <EOL> model = model . User <EOL> fields = [ "<STR_LIT:username>" ] <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> super ( SetUsernameForm , self ) . __init__ ( * args , ** kwargs ) <EOL> self . initial [ "<STR_LIT:username>" ] = "<STR_LIT>" <EOL> OPERATORS = { <EOL> "<STR_LIT>" : operator . add , <EOL> "<STR_LIT>" : operator . sub , <EOL> "<STR_LIT>" : operator . mul , <EOL> } <EOL> CAPTCHA_SESSION_KEY = "<STR_LIT>" <EOL> class CaptchaAuthenticationForm ( auth_forms . AuthenticationForm ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , request = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> super ( CaptchaAuthenticationForm , self ) . __init__ ( * args , ** kwargs ) <EOL> self . captcha_answer = request . session . get ( CAPTCHA_SESSION_KEY ) <EOL> if getattr ( request , "<STR_LIT>" , False ) : <EOL> a , b = random . randint ( <NUM_LIT:1> , <NUM_LIT:9> ) , random . randint ( <NUM_LIT:1> , <NUM_LIT:9> ) <EOL> if b > a : <EOL> a , b = b , a <EOL> opname , op = random . choice ( OPERATORS . items ( ) ) <EOL> request . session [ CAPTCHA_SESSION_KEY ] = op ( a , b ) <EOL> self . fields [ "<STR_LIT>" ] = forms . IntegerField ( <EOL> widget = forms . TextInput , <EOL> required = False , <EOL> label = u"<STR_LIT>" . format ( a , opname , b ) , <EOL> ) <EOL> def clean_captcha ( self ) : <EOL> """<STR_LIT>""" <EOL> answer = self . cleaned_data . get ( "<STR_LIT>" ) <EOL> if answer != self . captcha_answer : <EOL> raise forms . ValidationError ( <EOL> "<STR_LIT>" ) </s>
