<commit_before># -*- coding: utf-8 -*-

# Copyright (C) 2011,2012 <NAME>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with this program.  If not, see
# <http://www.gnu.org/licenses/>.

"""
Views for showing different statistics of the team.
"""

from __future__ import division

from django.shortcuts import render_to_response, render
from django.template import RequestContext, Context, loader
from django.http import HttpResponse
from django.db.models import Sum, Q
from django.core.urlresolvers import reverse
from itertools import chain
from django.conf import settings

from teamstats.models import *

def index(request,
          league_class=League,
          template_name='teamstats/index.html'):
    '''
    View for the main page.
    '''
    context = {
        'league_list': league_class.objects.all(),
        'team_name':   settings.TEAM_NAME,
        }
    return render_to_response(template_name,
                              context)

def get_match_enrollments(match, 
                          seasonplayer_class=SeasonPlayer,
                          enrolledplayer_class=EnrolledPlayer):
    """
    Returns the enrollment statuses of the players.
    """
    # Show in/out stats
    seasonplayers = seasonplayer_class.objects.filter(season=match.season,
                                                      passive=False).order_by('player')
    # TODO: Use related_names and then just match.enrolledplayers
    enrollments = enrolledplayer_class.objects.filter(match=match).order_by('player__player')

    ind = 0
    for player in seasonplayers:
        if ind < len(enrollments) and player.id == enrollments[ind].player.id:
            if enrollments[ind].enroll:
                player.choice = 1
            else:
                player.choice = 2
            ind = ind + 1
        else:
            player.choice = 3

    return seasonplayers
    
def get_match_goals(match):
    if match.opponent_goals is not None:
        matchplayers = MatchPlayer.objects.filter(match=match)
        if not matchplayers:
            goals = match.opponent_owngoals
        else:
            goals = matchplayers.aggregate(Sum('goals')).values()[0] + match.opponent_owngoals
    else:
        goals = None

    return goals

def show_season(request, 
                season_id,
                season_class=Season,
                league_class=League,
                seasonplayer_class=SeasonPlayer,
                enrolledplayer_class=EnrolledPlayer,
                match_class=Match,
                template_name='teamstats/show_season.html'):
    '''
    View for showing information about a season: the players with
    statistics and all the matches.
    '''

    try:
        season = season_class.objects.get(id__exact=season_id)

        # Player stats
        player_list = seasonplayer_class.objects.filter(season=season)
        for player in player_list:
            matches = MatchPlayer.objects.filter(player=player)

            player.games = matches.count()
            player.goals = matches.aggregate(goals=Sum('goals')).values()[0]
            player.assists = matches.aggregate(assists=Sum('assists')).values()[0]
            
            if player.goals is None:
                player.goals = 0
            if player.assists is None:
                player.assists = 0

            player.points = player.goals + player.assists
            if player.games:
                player.ppg = player.points / player.games
            else:
                player.ppg = 0.0

                #season_list = season_class.objects.all()

        # TODO: Show opponent own goals!

        # Match list
        match_list = match_class.objects.filter(season=season)
        for match in match_list:
            match.goals = get_match_goals(match)
            match.result = (match.goals != None)
            if not match.result:
                match.player_list = get_match_enrollments(match,
                                                          seasonplayer_class=seasonplayer_class, 
                                                          enrolledplayer_class=enrolledplayer_class)
                match.player_list = sorted(match.player_list, 
                                           key=lambda player: player.player.shortname())

                ## # Get enrollments: 1) IN, 2) OUT, 3) Unknown
                ## enrolledplayers = enrolledplayer_class.objects.filter(match=match)
                ## match.in_players = enrolledplayers.filter(enroll=True)
                ## match.in_players = sorted(match.in_players, 
                ##                           key=lambda player: player.player.player.shortname())
                ## match.out_players = enrolledplayers.filter(enroll=False)
                ## match.out_players = sorted(match.out_players, 
                ##                            key=lambda player: player.player.player.shortname())
                ## unknown = list()
                ## for seasonplayer in SeasonPlayer.objects.filter(season=season,
                ##                                                 passive=False):
                ##     # TODO: In future Djangos, use .exists() !!
                ##     if not enrolledplayers.filter(player=seasonplayer):
                ##         unknown.append(seasonplayer)
                ## match.unknown_players = sorted(unknown, 
                ##                                key=lambda player: player.player.shortname())

        context = {
            'season':      season,
            'league_list': league_class.objects.all(),
            'match_list':  match_list,
            'player_list': player_list,
            'team_name':   settings.TEAM_NAME,
            }
        return render_to_response(template_name,
                                  context)

    except Season.DoesNotExist:
        return HttpResponse("Kautta ei olemassa.")


