<s> from django . core . exceptions import ValidationError <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from useradmin . conf import PASSWORD_POLICY <EOL> import re <EOL> _PASSWORD_POLICY = None <EOL> class PasswordPolicy ( object ) : <EOL> def __init__ ( self , is_enabled , rule , hint , error_message ) : <EOL> self . _is_enabled = is_enabled <EOL> self . _rule = rule <EOL> self . _hint = hint <EOL> self . _message = error_message <EOL> self . _valid_pattern = re . compile ( self . _rule ) <EOL> def validate_password ( self , password ) : <EOL> if self . _is_enabled and not self . _valid_pattern . match ( password ) : <EOL> raise ValidationError ( _ ( self . _message ) ) <EOL> @ property <EOL> def password_hint ( self ) : <EOL> return _ ( self . _hint ) <EOL> @ property <EOL> def is_enabled ( self ) : <EOL> return self . _is_enabled <EOL> def get_password_policy ( ) : <EOL> global _PASSWORD_POLICY <EOL> if _PASSWORD_POLICY is None : <EOL> _PASSWORD_POLICY = PasswordPolicy ( is_enabled = PASSWORD_POLICY . IS_ENABLED . get ( ) , <EOL> rule = PASSWORD_POLICY . PWD_RULE . get ( ) , <EOL> hint = PASSWORD_POLICY . PWD_HINT . get ( ) , <EOL> error_message = PASSWORD_POLICY . PWD_ERROR_MESSAGE . get ( ) ) <EOL> return _PASSWORD_POLICY <EOL> def reset_password_policy ( ) : <EOL> global _PASSWORD_POLICY <EOL> _PASSWORD_POLICY = None <EOL> def get_password_validators ( ) : <EOL> def validate_against_policy ( password ) : <EOL> get_password_policy ( ) . validate_password ( password ) <EOL> return [ validate_against_policy ] <EOL> def is_password_policy_enabled ( ) : <EOL> return get_password_policy ( ) . is_enabled <EOL> def get_password_hint ( ) : <EOL> return get_password_policy ( ) . password_hint </s>
