<s> """<STR_LIT>""" <EOL> import time <EOL> import datetime <EOL> import parsedatetime as pdt <EOL> import requests <EOL> from operator import itemgetter <EOL> import utilities <EOL> def index ( ) : <EOL> """<STR_LIT>""" <EOL> if session [ "<STR_LIT>" ] : <EOL> session . flash = "<STR_LIT>" <EOL> redirect ( URL ( "<STR_LIT:default>" , "<STR_LIT>" , args = [ <NUM_LIT:1> ] ) ) <EOL> return dict ( ) <EOL> @ auth . requires_login ( ) <EOL> def notifications ( ) : <EOL> """<STR_LIT>""" <EOL> if session [ "<STR_LIT>" ] is None : <EOL> redirect ( URL ( "<STR_LIT:default>" , "<STR_LIT:index>" ) ) <EOL> ftable = db . friends <EOL> atable = db . auth_user <EOL> cftable = db . custom_friend <EOL> query = ( ftable . user_id == session [ "<STR_LIT>" ] ) <EOL> rows = db ( query ) . select ( atable . first_name , <EOL> atable . last_name , <EOL> atable . stopstalk_handle , <EOL> join = ftable . on ( ftable . friend_id == atable . id ) ) <EOL> handles = [ ] <EOL> complete_dict = { } <EOL> friends_stopstalk_handles = [ ] <EOL> for user in rows : <EOL> complete_dict [ user . stopstalk_handle ] = [ ] <EOL> friends_stopstalk_handles . append ( "<STR_LIT:'>" + user . stopstalk_handle + "<STR_LIT:'>" ) <EOL> handles . append ( ( user . stopstalk_handle , <EOL> user . first_name + "<STR_LIT:U+0020>" + user . last_name , <EOL> user . stopstalk_handle ) ) <EOL> query = ( cftable . user_id == session [ "<STR_LIT>" ] ) <EOL> rows = db ( query ) . select ( cftable . first_name , <EOL> cftable . last_name , <EOL> cftable . duplicate_cu , <EOL> cftable . stopstalk_handle ) <EOL> for user in rows : <EOL> name = user . first_name + "<STR_LIT:U+0020>" + user . last_name <EOL> actual_handle = user . stopstalk_handle <EOL> if user . duplicate_cu : <EOL> stopstalk_handle = user . duplicate_cu . stopstalk_handle <EOL> else : <EOL> stopstalk_handle = user . stopstalk_handle <EOL> handles . append ( ( stopstalk_handle , name , actual_handle ) ) <EOL> complete_dict [ stopstalk_handle ] = [ ] <EOL> friends_stopstalk_handles . append ( "<STR_LIT:'>" + stopstalk_handle + "<STR_LIT:'>" ) <EOL> if friends_stopstalk_handles == [ ] : <EOL> friends_stopstalk_handles = [ "<STR_LIT>" ] <EOL> sql_query = """<STR_LIT>""" % ( "<STR_LIT:U+002CU+0020>" . join ( friends_stopstalk_handles ) ) <EOL> user_rows = db . executesql ( sql_query ) <EOL> for user in user_rows : <EOL> if complete_dict [ user [ <NUM_LIT:0> ] ] != [ ] : <EOL> complete_dict [ user [ <NUM_LIT:0> ] ] . append ( ( user [ <NUM_LIT:1> ] , user [ <NUM_LIT:2> ] ) ) <EOL> else : <EOL> complete_dict [ user [ <NUM_LIT:0> ] ] = [ ( user [ <NUM_LIT:1> ] , user [ <NUM_LIT:2> ] ) ] <EOL> users_on_day_streak = [ ] <EOL> users_on_submission_streak = [ ] <EOL> for handle in handles : <EOL> curr_streak = utilities . get_max_streak ( complete_dict [ handle [ <NUM_LIT:0> ] ] ) [ <NUM_LIT:2> ] <EOL> curr_accepted_streak = utilities . get_accepted_streak ( handle [ <NUM_LIT:0> ] ) <EOL> if curr_streak : <EOL> users_on_day_streak . append ( ( handle , curr_streak ) ) <EOL> if curr_accepted_streak : <EOL> users_on_submission_streak . append ( ( handle , curr_accepted_streak ) ) <EOL> users_on_day_streak . sort ( key = lambda k : k [ <NUM_LIT:1> ] , reverse = True ) <EOL> users_on_submission_streak . sort ( key = lambda k : k [ <NUM_LIT:1> ] , reverse = True ) <EOL> table1 = TABLE ( THEAD ( TR ( TH ( STRONG ( "<STR_LIT>" ) ) , <EOL> TH ( STRONG ( "<STR_LIT>" ) ) ) ) , <EOL> _class = "<STR_LIT>" ) <EOL> table2 = TABLE ( THEAD ( TR ( TH ( STRONG ( "<STR_LIT>" ) ) , <EOL> TH ( STRONG ( "<STR_LIT>" ) ) ) ) , <EOL> _class = "<STR_LIT>" ) <EOL> tbody1 = TBODY ( ) <EOL> tbody2 = TBODY ( ) <EOL> for users in users_on_day_streak : <EOL> handle = users [ <NUM_LIT:0> ] <EOL> curr_streak = users [ <NUM_LIT:1> ] <EOL> tr = TR ( TD ( ( A ( handle [ <NUM_LIT:1> ] , <EOL> _href = URL ( "<STR_LIT:user>" , "<STR_LIT>" , args = [ handle [ <NUM_LIT:2> ] ] ) , <EOL> _target = "<STR_LIT>" ) ) ) , <EOL> TD ( str ( curr_streak ) + "<STR_LIT:U+0020>" , <EOL> I ( _class = "<STR_LIT>" , <EOL> _style = "<STR_LIT>" ) , <EOL> _class = "<STR_LIT>" ) ) <EOL> tbody1 . append ( tr ) <EOL> table1 . append ( tbody1 ) <EOL> if len ( users_on_day_streak ) == <NUM_LIT:0> : <EOL> table1 = H6 ( "<STR_LIT>" , _class = "<STR_LIT>" ) <EOL> for users in users_on_submission_streak : <EOL> handle = users [ <NUM_LIT:0> ] <EOL> curr_accepted_streak = users [ <NUM_LIT:1> ] <EOL> tr = TR ( TD ( ( A ( handle [ <NUM_LIT:1> ] , <EOL> _href = URL ( "<STR_LIT:user>" , "<STR_LIT>" , args = [ handle [ <NUM_LIT:2> ] ] ) , <EOL> _target = "<STR_LIT>" ) ) ) , <EOL> TD ( str ( curr_accepted_streak ) + "<STR_LIT:U+0020>" , <EOL> I ( _class = "<STR_LIT>" , <EOL> _style = "<STR_LIT>" ) , <EOL> _class = "<STR_LIT>" ) ) <EOL> tbody2 . append ( tr ) <EOL> table2 . append ( tbody2 ) <EOL> if len ( users_on_submission_streak ) == <NUM_LIT:0> : <EOL> table2 = H6 ( "<STR_LIT>" , _class = "<STR_LIT>" ) <EOL> return dict ( table1 = table1 , <EOL> table2 = table2 ) <EOL> @ auth . requires_login ( ) <EOL> def unsubscribe ( ) : <EOL> utable = db . unsubscriber <EOL> utable . email . default = session . auth . user . email <EOL> utable . email . writable = False <EOL> already_unsubscribed = False <EOL> query = ( utable . email == session . auth . user . email ) <EOL> rows = db ( query ) . select ( ) <EOL> if len ( rows ) != <NUM_LIT:0> : <EOL> already_unsubscribed = True <EOL> form = SQLFORM ( utable , <EOL> submit_button = "<STR_LIT>" ) <EOL> if form . validate ( ) : <EOL> for row in rows : <EOL> row . delete_record ( ) <EOL> session . flash = "<STR_LIT>" <EOL> redirect ( URL ( "<STR_LIT:default>" , "<STR_LIT>" ) ) <EOL> elif form . errors : <EOL> response . flash = "<STR_LIT>" <EOL> else : <EOL> form = SQLFORM ( utable , <EOL> submit_button = "<STR_LIT>" ) <EOL> if form . process ( ) . accepted : <EOL> response . flash = "<STR_LIT>" <EOL> redirect ( URL ( "<STR_LIT:default>" , "<STR_LIT>" ) ) <EOL> elif form . errors : <EOL> response . flash = "<STR_LIT>" <EOL> return dict ( form = form , <EOL> already_unsubscribed = already_unsubscribed ) <EOL> @ auth . requires_login ( ) <EOL> def my_friends ( ) : <EOL> """<STR_LIT>""" <EOL> ftable = db . friends <EOL> rows = db ( ftable . user_id == session . user_id ) . select ( ftable . friend_id ) <EOL> table = TABLE ( THEAD ( TR ( TH ( "<STR_LIT>" ) , <EOL> TH ( "<STR_LIT>" ) , <EOL> TH ( "<STR_LIT>" ) , <EOL> TH ( "<STR_LIT>" ) , <EOL> TH ( "<STR_LIT>" ) ) ) , <EOL> _class = "<STR_LIT>" ) <EOL> tbody = TBODY ( ) <EOL> check = I ( _class = "<STR_LIT>" , <EOL> _style = "<STR_LIT>" ) <EOL> cross = I ( _class = "<STR_LIT>" , <EOL> _style = "<STR_LIT>" ) <EOL> valid_friends = <NUM_LIT:0> <EOL> for row in rows : <EOL> claimable = True <EOL> thisfriend = row . friend_id <EOL> name = thisfriend . first_name + "<STR_LIT:U+0020>" + thisfriend . last_name <EOL> tr = TR ( TD ( A ( name , _href = URL ( "<STR_LIT:user>" , "<STR_LIT>" , <EOL> args = thisfriend . stopstalk_handle ) ) ) ) <EOL> claimable &= thisfriend . authentic <EOL> if thisfriend . authentic : <EOL> tr . append ( check ) <EOL> else : <EOL> tr . append ( cross ) <EOL> query = ( ftable . user_id == thisfriend ) <EOL> friends_of_friends = db ( query ) . select ( ftable . friend_id ) <EOL> institute_friends = <NUM_LIT:0> <EOL> for fof in friends_of_friends : <EOL> if fof . friend_id . institute == thisfriend . institute and fof . friend_id . institute != "<STR_LIT>" : <EOL> institute_friends += <NUM_LIT:1> <EOL> if institute_friends > <NUM_LIT:1> : <EOL> tr . append ( TD ( check ) ) <EOL> tr . append ( TD ( check ) ) <EOL> else : <EOL> if institute_friends == <NUM_LIT:0> : <EOL> claimable &= False <EOL> tr . append ( cross ) <EOL> if len ( friends_of_friends ) >= <NUM_LIT:2> : <EOL> tr . append ( check ) <EOL> else : <EOL> tr . append ( cross ) <EOL> else : <EOL> tr . append ( check ) <EOL> if len ( friends_of_friends ) >= <NUM_LIT:3> : <EOL> claimable &= True <EOL> tr . append ( check ) <EOL> else : <EOL> claimable &= False <EOL> tr . append ( cross ) <EOL> if claimable : <EOL> valid_friends += <NUM_LIT:1> <EOL> tr . append ( TD ( I ( _class = "<STR_LIT>" ) ) ) <EOL> else : <EOL> tr . append ( TD ( ) ) <EOL> tbody . append ( tr ) <EOL> table . append ( tbody ) <EOL> claimable_stickers = valid_friends / <NUM_LIT:3> <EOL> stickers = [ claimable_stickers / <NUM_LIT:3> ] * <NUM_LIT:3> <EOL> residue = claimable_stickers - ( claimable_stickers / <NUM_LIT:3> ) * <NUM_LIT:3> <EOL> i = <NUM_LIT:0> <EOL> while residue : <EOL> stickers [ i ] += <NUM_LIT:1> <EOL> i += <NUM_LIT:1> <EOL> residue -= <NUM_LIT:1> <EOL> return dict ( table = DIV ( table , _class = "<STR_LIT>" ) , <EOL> claimable_stickers = claimable_stickers , <EOL> stickers = stickers ) <EOL> def sort_contests ( a , b ) : <EOL> if a [ "<STR_LIT>" ] and b [ "<STR_LIT>" ] : <EOL> return a [ "<STR_LIT:end>" ] < b [ "<STR_LIT:end>" ] <EOL> elif a [ "<STR_LIT>" ] and b [ "<STR_LIT>" ] == False : <EOL> return False <EOL> elif a [ "<STR_LIT>" ] == False and b [ "<STR_LIT>" ] : <EOL> return True <EOL> else : <EOL> return a [ "<STR_LIT:start>" ] < b [ "<STR_LIT:start>" ] <EOL> def contests ( ) : <EOL> """<STR_LIT>""" <EOL> today = datetime . datetime . today ( ) <EOL> today = datetime . datetime . strptime ( str ( today ) [ : - <NUM_LIT:7> ] , <EOL> "<STR_LIT>" ) <EOL> start_date = today . date ( ) <EOL> end_date = start_date + datetime . timedelta ( <NUM_LIT> ) <EOL> url = "<STR_LIT>" <EOL> response = requests . get ( url ) <EOL> if response . status_code == <NUM_LIT:200> : <EOL> response = response . json ( ) [ "<STR_LIT:result>" ] <EOL> else : <EOL> return dict ( ) <EOL> ongoing = response [ "<STR_LIT>" ] <EOL> upcoming = response [ "<STR_LIT>" ] <EOL> contests = [ ] <EOL> cal = pdt . Calendar ( ) <EOL> table = TABLE ( _class = "<STR_LIT>" ) <EOL> thead = THEAD ( TR ( TH ( "<STR_LIT>" ) , <EOL> TH ( "<STR_LIT>" ) , <EOL> TH ( "<STR_LIT>" ) , <EOL> TH ( "<STR_LIT>" ) , <EOL> TH ( "<STR_LIT>" ) , <EOL> TH ( "<STR_LIT>" ) ) ) <EOL> table . append ( thead ) <EOL> tbody = TBODY ( ) <EOL> for i in ongoing : <EOL> if i [ "<STR_LIT>" ] in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> continue <EOL> try : <EOL> endtime = datetime . datetime . strptime ( i [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" ) <EOL> except ValueError : <EOL> continue <EOL> tr = TR ( ) <EOL> span = SPAN ( _class = "<STR_LIT>" , <EOL> data = { "<STR_LIT>" : "<STR_LIT:right>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" } , <EOL> _style = "<STR_LIT>" + "<STR_LIT>" + "<STR_LIT>" + "<STR_LIT>" + "<STR_LIT>" ) <EOL> tr . append ( TD ( i [ "<STR_LIT:Name>" ] , span ) ) <EOL> tr . append ( TD ( i [ "<STR_LIT>" ] . capitalize ( ) ) ) <EOL> tr . append ( TD ( "<STR_LIT:->" ) ) <EOL> tr . append ( TD ( str ( endtime ) . replace ( "<STR_LIT:->" , "<STR_LIT:/>" ) , <EOL> _class = "<STR_LIT>" ) ) <EOL> tr . append ( TD ( A ( I ( _class = "<STR_LIT>" ) , <EOL> _class = "<STR_LIT>" , <EOL> _href = i [ "<STR_LIT:url>" ] , <EOL> data = { "<STR_LIT>" : "<STR_LIT:left>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" } , <EOL> _target = "<STR_LIT>" ) ) ) <EOL> tr . append ( TD ( BUTTON ( I ( _class = "<STR_LIT>" ) , <EOL> _class = "<STR_LIT>" , <EOL> data = { "<STR_LIT>" : "<STR_LIT:left>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" } ) ) ) <EOL> tbody . append ( tr ) <EOL> button_id = <NUM_LIT:1> <EOL> for i in upcoming : <EOL> if i [ "<STR_LIT>" ] in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> continue <EOL> start_time = datetime . datetime . strptime ( i [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" ) <EOL> tr = TR ( _id = "<STR_LIT>" + str ( button_id ) ) <EOL> tr . append ( TD ( i [ "<STR_LIT:Name>" ] ) ) <EOL> tr . append ( TD ( i [ "<STR_LIT>" ] . capitalize ( ) ) ) <EOL> tr . append ( TD ( str ( start_time ) ) ) <EOL> duration = i [ "<STR_LIT>" ] <EOL> duration = duration . replace ( "<STR_LIT>" , "<STR_LIT:d>" ) <EOL> duration = duration . replace ( "<STR_LIT>" , "<STR_LIT:d>" ) <EOL> tr . append ( TD ( duration ) ) <EOL> tr . append ( TD ( A ( I ( _class = "<STR_LIT>" ) , <EOL> _class = "<STR_LIT>" , <EOL> _href = i [ "<STR_LIT:url>" ] , <EOL> data = { "<STR_LIT>" : "<STR_LIT:left>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" } , <EOL> _target = "<STR_LIT>" ) ) ) <EOL> tr . append ( TD ( BUTTON ( I ( _class = "<STR_LIT>" ) , <EOL> _class = "<STR_LIT>" , <EOL> data = { "<STR_LIT>" : "<STR_LIT:left>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" } , <EOL> _id = "<STR_LIT>" + str ( button_id ) ) ) ) <EOL> tbody . append ( tr ) <EOL> button_id += <NUM_LIT:1> <EOL> table . append ( tbody ) <EOL> return dict ( table = table , upcoming = upcoming ) <EOL> def leaderboard ( ) : <EOL> """<STR_LIT>""" <EOL> specific_institute = False <EOL> atable = db . auth_user <EOL> cftable = db . custom_friend <EOL> global_leaderboard = False <EOL> if request . vars . has_key ( "<STR_LIT>" ) : <EOL> if request . vars [ "<STR_LIT>" ] == "<STR_LIT:True>" : <EOL> global_leaderboard = True <EOL> else : <EOL> if session . user_id is None : <EOL> response . flash = "<STR_LIT>" <EOL> global_leaderboard = True <EOL> else : <EOL> if session . user_id is None : <EOL> global_leaderboard = True <EOL> heading = "<STR_LIT>" <EOL> afields = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> cfields = afields + [ "<STR_LIT>" ] <EOL> aquery = ( atable . id > <NUM_LIT:0> ) <EOL> cquery = ( cftable . id > <NUM_LIT:0> ) <EOL> if global_leaderboard is False : <EOL> if session . user_id : <EOL> heading = "<STR_LIT>" <EOL> friends , cusfriends = utilities . get_friends ( session . user_id ) <EOL> custom_friends = [ x [ <NUM_LIT:0> ] for x in cusfriends ] <EOL> friends . append ( session . user_id ) <EOL> aquery &= ( atable . id . belongs ( friends ) ) <EOL> cquery &= ( cftable . id . belongs ( custom_friends ) ) <EOL> else : <EOL> aquery &= ( <NUM_LIT:1> == <NUM_LIT:0> ) <EOL> cquery &= ( <NUM_LIT:1> == <NUM_LIT:0> ) <EOL> if request . vars . has_key ( "<STR_LIT:q>" ) : <EOL> heading = "<STR_LIT>" <EOL> institute = request . vars [ "<STR_LIT:q>" ] <EOL> if institute != "<STR_LIT>" : <EOL> specific_institute = True <EOL> aquery &= ( atable . institute == institute ) <EOL> cquery &= ( cftable . institute == institute ) <EOL> reg_users = db ( aquery ) . select ( * afields ) <EOL> custom_users = db ( cquery ) . select ( * cfields ) <EOL> if specific_institute is False : <EOL> reg_users = db ( aquery ) . select ( * afields ) <EOL> custom_users = db ( cquery ) . select ( * cfields ) <EOL> solved_count = { } <EOL> sql = """<STR_LIT>""" <EOL> tmplist = db . executesql ( sql ) <EOL> for user in tmplist : <EOL> solved_count [ user [ <NUM_LIT:0> ] ] = user [ <NUM_LIT:1> ] <EOL> complete_dict = { } <EOL> friends_stopstalk_handles = [ ] <EOL> for x in reg_users : <EOL> friends_stopstalk_handles . append ( "<STR_LIT:'>" + x . stopstalk_handle + "<STR_LIT:'>" ) <EOL> complete_dict [ x . stopstalk_handle ] = [ ] <EOL> for custom_user in custom_users : <EOL> stopstalk_handle = custom_user . stopstalk_handle <EOL> if custom_user . duplicate_cu : <EOL> stopstalk_handle = cftable ( custom_user . duplicate_cu ) . stopstalk_handle <EOL> friends_stopstalk_handles . append ( "<STR_LIT:'>" + stopstalk_handle + "<STR_LIT:'>" ) <EOL> complete_dict [ stopstalk_handle ] = [ ] <EOL> if friends_stopstalk_handles == [ ] : <EOL> friends_stopstalk_handles = [ "<STR_LIT>" ] <EOL> sql_query = """<STR_LIT>""" % ( "<STR_LIT:U+002CU+0020>" . join ( friends_stopstalk_handles ) ) <EOL> user_rows = db . executesql ( sql_query ) <EOL> for user in user_rows : <EOL> if complete_dict [ user [ <NUM_LIT:0> ] ] != [ ] : <EOL> complete_dict [ user [ <NUM_LIT:0> ] ] . append ( ( user [ <NUM_LIT:1> ] , user [ <NUM_LIT:2> ] ) ) <EOL> else : <EOL> complete_dict [ user [ <NUM_LIT:0> ] ] = [ ( user [ <NUM_LIT:1> ] , user [ <NUM_LIT:2> ] ) ] <EOL> users = [ ] <EOL> for user in reg_users : <EOL> try : <EOL> solved = solved_count [ user . stopstalk_handle ] <EOL> except KeyError : <EOL> solved = <NUM_LIT:0> <EOL> tup = utilities . compute_row ( user , complete_dict , solved ) <EOL> if tup is not ( ) : <EOL> users . append ( tup ) <EOL> for user in custom_users : <EOL> try : <EOL> if user . duplicate_cu : <EOL> orig_user = cftable ( user . duplicate_cu ) <EOL> solved = solved_count [ orig_user . stopstalk_handle ] <EOL> else : <EOL> solved = solved_count [ user . stopstalk_handle ] <EOL> except KeyError : <EOL> solved = <NUM_LIT:0> <EOL> tup = utilities . compute_row ( user , complete_dict , solved , True ) <EOL> if tup is not ( ) : <EOL> users . append ( tup ) <EOL> users = sorted ( users , key = lambda x : x [ <NUM_LIT:3> ] , reverse = True ) <EOL> table = TABLE ( _class = "<STR_LIT>" ) <EOL> table . append ( THEAD ( TR ( TH ( "<STR_LIT>" ) , <EOL> TH ( "<STR_LIT:Name>" ) , <EOL> TH ( "<STR_LIT>" ) , <EOL> TH ( "<STR_LIT>" ) , <EOL> TH ( "<STR_LIT>" ) , <EOL> TH ( "<STR_LIT>" ) , <EOL> TH ( "<STR_LIT>" ) ) ) ) <EOL> tbody = TBODY ( ) <EOL> rank = <NUM_LIT:1> <EOL> for i in users : <EOL> if i [ <NUM_LIT:5> ] : <EOL> span = SPAN ( _class = "<STR_LIT>" , <EOL> data = { "<STR_LIT>" : "<STR_LIT:right>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" } , <EOL> _style = "<STR_LIT>" + "<STR_LIT>" + "<STR_LIT>" + "<STR_LIT>" + "<STR_LIT>" ) <EOL> else : <EOL> span = SPAN ( ) <EOL> tr = TR ( ) <EOL> tr . append ( TD ( str ( rank ) + "<STR_LIT:.>" ) ) <EOL> tr . append ( TD ( DIV ( span , DIV ( i [ <NUM_LIT:0> ] ) ) ) ) <EOL> tr . append ( TD ( A ( i [ <NUM_LIT:1> ] , <EOL> _href = URL ( "<STR_LIT:user>" , "<STR_LIT>" , args = [ i [ <NUM_LIT:1> ] ] ) , <EOL> _target = "<STR_LIT>" ) ) ) <EOL> tr . append ( TD ( A ( i [ <NUM_LIT:2> ] , <EOL> _href = URL ( "<STR_LIT:default>" , <EOL> "<STR_LIT>" , <EOL> vars = { "<STR_LIT:q>" : i [ <NUM_LIT:2> ] , <EOL> "<STR_LIT>" : global_leaderboard } ) ) ) ) <EOL> tr . append ( TD ( i [ <NUM_LIT:3> ] ) ) <EOL> if i [ <NUM_LIT:6> ] > <NUM_LIT:0> : <EOL> tr . append ( TD ( B ( "<STR_LIT>" % str ( i [ <NUM_LIT:6> ] ) ) , <EOL> _class = "<STR_LIT>" ) ) <EOL> elif i [ <NUM_LIT:6> ] < <NUM_LIT:0> : <EOL> tr . append ( TD ( B ( i [ <NUM_LIT:6> ] ) , _class = "<STR_LIT>" ) ) <EOL> else : <EOL> tr . append ( TD ( i [ <NUM_LIT:6> ] , _class = "<STR_LIT>" ) ) <EOL> diff = "<STR_LIT>" . format ( i [ <NUM_LIT:4> ] ) <EOL> if float ( diff ) == <NUM_LIT:0.0> : <EOL> tr . append ( TD ( "<STR_LIT:+>" + diff , "<STR_LIT:U+0020>" , <EOL> I ( _class = "<STR_LIT>" ) ) ) <EOL> elif i [ <NUM_LIT:4> ] > <NUM_LIT:0> : <EOL> tr . append ( TD ( "<STR_LIT:+>" + str ( diff ) , "<STR_LIT:U+0020>" , <EOL> I ( _class = "<STR_LIT>" , <EOL> _style = "<STR_LIT>" ) ) ) <EOL> elif i [ <NUM_LIT:4> ] < <NUM_LIT:0> : <EOL> tr . append ( TD ( diff , "<STR_LIT:U+0020>" , <EOL> I ( _class = "<STR_LIT>" , <EOL> _style = "<STR_LIT>" ) ) ) <EOL> tbody . append ( tr ) <EOL> rank += <NUM_LIT:1> <EOL> table . append ( tbody ) <EOL> switch = DIV ( LABEL ( H6 ( "<STR_LIT>" , <EOL> INPUT ( _type = "<STR_LIT>" , _id = "<STR_LIT>" ) , <EOL> SPAN ( _class = "<STR_LIT>" ) , <EOL> "<STR_LIT>" ) ) , <EOL> _class = "<STR_LIT>" ) <EOL> div = TAG [ "<STR_LIT>" ] ( switch , table ) <EOL> return dict ( div = div , <EOL> heading = heading , <EOL> global_leaderboard = global_leaderboard ) <EOL> def user ( ) : <EOL> """<STR_LIT>""" <EOL> return dict ( form = auth ( ) ) <EOL> @ auth . requires_login ( ) <EOL> def search ( ) : <EOL> """<STR_LIT>""" <EOL> return dict ( ) <EOL> def filters ( ) : <EOL> """<STR_LIT>""" <EOL> stable = db . submission <EOL> get_vars = request . get_vars <EOL> if len ( request . args ) == <NUM_LIT:0> : <EOL> page = <NUM_LIT:1> <EOL> else : <EOL> page = int ( request . args [ <NUM_LIT:0> ] ) <EOL> page -= <NUM_LIT:1> <EOL> all_languages = db ( stable ) . select ( stable . lang , <EOL> distinct = True ) <EOL> languages = [ ] <EOL> for i in all_languages : <EOL> languages . append ( i [ "<STR_LIT>" ] ) <EOL> table = None <EOL> global_submissions = False <EOL> if get_vars . has_key ( "<STR_LIT>" ) : <EOL> if get_vars [ "<STR_LIT>" ] == "<STR_LIT:True>" : <EOL> global_submissions = True <EOL> if get_vars == { } : <EOL> return dict ( languages = languages , <EOL> div = DIV ( ) , <EOL> global_submissions = global_submissions ) <EOL> compulsary_keys = [ "<STR_LIT>" , "<STR_LIT:name>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> if set ( compulsary_keys ) . issubset ( get_vars . keys ( ) ) is False : <EOL> session . flash = "<STR_LIT>" <EOL> redirect ( URL ( "<STR_LIT:default>" , "<STR_LIT>" ) ) <EOL> cftable = db . custom_friend <EOL> atable = db . auth_user <EOL> ftable = db . friends <EOL> duplicates = [ ] <EOL> switch = DIV ( LABEL ( H6 ( "<STR_LIT>" , <EOL> INPUT ( _type = "<STR_LIT>" , _id = "<STR_LIT>" ) , <EOL> SPAN ( _class = "<STR_LIT>" ) , <EOL> "<STR_LIT>" ) ) , <EOL> _class = "<STR_LIT>" ) <EOL> table = TABLE ( ) <EOL> div = TAG [ "<STR_LIT>" ] ( H4 ( "<STR_LIT>" ) , switch , table ) <EOL> if global_submissions is False and session . user_id is None : <EOL> session . flash = "<STR_LIT>" <EOL> new_vars = request . vars <EOL> new_vars [ "<STR_LIT>" ] = True <EOL> redirect ( URL ( "<STR_LIT:default>" , "<STR_LIT>" , <EOL> vars = new_vars , <EOL> args = request . args ) ) <EOL> query = True <EOL> username = get_vars [ "<STR_LIT:name>" ] <EOL> if username != "<STR_LIT>" : <EOL> tmplist = username . split ( ) <EOL> for token in tmplist : <EOL> query &= ( ( cftable . first_name . contains ( token ) ) | ( cftable . last_name . contains ( token ) ) ) <EOL> if global_submissions is False : <EOL> query = ( cftable . user_id == session . user_id ) & query <EOL> cust_friends = db ( query ) . select ( cftable . id , cftable . duplicate_cu ) <EOL> custom_friends = [ ] <EOL> for cus_id in cust_friends : <EOL> if cus_id . duplicate_cu : <EOL> duplicates . append ( ( cus_id . id , cus_id . duplicate_cu ) ) <EOL> custom_friends . append ( cus_id . duplicate_cu ) <EOL> else : <EOL> custom_friends . append ( cus_id . id ) <EOL> query = True <EOL> if username != "<STR_LIT>" : <EOL> tmplist = username . split ( ) <EOL> for token in tmplist : <EOL> query &= ( ( atable . first_name . contains ( token ) ) | ( atable . last_name . contains ( token ) ) ) <EOL> possible_users = db ( query ) . select ( atable . id ) <EOL> possible_users = [ x [ "<STR_LIT:id>" ] for x in possible_users ] <EOL> friends = possible_users <EOL> if global_submissions is False : <EOL> query = ( ftable . user_id == session . user_id ) & ( ftable . friend_id . belongs ( possible_users ) ) <EOL> friend_ids = db ( query ) . select ( ftable . friend_id ) <EOL> friends = [ x [ "<STR_LIT>" ] for x in friend_ids ] <EOL> if session . user_id in possible_users : <EOL> friends . append ( session . user_id ) <EOL> query = ( stable . user_id . belongs ( friends ) ) <EOL> query |= ( stable . custom_user_id . belongs ( custom_friends ) ) <EOL> start_date = get_vars [ "<STR_LIT>" ] <EOL> end_date = get_vars [ "<STR_LIT>" ] <EOL> if start_date == "<STR_LIT>" : <EOL> start_date = current . INITIAL_DATE <EOL> else : <EOL> start_date += "<STR_LIT>" <EOL> if end_date == "<STR_LIT>" : <EOL> end_date = str ( datetime . datetime . today ( ) ) <EOL> end_date = end_date [ : - <NUM_LIT:7> ] <EOL> else : <EOL> end_date += "<STR_LIT>" <EOL> start_time = time . strptime ( start_date , "<STR_LIT>" ) <EOL> end_time = time . strptime ( end_date , "<STR_LIT>" ) <EOL> if end_time > start_time : <EOL> query &= ( stable . time_stamp >= start_date ) & ( stable . time_stamp <= end_date ) <EOL> else : <EOL> session . flash = "<STR_LIT>" <EOL> redirect ( URL ( "<STR_LIT:default>" , "<STR_LIT>" ) ) <EOL> pname = get_vars [ "<STR_LIT>" ] <EOL> if pname != "<STR_LIT>" : <EOL> pname = pname . split ( ) <EOL> for token in pname : <EOL> query &= ( stable . problem_name . contains ( token ) ) <EOL> def get_values_list ( param_name ) : <EOL> values_list = None <EOL> if get_vars . has_key ( param_name ) : <EOL> values_list = get_vars [ param_name ] <EOL> if isinstance ( values_list , str ) : <EOL> values_list = [ values_list ] <EOL> elif get_vars . has_key ( param_name + "<STR_LIT>" ) : <EOL> values_list = get_vars [ param_name + "<STR_LIT>" ] <EOL> if isinstance ( values_list , str ) : <EOL> values_list = [ values_list ] <EOL> return values_list <EOL> sites = get_values_list ( "<STR_LIT>" ) <EOL> if sites : <EOL> query &= ( stable . site . belongs ( sites ) ) <EOL> langs = get_values_list ( "<STR_LIT>" ) <EOL> if langs : <EOL> query &= ( stable . lang . belongs ( langs ) ) <EOL> statuses = get_values_list ( "<STR_LIT:status>" ) <EOL> if statuses : <EOL> query &= ( stable . status . belongs ( statuses ) ) <EOL> PER_PAGE = current . PER_PAGE <EOL> filtered = db ( query ) . select ( limitby = ( page * PER_PAGE , <EOL> ( page + <NUM_LIT:1> ) * PER_PAGE ) , <EOL> orderby = ~ stable . time_stamp ) <EOL> total_problems = db ( query ) . count ( ) <EOL> total_pages = total_problems / <NUM_LIT:100> <EOL> if total_problems % <NUM_LIT:100> == <NUM_LIT:0> : <EOL> total_pages += <NUM_LIT:1> <EOL> table = utilities . render_table ( filtered , duplicates ) <EOL> switch = DIV ( LABEL ( H6 ( "<STR_LIT>" , <EOL> INPUT ( _type = "<STR_LIT>" , _id = "<STR_LIT>" ) , <EOL> SPAN ( _class = "<STR_LIT>" ) , <EOL> "<STR_LIT>" ) ) , <EOL> _class = "<STR_LIT>" ) <EOL> div = TAG [ "<STR_LIT>" ] ( switch , table ) <EOL> return dict ( languages = languages , <EOL> div = div , <EOL> total_pages = total_pages , <EOL> global_submissions = global_submissions ) <EOL> @ auth . requires_login ( ) <EOL> def mark_friend ( ) : <EOL> """<STR_LIT>""" <EOL> if len ( request . args ) < <NUM_LIT:1> : <EOL> return "<STR_LIT>" <EOL> frtable = db . friend_requests <EOL> ftable = db . friends <EOL> friend_id = long ( request . args [ <NUM_LIT:0> ] ) <EOL> if friend_id != session . user_id : <EOL> query = ( ftable . user_id == session . user_id ) & ( ftable . friend_id == friend_id ) <EOL> value = db ( query ) . count ( ) <EOL> if value == <NUM_LIT:0> : <EOL> query = ( ( frtable . from_h == session . user_id ) & ( frtable . to_h == friend_id ) ) | ( ( frtable . from_h == friend_id ) & ( frtable . to_h == session . user_id ) ) <EOL> value = db ( query ) . count ( ) <EOL> if value != <NUM_LIT:0> : <EOL> session . flash = "<STR_LIT>" <EOL> redirect ( URL ( "<STR_LIT:default>" , "<STR_LIT>" ) ) <EOL> else : <EOL> return "<STR_LIT>" <EOL> else : <EOL> return "<STR_LIT>" <EOL> db . friend_requests . insert ( from_h = session . user_id , to_h = friend_id ) <EOL> atable = db . auth_user <EOL> query = ( atable . id == friend_id ) <EOL> row = db ( query ) . select ( atable . email , <EOL> atable . stopstalk_handle ) . first ( ) <EOL> current . send_mail ( to = row . email , <EOL> subject = session . handle + "<STR_LIT>" , <EOL> message = """<STR_LIT>""" % ( session . handle , <EOL> URL ( "<STR_LIT:user>" , <EOL> "<STR_LIT>" , <EOL> args = [ session . handle ] , <EOL> scheme = True , <EOL> host = True , <EOL> extension = False ) , <EOL> URL ( "<STR_LIT:user>" , <EOL> "<STR_LIT>" , <EOL> scheme = True , <EOL> host = True , <EOL> extension = False ) , <EOL> URL ( "<STR_LIT:default>" , "<STR_LIT>" , <EOL> scheme = True , <EOL> host = True , <EOL> extension = False ) ) ) <EOL> return "<STR_LIT>" <EOL> @ auth . requires_login ( ) <EOL> def retrieve_users ( ) : <EOL> """<STR_LIT>""" <EOL> atable = db . auth_user <EOL> frtable = db . friend_requests <EOL> ftable = db . friends <EOL> q = request . get_vars . get ( "<STR_LIT:q>" , None ) <EOL> query = ( atable . first_name . contains ( q ) ) | ( atable . last_name . contains ( q ) ) | ( atable . stopstalk_handle . contains ( q ) ) <EOL> for site in current . SITES : <EOL> field_name = site . lower ( ) + "<STR_LIT>" <EOL> query |= ( atable [ field_name ] . contains ( q ) ) <EOL> query &= ( atable . id != session . user_id ) <EOL> tmprows = db ( frtable . to_h == session . user_id ) . select ( frtable . from_h ) <EOL> for row in tmprows : <EOL> query &= ( atable . id != row . from_h ) <EOL> columns = [ atable . id , <EOL> atable . first_name , <EOL> atable . last_name , <EOL> atable . stopstalk_handle ] <EOL> for site in current . SITES : <EOL> columns . append ( atable [ site . lower ( ) + "<STR_LIT>" ] ) <EOL> rows = db ( query ) . select ( * columns ) <EOL> t = TABLE ( _class = "<STR_LIT>" ) <EOL> tr = TR ( TH ( "<STR_LIT:Name>" ) , <EOL> TH ( "<STR_LIT>" ) ) <EOL> for site in current . SITES : <EOL> tr . append ( TH ( site + "<STR_LIT>" ) ) <EOL> tr . append ( TH ( "<STR_LIT>" ) ) <EOL> thead = THEAD ( ) <EOL> thead . append ( tr ) <EOL> t . append ( thead ) <EOL> tbody = TBODY ( ) <EOL> query = ( ftable . user_id == session [ "<STR_LIT>" ] ) <EOL> friends = db ( query ) . select ( atable . id , <EOL> join = ftable . on ( ftable . friend_id == atable . id ) ) <EOL> friends = [ x [ "<STR_LIT:id>" ] for x in friends ] <EOL> for user in rows : <EOL> tr = TR ( ) <EOL> tr . append ( TD ( A ( user . first_name + "<STR_LIT:U+0020>" + user . last_name , <EOL> _href = URL ( "<STR_LIT:user>" , "<STR_LIT>" , <EOL> args = [ user . stopstalk_handle ] , <EOL> extension = False ) , <EOL> _target = "<STR_LIT>" ) ) ) <EOL> tr . append ( TD ( user . stopstalk_handle ) ) <EOL> for site in current . SITES : <EOL> tr . append ( TD ( user [ site . lower ( ) + "<STR_LIT>" ] ) ) <EOL> if user . id not in friends : <EOL> r = db ( ( frtable . from_h == session . user_id ) & <EOL> ( frtable . to_h == user . id ) ) . count ( ) <EOL> if r == <NUM_LIT:0> : <EOL> tr . append ( TD ( BUTTON ( I ( _class = "<STR_LIT>" ) , <EOL> _class = "<STR_LIT>" , <EOL> data = { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : str ( user . id ) , <EOL> "<STR_LIT:type>" : "<STR_LIT>" } ) ) ) <EOL> else : <EOL> tr . append ( TD ( BUTTON ( I ( _class = "<STR_LIT>" ) , <EOL> _class = "<STR_LIT>" , <EOL> data = { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:type>" : "<STR_LIT>" } ) ) ) <EOL> else : <EOL> tr . append ( TD ( BUTTON ( I ( _class = "<STR_LIT>" ) , <EOL> _class = "<STR_LIT>" , <EOL> data = { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : str ( user . id ) , <EOL> "<STR_LIT:type>" : "<STR_LIT>" } ) ) ) <EOL> tbody . append ( tr ) <EOL> t . append ( tbody ) <EOL> return dict ( t = t ) <EOL> @ auth . requires_login ( ) <EOL> def unfriend ( ) : <EOL> """<STR_LIT>""" <EOL> if len ( request . args ) != <NUM_LIT:1> : <EOL> return "<STR_LIT>" <EOL> else : <EOL> friend_id = long ( request . args [ <NUM_LIT:0> ] ) <EOL> user_id = session [ "<STR_LIT>" ] <EOL> ftable = db . friends <EOL> query = ( ftable . user_id == user_id ) & ( ftable . friend_id == friend_id ) <EOL> value = db ( query ) . count ( ) <EOL> if value == <NUM_LIT:0> : <EOL> return "<STR_LIT>" <EOL> db ( query ) . delete ( ) <EOL> query = ( ftable . user_id == friend_id ) & ( ftable . friend_id == user_id ) <EOL> db ( query ) . delete ( ) <EOL> atable = db . auth_user <EOL> row = db ( atable . id == friend_id ) . select ( atable . email ) . first ( ) <EOL> current . send_mail ( to = row . email , <EOL> subject = "<STR_LIT>" , <EOL> message = """<STR_LIT>""" % ( session . handle , <EOL> URL ( "<STR_LIT:user>" , <EOL> "<STR_LIT>" , <EOL> args = [ session . handle ] , <EOL> scheme = True , <EOL> host = True , <EOL> extension = False ) , <EOL> URL ( "<STR_LIT:default>" , "<STR_LIT>" , <EOL> scheme = True , <EOL> host = True , <EOL> extension = False ) ) ) <EOL> return "<STR_LIT>" <EOL> @ auth . requires_login ( ) <EOL> def submissions ( ) : <EOL> """<STR_LIT>""" <EOL> if len ( request . args ) == <NUM_LIT:0> : <EOL> active = "<STR_LIT:1>" <EOL> else : <EOL> active = request . args [ <NUM_LIT:0> ] <EOL> cftable = db . custom_friend <EOL> ftable = db . friends <EOL> stable = db . submission <EOL> atable = db . auth_user <EOL> friends , cusfriends = utilities . get_friends ( session [ "<STR_LIT>" ] ) <EOL> custom_friends = [ ] <EOL> for cus_id in cusfriends : <EOL> if cus_id [ <NUM_LIT:1> ] == None : <EOL> custom_friends . append ( cus_id [ <NUM_LIT:0> ] ) <EOL> else : <EOL> custom_friends . append ( cus_id [ <NUM_LIT:1> ] ) <EOL> query = ( stable . user_id . belongs ( friends ) ) | ( stable . custom_user_id . belongs ( custom_friends ) ) <EOL> total_count = db ( query ) . count ( ) <EOL> PER_PAGE = current . PER_PAGE <EOL> count = total_count / PER_PAGE <EOL> if total_count % PER_PAGE : <EOL> count += <NUM_LIT:1> <EOL> if request . extension == "<STR_LIT>" : <EOL> return dict ( count = count , <EOL> total_rows = <NUM_LIT:1> ) <EOL> offset = PER_PAGE * ( int ( active ) - <NUM_LIT:1> ) <EOL> rows = db ( query ) . select ( orderby = ~ db . submission . time_stamp , <EOL> limitby = ( offset , offset + PER_PAGE ) ) <EOL> table = utilities . render_table ( rows , cusfriends ) <EOL> return dict ( table = table , <EOL> friends = friends , <EOL> cusfriends = cusfriends , <EOL> total_rows = len ( rows ) ) <EOL> def faq ( ) : <EOL> """<STR_LIT>""" <EOL> div = DIV ( _class = "<STR_LIT>" ) <EOL> ul = UL ( _class = "<STR_LIT>" , data = { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> faqs = db ( db . faq ) . select ( ) <EOL> for i in xrange ( len ( faqs ) ) : <EOL> li = LI ( DIV ( B ( str ( i + <NUM_LIT:1> ) + "<STR_LIT>" + faqs [ i ] . question ) , <EOL> _class = "<STR_LIT>" ) , <EOL> DIV ( MARKMIN ( faqs [ i ] . answer ) , <EOL> _class = "<STR_LIT>" ) ) <EOL> ul . append ( li ) <EOL> div . append ( ul ) <EOL> return dict ( div = div ) <EOL> def contact_us ( ) : <EOL> """<STR_LIT>""" <EOL> ctable = db . contact_us <EOL> if session [ "<STR_LIT>" ] and request . post_vars : <EOL> if request . post_vars . stickers is not None : <EOL> response . flash = "<STR_LIT>" <EOL> user = session . auth . user <EOL> stickers = eval ( request . post_vars [ "<STR_LIT>" ] ) <EOL> if stickers [ <NUM_LIT:0> ] == <NUM_LIT:0> : <EOL> session . flash = "<STR_LIT>" <EOL> redirect ( URL ( "<STR_LIT:default>" , "<STR_LIT>" ) ) <EOL> ctable . email . default = user . email <EOL> ctable . name . default = user . first_name + "<STR_LIT:U+0020>" + user . last_name <EOL> ctable . subject . default = "<STR_LIT>" <EOL> ctable . text_message . default = "<STR_LIT>" <EOL> form = SQLFORM ( ctable ) <EOL> if session [ "<STR_LIT>" ] : <EOL> user = session [ "<STR_LIT>" ] [ "<STR_LIT:user>" ] <EOL> ctable . email . default = user [ "<STR_LIT:email>" ] <EOL> if form . process ( ) . accepted : <EOL> session . flash = "<STR_LIT>" <EOL> current . send_mail ( to = "<STR_LIT>" , <EOL> subject = form . vars . subject , <EOL> message = "<STR_LIT>" % ( form . vars . name , <EOL> form . vars . email , <EOL> form . vars . phone_number ) + "<STR_LIT>" % form . vars . subject + "<STR_LIT>" % form . vars . text_message ) <EOL> redirect ( URL ( "<STR_LIT:default>" , "<STR_LIT:index>" ) ) <EOL> elif form . errors : <EOL> response . flash = "<STR_LIT>" <EOL> return dict ( form = form ) <EOL> def call ( ) : <EOL> """<STR_LIT>""" <EOL> return service ( ) </s>
