<s> from __future__ import division <EOL> import datetime <EOL> import json <EOL> from django . http import HttpResponseRedirect , Http404 , HttpResponse <EOL> from django . shortcuts import render , get_object_or_404 <EOL> from django . utils . html import escape <EOL> from django . core . urlresolvers import reverse <EOL> from django . views . decorators . cache import cache_page <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . db . models import Q , Count , Max <EOL> from django . views . decorators . gzip import gzip_page <EOL> from ladder . models import Ladder , Player , Result , Season , League <EOL> from ladder . forms import AddResultForm <EOL> @ gzip_page <EOL> @ cache_page ( <NUM_LIT> * <NUM_LIT> * <NUM_LIT> * <NUM_LIT:2> , key_prefix = '<STR_LIT:index>' ) <EOL> def index ( request ) : <EOL> current_season = Season . objects . latest ( '<STR_LIT>' ) <EOL> os_year = Season . objects . order_by ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . start_date . year <EOL> cs_year = current_season . start_date . year <EOL> at_ladders = Season . objects . count ( ) <EOL> at_divisions = Ladder . objects . count ( ) <EOL> at_results = int ( Result . objects . count ( ) / <NUM_LIT:2> ) <EOL> at_players = Player . objects . count ( ) <EOL> context = { <EOL> '<STR_LIT>' : current_season , <EOL> '<STR_LIT>' : ( cs_year - os_year ) , <EOL> '<STR_LIT>' : '<STR_LIT>' + os_year . __str__ ( ) + '<STR_LIT>' + cs_year . __str__ ( ) + '<STR_LIT:)>' , <EOL> '<STR_LIT>' : at_divisions , <EOL> '<STR_LIT>' : at_ladders , <EOL> '<STR_LIT>' : at_results , <EOL> '<STR_LIT>' : at_players , <EOL> } <EOL> return render ( request , '<STR_LIT>' , context ) <EOL> @ gzip_page <EOL> @ cache_page ( <NUM_LIT> * <NUM_LIT> * <NUM_LIT> , key_prefix = '<STR_LIT>' ) <EOL> def list_rounds ( request ) : <EOL> seasons = Season . objects . order_by ( '<STR_LIT>' ) <EOL> context = { <EOL> '<STR_LIT>' : seasons , <EOL> } <EOL> return render ( request , '<STR_LIT>' , context ) <EOL> @ gzip_page <EOL> @ cache_page ( <NUM_LIT> * <NUM_LIT> , key_prefix = '<STR_LIT>' ) <EOL> def season ( request , year , season_round ) : <EOL> season_object = get_object_or_404 ( Season , start_date__year = year , season_round = season_round ) <EOL> ladders = Ladder . objects . filter ( season = season_object ) <EOL> results = Result . objects . filter ( ladder__season = season_object ) <EOL> league = League . objects . filter ( ladder__season = season_object ) <EOL> results_dict = { } <EOL> for result in results : <EOL> results_dict . setdefault ( result . player . id , [ ] ) . append ( result ) <EOL> return render ( <EOL> request , '<STR_LIT>' , <EOL> dict ( season = season_object , ladders = ladders , results_dict = results_dict , league = league ) <EOL> ) <EOL> @ gzip_page <EOL> def ladder ( request , year , season_round , division_id ) : <EOL> ladder_object = get_object_or_404 ( Ladder , division = division_id , season__start_date__year = year , <EOL> season__season_round = season_round ) <EOL> results = Result . objects . filter ( ladder = ladder_object ) <EOL> results_dict = { } <EOL> for result in results : <EOL> results_dict . setdefault ( result . player . id , [ ] ) . append ( result ) <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT>' : ladder_object , '<STR_LIT>' : results_dict } ) <EOL> @ login_required <EOL> def add ( request , year , season_round , division_id ) : <EOL> ladder_object = get_object_or_404 ( Ladder , division = division_id , season__start_date__year = year , <EOL> season__season_round = season_round ) <EOL> result = Result ( ladder = ladder_object , date_added = datetime . datetime . now ( ) ) <EOL> try : <EOL> next_ladder = Ladder . objects . get ( season = ladder_object . season , division = ladder_object . division + <NUM_LIT:1> ) <EOL> except Ladder . DoesNotExist : <EOL> next_ladder = False <EOL> try : <EOL> previous_ladder = Ladder . objects . get ( season = ladder_object . season , division = ladder_object . division - <NUM_LIT:1> ) <EOL> except Ladder . DoesNotExist : <EOL> previous_ladder = False <EOL> if request . POST : <EOL> form = AddResultForm ( ladder_object , request . POST , instance = result ) <EOL> if form . is_valid ( ) : <EOL> losing_result = form . save ( commit = False ) <EOL> try : <EOL> existing_player_result = Result . objects . get ( ladder = ladder_object , player = losing_result . player , <EOL> opponent = losing_result . opponent ) <EOL> existing_player_result . delete ( ) <EOL> existing_opponent_result = Result . objects . get ( ladder = ladder_object , player = losing_result . opponent , <EOL> opponent = losing_result . player ) <EOL> existing_opponent_result . delete ( ) <EOL> except Result . DoesNotExist : <EOL> pass <EOL> winning_result = Result ( ladder = losing_result . ladder , player = losing_result . opponent , <EOL> opponent = losing_result . player , result = <NUM_LIT:9> , date_added = losing_result . date_added , <EOL> inaccurate_flag = losing_result . inaccurate_flag ) <EOL> losing_result . save ( ) <EOL> winning_result . save ( ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' , args = ( <EOL> ladder_object . season . start_date . year , ladder_object . season . season_round , ladder_object . division ) ) ) <EOL> else : <EOL> form = AddResultForm ( ladder_object , instance = result ) <EOL> results = Result . objects . filter ( ladder = ladder_object ) <EOL> results_dict = { } <EOL> for result in results : <EOL> results_dict . setdefault ( result . player . id , [ ] ) . append ( result ) <EOL> return render ( request , '<STR_LIT>' , <EOL> { '<STR_LIT>' : ladder_object , '<STR_LIT>' : results_dict , '<STR_LIT>' : form , <EOL> '<STR_LIT>' : next_ladder , '<STR_LIT>' : previous_ladder } ) <EOL> @ gzip_page <EOL> def player_history ( request , player_id ) : <EOL> player = get_object_or_404 ( Player , pk = player_id ) <EOL> league_set = player . league_set . order_by ( '<STR_LIT>' ) <EOL> try : <EOL> head = Result . objects . values ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) . filter ( <EOL> player = player ) . annotate ( times_played = Count ( '<STR_LIT>' ) , last_played = Max ( '<STR_LIT>' ) ) . order_by ( <EOL> '<STR_LIT>' ) [ : <NUM_LIT:10> ] <EOL> except Result . DoesNotExist : <EOL> raise Http404 <EOL> return render ( request , '<STR_LIT>' , <EOL> { '<STR_LIT>' : player , '<STR_LIT>' : league_set , '<STR_LIT>' : league_set , '<STR_LIT>' : head } ) <EOL> @ gzip_page <EOL> def head_to_head ( request , player_id , opponent_id ) : <EOL> player = get_object_or_404 ( Player , pk = player_id ) <EOL> opponent = get_object_or_404 ( Player , pk = opponent_id ) <EOL> results1 = Result . objects . filter ( player = player , opponent = opponent , result__lt = <NUM_LIT:9> ) . order_by ( <EOL> '<STR_LIT>' ) <EOL> results2 = Result . objects . filter ( player = opponent , opponent = player , result__lt = <NUM_LIT:9> ) . order_by ( <EOL> '<STR_LIT>' ) <EOL> results = results1 | results2 <EOL> stats = { '<STR_LIT>' : results2 . count ( ) , '<STR_LIT>' : results1 . count ( ) , '<STR_LIT>' : results1 . count ( ) + results2 . count ( ) } <EOL> return render ( request , '<STR_LIT>' , <EOL> { '<STR_LIT>' : stats , '<STR_LIT>' : results , '<STR_LIT>' : player , '<STR_LIT>' : opponent } ) <EOL> @ gzip_page <EOL> def player_result ( request ) : <EOL> query = request . GET . get ( '<STR_LIT>' , False ) <EOL> if query is False : <EOL> raise Http404 <EOL> qs = Player . objects . all ( ) <EOL> for term in query . split ( ) : <EOL> qs = qs . filter ( Q ( first_name__icontains = term ) | Q ( last_name__icontains = term ) ) <EOL> results = [ x for x in qs ] <EOL> if len ( results ) == <NUM_LIT:1> : <EOL> player = results [ <NUM_LIT:0> ] <EOL> return player_history ( request , player . id ) <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT>' : results , '<STR_LIT>' : escape ( query ) } ) <EOL> def player_search ( request ) : <EOL> result_set = { } <EOL> query = request . GET . get ( '<STR_LIT>' , False ) <EOL> if query is False : <EOL> raise Http404 <EOL> qs = Player . objects . all ( ) <EOL> for term in query . split ( ) : <EOL> qs = qs . filter ( Q ( first_name__icontains = term ) | Q ( last_name__icontains = term ) ) <EOL> results = [ escape ( x . first_name . strip ( ) + '<STR_LIT:U+0020>' + x . last_name . strip ( ) ) for x in qs ] <EOL> result_set [ "<STR_LIT>" ] = results <EOL> return HttpResponse ( json . dumps ( result_set ) , content_type = "<STR_LIT:application/json>" ) <EOL> def h2h_search ( request , player_id ) : <EOL> result_set = { } <EOL> query = request . GET . get ( '<STR_LIT>' , False ) <EOL> if query is False : <EOL> raise Http404 <EOL> head = Result . objects . values ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> for term in query . split ( ) : <EOL> head = head . filter ( <EOL> Q ( opponent__first_name__icontains = term ) | Q ( opponent__last_name__icontains = term ) , <EOL> Q ( player__id = player_id ) ) . annotate ( times_played = Count ( '<STR_LIT>' ) ) . order_by ( '<STR_LIT>' ) <EOL> results = { } <EOL> for x in head : <EOL> results [ x [ '<STR_LIT>' ] ] = escape ( <EOL> unicode ( x [ '<STR_LIT>' ] ) . strip ( ) + '<STR_LIT>' + x [ '<STR_LIT>' ] . strip ( ) + '<STR_LIT:U+0020>' + x [ <EOL> '<STR_LIT>' ] . strip ( ) ) <EOL> result_set [ "<STR_LIT>" ] = results <EOL> return HttpResponse ( json . dumps ( result_set ) , content_type = "<STR_LIT:application/json>" ) <EOL> def season_ajax_stats ( request ) : <EOL> season_id = request . GET . get ( '<STR_LIT:id>' , False ) <EOL> if season_id is False : <EOL> raise Http404 <EOL> try : <EOL> season_object = Season . objects . get ( pk = season_id ) <EOL> except Season . DoesNotExist : <EOL> raise Http404 <EOL> except ValueError : <EOL> raise Http404 <EOL> stats = season_object . get_stats ( ) <EOL> include_leader = request . GET . get ( '<STR_LIT>' , False ) <EOL> if include_leader : <EOL> stats . update ( season_object . get_leader_stats ( ) ) <EOL> return HttpResponse ( json . dumps ( stats ) , content_type = "<STR_LIT:application/json>" ) <EOL> def season_ajax_progress ( request ) : <EOL> season_id = request . GET . get ( '<STR_LIT:id>' , False ) <EOL> if season_id is False : <EOL> raise Http404 <EOL> try : <EOL> season_object = Season . objects . get ( pk = season_id ) <EOL> except Season . DoesNotExist : <EOL> raise Http404 <EOL> except ValueError : <EOL> raise Http404 <EOL> return HttpResponse ( json . dumps ( season_object . get_progress ( ) ) , content_type = "<STR_LIT:application/json>" ) </s>
