<s> """<STR_LIT>""" <EOL> import smtplib <EOL> import bcrypt <EOL> from email . mime . multipart import MIMEMultipart <EOL> from email . mime . text import MIMEText <EOL> from email . utils import formataddr <EOL> import api <EOL> from api . common import check , validate , safe_fail , WebException <EOL> from voluptuous import Required , Length , Schema <EOL> from datetime import datetime <EOL> enable_email = False <EOL> smtp_url = '<STR_LIT>' <EOL> email_username = '<STR_LIT>' <EOL> email_password = '<STR_LIT>' <EOL> from_addr = '<STR_LIT>' <EOL> from_name = '<STR_LIT>' <EOL> password_reset_request_schema = Schema ( { <EOL> Required ( '<STR_LIT:username>' ) : check ( <EOL> ( "<STR_LIT>" , [ str , Length ( min = <NUM_LIT:3> , max = <NUM_LIT:20> ) ] ) , <EOL> ) <EOL> } ) <EOL> password_reset_schema = Schema ( { <EOL> Required ( "<STR_LIT>" ) : check ( <EOL> ( "<STR_LIT>" , [ str , Length ( max = <NUM_LIT:100> ) ] ) <EOL> ) , <EOL> Required ( '<STR_LIT:password>' ) : check ( <EOL> ( "<STR_LIT>" , [ str , Length ( min = <NUM_LIT:3> , max = <NUM_LIT:20> ) ] ) <EOL> ) <EOL> } ) <EOL> def send_email ( recipient , subject , body ) : <EOL> """<STR_LIT>""" <EOL> if enable_email : <EOL> msg = MIMEMultipart ( '<STR_LIT>' ) <EOL> msg [ '<STR_LIT>' ] = subject <EOL> msg [ '<STR_LIT>' ] = formataddr ( ( from_name , from_addr ) ) <EOL> msg [ '<STR_LIT>' ] = recipient <EOL> part1 = MIMEText ( body , '<STR_LIT>' ) <EOL> msg . attach ( part1 ) <EOL> s = smtplib . SMTP_SSL ( smtp_url ) <EOL> s . login ( email_username , email_password ) <EOL> s . sendmail ( from_addr , recipient , msg . as_string ( ) ) <EOL> s . quit ( ) <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> def send_email_to_list ( recipients , subject , body ) : <EOL> """<STR_LIT>""" <EOL> if recipients is not None : <EOL> for recipient in recipients : <EOL> send_email ( recipient , subject , body ) <EOL> def reset_password ( token , password , confirm_password ) : <EOL> """<STR_LIT>""" <EOL> validate ( password_reset_schema , { "<STR_LIT>" : token , "<STR_LIT:password>" : password } ) <EOL> user = api . user . find_user_by_reset_token ( token ) <EOL> api . user . update_password_request ( { <EOL> "<STR_LIT>" : password , <EOL> "<STR_LIT>" : confirm_password <EOL> } , uid = user [ '<STR_LIT>' ] ) <EOL> api . user . delete_password_reset_token ( user [ '<STR_LIT>' ] ) <EOL> def request_password_reset ( username ) : <EOL> """<STR_LIT>""" <EOL> validate ( password_reset_request_schema , { "<STR_LIT:username>" : username } ) <EOL> user = safe_fail ( api . user . get_user , name = username ) <EOL> if user is None : <EOL> raise WebException ( "<STR_LIT>" . format ( username ) ) <EOL> token = api . common . token ( ) <EOL> api . user . set_password_reset_token ( user [ '<STR_LIT>' ] , token ) <EOL> msgBody = """<STR_LIT>""" . format ( api . config . competition_name , api . config . competition_urls [ <NUM_LIT:0> ] , username , token ) <EOL> send_email ( user [ '<STR_LIT:email>' ] , "<STR_LIT>" . format ( api . config . competition_name ) , msgBody ) <EOL> def check_competition_active ( ) : <EOL> """<STR_LIT>""" <EOL> return api . config . start_time . timestamp ( ) < datetime . utcnow ( ) . timestamp ( ) < api . config . end_time . timestamp ( ) </s>
