<s> from datetime import date <EOL> from django . contrib . auth . signals import user_logged_in , user_login_failed <EOL> from django . dispatch import receiver <EOL> from corehq . apps . users . models import CouchUser <EOL> def clear_login_attempts ( user ) : <EOL> if user and user . is_web_user ( ) and user . login_attempts > <NUM_LIT:0> : <EOL> user . login_attempts = <NUM_LIT:0> <EOL> user . save ( ) <EOL> @ receiver ( user_logged_in ) <EOL> def clear_failed_logins_and_unlock_account ( sender , request , user , ** kwargs ) : <EOL> couch_user = CouchUser . from_django_user ( user ) <EOL> clear_login_attempts ( couch_user ) <EOL> @ receiver ( user_login_failed ) <EOL> def add_failed_attempt ( sender , credentials , ** kwargs ) : <EOL> user = CouchUser . get_by_username ( credentials [ '<STR_LIT:username>' ] ) <EOL> if user and user . is_web_user ( ) : <EOL> if user . is_locked_out ( ) : <EOL> return <EOL> if user . attempt_date == date . today ( ) : <EOL> user . login_attempts += <NUM_LIT:1> <EOL> else : <EOL> user . login_attempts = <NUM_LIT:1> <EOL> user . attempt_date = date . today ( ) <EOL> user . save ( ) </s>
