<s> from datetime import datetime , timedelta <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . db . models import Count , Sum <EOL> from django . shortcuts import get_object_or_404 , redirect <EOL> from accounts . models import UserProfile <EOL> from race . models import Map , MapType , Run , BestRun , Server <EOL> from annoying . decorators import render_to <EOL> from annoying . functions import get_object_or_None <EOL> from brabeion import badges <EOL> from brabeion . models import BadgeAward <EOL> @ render_to ( '<STR_LIT>' ) <EOL> def ranks ( request ) : <EOL> users = UserProfile . objects . filter ( points__gt = <NUM_LIT:0> ) . exclude ( user__is_active = False ) . extra ( <EOL> select = { '<STR_LIT>' : <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> } , <EOL> order_by = [ '<STR_LIT>' ] <EOL> ) <EOL> total_runtime = Run . objects . aggregate ( Sum ( '<STR_LIT:time>' ) ) [ '<STR_LIT>' ] <EOL> total_runs = Run . objects . count ( ) <EOL> return { <EOL> '<STR_LIT>' : total_runtime , <EOL> '<STR_LIT>' : total_runs , <EOL> '<STR_LIT>' : users , <EOL> } <EOL> @ render_to ( '<STR_LIT>' ) <EOL> def ranks_map_list ( request ) : <EOL> maps = Map . objects . order_by ( '<STR_LIT:name>' ) . select_related ( ) <EOL> return { <EOL> '<STR_LIT>' : maps , <EOL> } <EOL> @ render_to ( '<STR_LIT>' ) <EOL> def ranks_map_detail ( request , map_id ) : <EOL> map_obj = get_object_or_404 ( Map . objects . select_related ( ) , pk = map_id ) <EOL> best_runs = BestRun . objects . filter ( map = map_obj ) . exclude ( user__is_active = False ) . extra ( <EOL> select = { '<STR_LIT>' : <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> } , <EOL> ) <EOL> return { <EOL> '<STR_LIT>' : map_obj , <EOL> '<STR_LIT>' : best_runs , <EOL> } <EOL> @ render_to ( '<STR_LIT>' ) <EOL> def map_list ( request , map_type = None ) : <EOL> maps = Map . objects . all ( ) . select_related ( ) <EOL> filtered_type = None <EOL> if map_type : <EOL> filtered_type = get_object_or_404 ( MapType , slug = map_type ) <EOL> maps = maps . filter ( map_types__slug = map_type ) <EOL> maps = maps . order_by ( '<STR_LIT:name>' ) <EOL> map_types = MapType . objects . all ( ) <EOL> return { <EOL> '<STR_LIT>' : map_types , <EOL> '<STR_LIT>' : filtered_type , <EOL> '<STR_LIT>' : maps , <EOL> } <EOL> @ login_required <EOL> @ render_to ( '<STR_LIT>' ) <EOL> def map_list_unfinished ( request ) : <EOL> map_ids = BestRun . objects . filter ( user = request . user ) . values_list ( '<STR_LIT>' , flat = True ) <EOL> maps = Map . objects . exclude ( id__in = map_ids ) . select_related ( ) <EOL> return { <EOL> '<STR_LIT>' : MapType . objects . all ( ) , <EOL> '<STR_LIT>' : maps , <EOL> } <EOL> @ render_to ( '<STR_LIT>' ) <EOL> def map_detail ( request , map_id ) : <EOL> map_obj = get_object_or_404 ( Map . objects . select_related ( ) , pk = map_id ) <EOL> best_runs = BestRun . objects . filter ( map = map_obj ) . extra ( <EOL> select = { '<STR_LIT>' : <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> } , <EOL> ) [ : <NUM_LIT:5> ] <EOL> latest_runs = Run . objects . filter ( map = map_obj ) . order_by ( '<STR_LIT>' ) [ : <NUM_LIT:5> ] <EOL> if request . user . is_authenticated ( ) : <EOL> user_latest_runs = Run . objects . filter ( user = request . user , map = map_obj ) . order_by ( '<STR_LIT>' ) [ : <NUM_LIT:5> ] <EOL> user_bestrun = get_object_or_None ( BestRun , user = request . user , <EOL> map = map_obj ) <EOL> else : <EOL> user_latest_runs = None <EOL> user_bestrun = None <EOL> return { <EOL> '<STR_LIT>' : map_obj , <EOL> '<STR_LIT>' : best_runs , <EOL> '<STR_LIT>' : latest_runs , <EOL> '<STR_LIT>' : user_latest_runs , <EOL> '<STR_LIT>' : user_bestrun , <EOL> } <EOL> @ render_to ( '<STR_LIT>' ) <EOL> def awards ( request ) : <EOL> if request . user . is_authenticated ( ) : <EOL> user_badges = set ( ( slug , level ) for slug , level in <EOL> BadgeAward . objects . filter ( <EOL> user = request . user <EOL> ) . values_list ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> else : <EOL> user_badges = [ ] <EOL> badges_awarded = BadgeAward . objects . values ( "<STR_LIT>" , "<STR_LIT>" ) . annotate ( <EOL> num = Count ( "<STR_LIT>" ) <EOL> ) <EOL> badges_counts = { "<STR_LIT>" . format ( k [ '<STR_LIT>' ] , k [ '<STR_LIT>' ] ) : k [ '<STR_LIT>' ] for k in badges_awarded } <EOL> user_count = UserProfile . objects . count ( ) <EOL> badges_list = list ( ) <EOL> for badge_cls in badges . _registry . values ( ) : <EOL> for level , badge in enumerate ( badge_cls . levels ) : <EOL> badge_count = badges_counts . get ( "<STR_LIT>" . format ( badge_cls . slug , level ) , <NUM_LIT:0> ) <EOL> badges_list . append ( { <EOL> "<STR_LIT>" : badge_cls . slug , <EOL> "<STR_LIT>" : level , <EOL> "<STR_LIT:name>" : badge . name , <EOL> "<STR_LIT:description>" : badge . description , <EOL> "<STR_LIT:count>" : badge_count , <EOL> "<STR_LIT>" : badge_count / float ( user_count ) * <NUM_LIT:100> if user_count else <NUM_LIT:0> , <EOL> "<STR_LIT>" : ( badge_cls . slug , level ) in user_badges , <EOL> } ) <EOL> return { <EOL> '<STR_LIT>' : badges_list , <EOL> '<STR_LIT>' : user_count , <EOL> } <EOL> def map_download ( request , map_id ) : <EOL> map_obj = get_object_or_404 ( Map , pk = map_id ) <EOL> map_obj . download_count += <NUM_LIT:1> <EOL> map_obj . save ( ) <EOL> return redirect ( map_obj . get_download_url ( ) ) <EOL> @ render_to ( '<STR_LIT>' ) <EOL> def servers ( request ) : <EOL> servers_online = Server . objects . filter ( <EOL> last_connection_at__gte = ( datetime . now ( ) - timedelta ( minutes = <NUM_LIT:10> ) ) <EOL> ) <EOL> players_online_count = UserProfile . objects . filter ( <EOL> last_connection_at__gte = ( datetime . now ( ) - timedelta ( minutes = <NUM_LIT:10> ) ) <EOL> ) . filter ( last_played_server__isnull = False ) . count ( ) <EOL> return { <EOL> '<STR_LIT>' : servers_online , <EOL> '<STR_LIT>' : players_online_count , <EOL> } </s>
