<s> import string <EOL> from django import forms <EOL> from django . core . validators import validate_email <EOL> from django . forms . widgets import CheckboxInput <EOL> from django . utils . encoding import force_text <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from . conf import settings <EOL> class LengthValidator ( object ) : <EOL> code = '<STR_LIT>' <EOL> def __init__ ( self , min_length = None , max_length = None ) : <EOL> self . min_length = min_length or settings . USERS_PASSWORD_MIN_LENGTH <EOL> self . max_length = max_length or settings . USERS_PASSWORD_MAX_LENGTH <EOL> def __call__ ( self , value ) : <EOL> if self . min_length and len ( value ) < self . min_length : <EOL> raise forms . ValidationError ( <EOL> _ ( '<STR_LIT>' ) % self . min_length , <EOL> code = self . code ) <EOL> elif self . max_length and len ( value ) > self . max_length : <EOL> raise forms . ValidationError ( <EOL> _ ( '<STR_LIT>' ) % self . max_length , <EOL> code = self . code ) <EOL> length_validator = LengthValidator ( ) <EOL> class ComplexityValidator ( object ) : <EOL> code = '<STR_LIT>' <EOL> message = _ ( '<STR_LIT>' ) <EOL> def __init__ ( self ) : <EOL> self . password_policy = settings . USERS_PASSWORD_POLICY <EOL> def __call__ ( self , value ) : <EOL> if not settings . USERS_CHECK_PASSWORD_COMPLEXITY : <EOL> return <EOL> uppercase , lowercase , digits , non_ascii , punctuation = set ( ) , set ( ) , set ( ) , set ( ) , set ( ) <EOL> for char in value : <EOL> if char . isupper ( ) : <EOL> uppercase . add ( char ) <EOL> elif char . islower ( ) : <EOL> lowercase . add ( char ) <EOL> elif char . isdigit ( ) : <EOL> digits . add ( char ) <EOL> elif char in string . punctuation : <EOL> punctuation . add ( char ) <EOL> else : <EOL> non_ascii . add ( char ) <EOL> if len ( uppercase ) < self . password_policy . get ( '<STR_LIT>' , <NUM_LIT:0> ) : <EOL> raise forms . ValidationError ( <EOL> self . message % _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % self . password_policy , <EOL> code = self . code ) <EOL> elif len ( lowercase ) < self . password_policy . get ( '<STR_LIT>' , <NUM_LIT:0> ) : <EOL> raise forms . ValidationError ( <EOL> self . message % _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % self . password_policy , <EOL> code = self . code ) <EOL> elif len ( digits ) < self . password_policy . get ( '<STR_LIT>' , <NUM_LIT:0> ) : <EOL> raise forms . ValidationError ( <EOL> self . message % _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % self . password_policy , <EOL> code = self . code ) <EOL> elif len ( punctuation ) < self . password_policy . get ( '<STR_LIT>' , <NUM_LIT:0> ) : <EOL> raise forms . ValidationError ( <EOL> self . message % _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % self . password_policy , <EOL> code = self . code ) <EOL> complexity_validator = ComplexityValidator ( ) <EOL> class PasswordField ( forms . CharField ) : <EOL> widget = forms . PasswordInput ( ) <EOL> default_validators = [ length_validator , complexity_validator , ] <EOL> class HoneyPotField ( forms . BooleanField ) : <EOL> widget = CheckboxInput <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( HoneyPotField , self ) . __init__ ( * args , ** kwargs ) <EOL> self . required = False <EOL> if not self . label : <EOL> self . label = _ ( '<STR_LIT>' ) <EOL> if not self . help_text : <EOL> self . help_text = _ ( '<STR_LIT>' ) <EOL> def validate ( self , value ) : <EOL> if value : <EOL> raise forms . ValidationError ( _ ( '<STR_LIT>' ) ) <EOL> class EmailDomainValidator ( object ) : <EOL> message = _ ( '<STR_LIT>' ) <EOL> code = '<STR_LIT>' <EOL> def __init__ ( self , ) : <EOL> self . domain_blacklist = settings . USERS_EMAIL_DOMAINS_BLACKLIST <EOL> self . domain_whitelist = settings . USERS_EMAIL_DOMAINS_WHITELIST <EOL> def __call__ ( self , value ) : <EOL> if not settings . USERS_VALIDATE_EMAIL_DOMAIN : <EOL> return <EOL> if not value or '<STR_LIT:@>' not in value : <EOL> raise forms . ValidationError ( _ ( '<STR_LIT>' ) , code = self . code ) <EOL> value = force_text ( value ) <EOL> user_part , domain_part = value . rsplit ( '<STR_LIT:@>' , <NUM_LIT:1> ) <EOL> if self . domain_blacklist and domain_part in self . domain_blacklist : <EOL> raise forms . ValidationError ( self . message % domain_part , code = self . code ) <EOL> if self . domain_whitelist and domain_part not in self . domain_whitelist : <EOL> raise forms . ValidationError ( self . message % domain_part , code = self . code ) <EOL> validate_email_domain = EmailDomainValidator ( ) <EOL> class UsersEmailField ( forms . EmailField ) : <EOL> default_validators = [ validate_email , validate_email_domain ] </s>
