def matchup(request, matchup_id): matchup = get_object_or_404(Matchup, pk=matchup_id) context_data['matchup'] = matchup home_stats = calculate_team_totals(matchup.home_team, start_day=matchup.start_date, end_day=matchup.end_date) away_stats = calculate_team_totals(matchup.away_team, start_day=matchup.start_date, end_day=matchup.end_date) home_totals = home_stats.pop('totals') away_totals = away_stats.pop('totals') context_data['home_totals'] = home_totals context_data['away_totals'] = away_totals # attach matchup games for each player for player in matchup.home_team.players: home_stats[player.id]['matchup_games'] = player.matchup_games(matchup) for player in matchup.away_team.players: away_stats[player.id]['matchup_games'] = player.matchup_games(matchup) context_data['home_stats'] = home_stats context_data['away_stats'] = away_stats return render_to_response("matchup.html", context_data, context_instance=RequestContext(request))
def matchup(request, matchup_id): matchup = get_object_or_404(Matchup, pk=matchup_id) context_data['matchup'] = matchup home_stats = calculate_team_totals(matchup.home_team, start_day=matchup.start_date, end_day=matchup.end_date) away_stats = calculate_team_totals(matchup.away_team, start_day=matchup.start_date, end_day=matchup.end_date) home_totals = home_stats.pop('totals') away_totals = away_stats.pop('totals') context_data['home_totals'] = home_totals context_data['away_totals'] = away_totals # attach matchup games for each player for player in matchup.home_team.players: home_stats[player.id]['matchup_games'] = player.matchup_games(matchup) for player in matchup.away_team.players: away_stats[player.id]['matchup_games'] = player.matchup_games(matchup) context_data['home_stats'] = home_stats context_data['away_stats'] = away_stats return render_to_response("matchup.html", context_data, context_instance=RequestContext(request))
def add_player(request, player_id, num_days=15): player = get_object_or_404(Player, pk=player_id) delta = timedelta(days=int(num_days)) start_day = today - delta player_stats = calculate_totals(player, start_day=start_day, end_day=today) player_avg_stats = calculate_avgs(player_stats) context_data['player_avg_stats'] = player_avg_stats context_data['player'] = player team = Team.objects.get(owner=request.user.id) team_total_stats = calculate_team_totals(team, start_day=start_day, end_day=today) team_avg_stats = calculate_team_avgs(team_total_stats) team_avg_stats.pop('totals') context_data['stats'] = team_avg_stats return render_to_response('add_player.html', context_data, context_instance=RequestContext(request))
def team_profile(request, team_id, num_days=10): team = Team.objects.get(id=team_id) context_data['team'] = team delta = timedelta(days=int(num_days)) start_day = today - delta context_data['num_days'] = num_days total_stats = calculate_team_totals(team, start_day=start_day, end_day=today) stats = calculate_team_avgs(total_stats) stats.pop('totals') context_data['stats'] = stats context_data['user_team'] = Team.objects.get(owner=request.user.id) return render_to_response("team_profile.html", context_data, context_instance=RequestContext(request))
def handle(self, *args, **options): today = datetime.today() matchups = Matchup.objects.filter(finalized=False).filter(end_date__lt=today) for matchup in matchups: print('Evaluating matchup {0}'.format(matchup)) home_team_stats = calculate_team_totals(matchup.home_team, start_day=matchup.start_date, end_day = matchup.end_date) away_team_stats = calculate_team_totals(matchup.away_team, start_day=matchup.start_date, end_day = matchup.end_date) home_totals = home_team_stats.pop('totals') away_totals = away_team_stats.pop('totals') home_team_totals = prune_totals(home_totals) away_team_totals = prune_totals(away_totals) print('home_team_totals: {}'.format(home_team_totals)) print('away_team_totals: {}'.format(away_team_totals)) home_wins = 0 home_losses = 0 home_ties = 0 away_wins = 0 away_losses = 0 away_ties = 0 for k, v in home_team_totals.iteritems(): if k == 'turnovers': v = -v t = away_team_totals[k] away_team_totals[k] = -t if v > away_team_totals[k]: home_wins += 1 away_losses += 1 elif v < away_team_totals[k]: home_losses += 1 away_wins += 1 else: home_ties += 1 away_ties += 1 print('{0} result: {1}-{2}-{3}'.format(matchup.home_team, home_wins, home_losses, home_ties)) print('{0} result: {1}-{2}-{3}'.format(matchup.away_team, away_wins, away_losses, away_ties)) if home_ties > 0: result = "{0}-{1}-{2}".format(home_wins, home_losses, home_ties) else: result = "{0}-{1}".format(home_wins, home_losses) # save the matchup matchup.result = result matchup.finalized = True matchup.save() # save the respective teams records matchup.home_team.wins += home_wins matchup.home_team.losses += home_losses matchup.home_team.ties += home_ties matchup.home_team.save() print("saved {0} with new record: {1}".format(matchup.home_team, matchup.home_team.record)) matchup.away_team.wins += away_wins matchup.away_team.losses += away_losses matchup.away_team.ties += away_ties matchup.away_team.save() print("saved {0} with new record: {1}".format(matchup.away_team, matchup.away_team.record))
def handle(self, *args, **options): today = datetime.today() matchups = Matchup.objects.filter(finalized=False).filter( end_date__lt=today) for matchup in matchups: print('Evaluating matchup {0}'.format(matchup)) home_team_stats = calculate_team_totals( matchup.home_team, start_day=matchup.start_date, end_day=matchup.end_date) away_team_stats = calculate_team_totals( matchup.away_team, start_day=matchup.start_date, end_day=matchup.end_date) home_totals = home_team_stats.pop('totals') away_totals = away_team_stats.pop('totals') home_team_totals = prune_totals(home_totals) away_team_totals = prune_totals(away_totals) print('home_team_totals: {}'.format(home_team_totals)) print('away_team_totals: {}'.format(away_team_totals)) home_wins = 0 home_losses = 0 home_ties = 0 away_wins = 0 away_losses = 0 away_ties = 0 for k, v in home_team_totals.iteritems(): if k == 'turnovers': v = -v t = away_team_totals[k] away_team_totals[k] = -t if v > away_team_totals[k]: home_wins += 1 away_losses += 1 elif v < away_team_totals[k]: home_losses += 1 away_wins += 1 else: home_ties += 1 away_ties += 1 print('{0} result: {1}-{2}-{3}'.format(matchup.home_team, home_wins, home_losses, home_ties)) print('{0} result: {1}-{2}-{3}'.format(matchup.away_team, away_wins, away_losses, away_ties)) if home_ties > 0: result = "{0}-{1}-{2}".format(home_wins, home_losses, home_ties) else: result = "{0}-{1}".format(home_wins, home_losses) # save the matchup matchup.result = result matchup.finalized = True matchup.save() # save the respective teams records matchup.home_team.wins += home_wins matchup.home_team.losses += home_losses matchup.home_team.ties += home_ties matchup.home_team.save() print("saved {0} with new record: {1}".format( matchup.home_team, matchup.home_team.record)) matchup.away_team.wins += away_wins matchup.away_team.losses += away_losses matchup.away_team.ties += away_ties matchup.away_team.save() print("saved {0} with new record: {1}".format( matchup.away_team, matchup.away_team.record))