def show_all_players(request,
                     player_class=Player,
                     league_class=League,
                     matchplayer_class=MatchPlayer,
                     template_name='teamstats/show_all_players.html'):
<commit_msg>Fix grouping of enrolled players in season page<commit_after># -*- coding: utf-8 -*-

# Copyright (C) 2011,2012 <NAME>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with this program.  If not, see
# <http://www.gnu.org/licenses/>.

"""
Views for showing different statistics of the team.
"""

from __future__ import division

from django.shortcuts import render_to_response, render
from django.template import RequestContext, Context, loader
from django.http import HttpResponse
from django.db.models import Sum, Q
from django.core.urlresolvers import reverse
from itertools import chain
from django.conf import settings

from teamstats.models import *

def index(request,
          league_class=League,
          template_name='teamstats/index.html'):
    '''
    View for the main page.
    '''
    context = {
        'league_list': league_class.objects.all(),
        'team_name':   settings.TEAM_NAME,
        }
    return render_to_response(template_name,
                              context)

def get_match_enrollments(match, 
                          seasonplayer_class=SeasonPlayer,
                          enrolledplayer_class=EnrolledPlayer):
    """
    Returns the enrollment statuses of the players.
    """
    # Show in/out stats
    seasonplayers = seasonplayer_class.objects.filter(season=match.season,
                                                      passive=False).order_by('player')
    # TODO: Use related_names and then just match.enrolledplayers
    enrollments = enrolledplayer_class.objects.filter(match=match).order_by('player__player')

    ind = 0
    for player in seasonplayers:
        if ind < len(enrollments) and player.id == enrollments[ind].player.id:
            if enrollments[ind].enroll:
                player.choice = 1
            else:
                player.choice = 2
            ind = ind + 1
        else:
            player.choice = 3

    return seasonplayers
    
def get_match_goals(match):
    if match.opponent_goals is not None:
        matchplayers = MatchPlayer.objects.filter(match=match)
        if not matchplayers:
            goals = match.opponent_owngoals
        else:
            goals = matchplayers.aggregate(Sum('goals')).values()[0] + match.opponent_owngoals
    else:
        goals = None

    return goals

def show_season(request, 
                season_id,
                season_class=Season,
                league_class=League,
                seasonplayer_class=SeasonPlayer,
                enrolledplayer_class=EnrolledPlayer,
                match_class=Match,
                template_name='teamstats/show_season.html'):
    '''
    View for showing information about a season: the players with
    statistics and all the matches.
    '''

    try:
        season = season_class.objects.get(id__exact=season_id)

        # Player stats
        player_list = seasonplayer_class.objects.filter(season=season)
        for player in player_list:
            matches = MatchPlayer.objects.filter(player=player)

            player.games = matches.count()
            player.goals = matches.aggregate(goals=Sum('goals')).values()[0]
            player.assists = matches.aggregate(assists=Sum('assists')).values()[0]
            
            if player.goals is None:
                player.goals = 0
            if player.assists is None:
                player.assists = 0

            player.points = player.goals + player.assists
            if player.games:
                player.ppg = player.points / player.games
            else:
                player.ppg = 0.0

                #season_list = season_class.objects.all()

        # TODO: Show opponent own goals!

        # Match list
        match_list = match_class.objects.filter(season=season)
        for match in match_list:
            match.goals = get_match_goals(match)
            match.result = (match.goals != None)
            if not match.result:
                match.player_list = get_match_enrollments(match,
                                                          seasonplayer_class=seasonplayer_class, 
                                                          enrolledplayer_class=enrolledplayer_class)
                match.player_list = sorted(match.player_list, 
                                           key=lambda player: player.player.shortname())
                match.player_list = sorted(match.player_list, 
                                           key=lambda player: player.choice)

        context = {
            'season':      season,
            'league_list': league_class.objects.all(),
            'match_list':  match_list,
            'player_list': player_list,
            'team_name':   settings.TEAM_NAME,
            }
        return render_to_response(template_name,
                                  context)

    except Season.DoesNotExist:
        return HttpResponse("Kautta ei olemassa.")


def show_all_players(request,
                     player_class=Player,
                     league_class=League,
                     matchplayer_class=MatchPlayer,
                     template_name='teamstats/show_all_players.html'):

{'fraction_non_alphanumeric': 0.07959233195826255, 'fraction_numerical': 0.0038016662622340856, 'mean_word_length': 1.8989449003516998, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2440696', 'n_tokens_mistral': 3260, 'n_tokens_neox': 3089, 'n_words': 804}