def QueryTeams(request): # check if HTTP POST request if request.method == 'POST': # get data form = QueryForm(request.POST) # check if user posted all sections if form.is_valid(): # get form data display_all = form.cleaned_data['display_all'] # filter polls with form values teams = Team.objects.all() # if not HTTP POST set empty table else: form = QueryForm() teams = None display_all = None # remove form fields that will not be used form.filter_fields_teams() # render context = {'form' : form, 'teams' : teams, 'display_all' : display_all, } return render_to_response('query_teams.html', context, context_instance=RequestContext(request))
def QueryPollStats(request): # check if HTTP POST request if request.method == 'POST': # get data form = QueryForm(request.POST) # check if user posted all sections if form.is_valid(): # get form data team_abbr = form.cleaned_data['team'] year = form.cleaned_data['year'] week = form.cleaned_data['week'] display_all = form.cleaned_data['display_all'] team_name = team_name_from_abbr(team_abbr) # filter polls with form values pollstats = PollStat.objects.all() if team_abbr != 'ALL': team = Team.objects.get(name=team_name) pollstats = pollstats.filter(team=team) if year != 'ALL': pollstats = pollstats.filter(year=year) if week != 'ALL': pollstats = pollstats.filter(week=week) # if not HTTP POST set empty table else: form = QueryForm() pollstats = None display_all = None # remove form fields that will not be used form.filter_fields_polls() # render context = {'form' : form, 'pollstats' : pollstats, 'display_all' : display_all, } return render_to_response('query_pollstats.html', context, context_instance=RequestContext(request))
def QueryGames(request): # check if HTTP POST request if request.method == 'POST': # get data form = QueryForm(request.POST) # check if user posted all sections if form.is_valid(): #! FIXME: can be faster # get form data team_abbr = form.cleaned_data['team'] year = form.cleaned_data['year'] team_name = team_name_from_abbr(team_abbr) display_all = form.cleaned_data['display_all'] #! FIXME: can be faster # filter games with form options games = Game.objects.all() if year != 'ALL': games = games.filter(year=year) if team_abbr != 'ALL': games = games.filter(Q(home_team__name=team_name) | Q(away_team__name=team_name)) # if not HTTP POST set empty table else: form = QueryForm() games = None display_all = None # remove form fields that will not be used form.filter_fields_games() # render context = {'games' : games, 'form' : form, 'display_all' : display_all, } return render_to_response('query_games.html', context, context_instance=RequestContext(request))
def QueryPlayerStats(request): # check if HTTP POST request if request.method == 'POST': # get data form = QueryForm(request.POST) # check if user posted all sections if form.is_valid(): # get playerstats team_abbr = form.cleaned_data['team'] year = form.cleaned_data['year'] week = form.cleaned_data['week'] stat_type = form.cleaned_data['stat_type'] team_name = team_name_from_abbr(team_abbr) combine_stats = form.cleaned_data['combine_stats'] display_all = form.cleaned_data['display_all'] # filter with form values playerstats = PlayerStat.objects.all() if year != 'ALL': playerstats = playerstats.filter(game__year=year) if week != 'ALL': playerstats = playerstats.filter(game__week=week) if team_abbr != 'ALL': playerstats = playerstats.filter(player__team__name=team_name) if stat_type == 'pass': playerstats = playerstats.exclude(pass_attempts=None) elif stat_type == 'rush': playerstats = playerstats.exclude(rush_attempts=None) elif stat_type == 'rec': playerstats = playerstats.exclude(rec_catches=None) elif stat_type == 'def': playerstats = playerstats.exclude(def_stats=0) # check if we are suppose to combine the stats for a single player if combine_stats: # create a list that will hold aggregate stats aggstats = [] # find all Player slugs we will need and loop over them player_slugs = playerstats.order_by('player__slug').values('player__slug').distinct() for player_slug in player_slugs: player_slug = player_slug['player__slug'] # create Data instance to hold player aggregate data aggstat = Data() aggstat.year = year # get number of games aggstat.num_games = sum(1 for playerstat in playerstats if playerstat.player.slug == player_slug) # get Player aggstat.player = Player.objects.get(slug=player_slug) # get stats if stat_type == 'pass': stat_names = ['pass_completions', 'pass_attempts', 'pass_yards', 'pass_tds', 'pass_ints'] elif stat_type == 'rush': stat_names = ['rush_attempts', 'rush_yards', 'rush_tds', 'rush_fums'] elif stat_type == 'rec': stat_names = ['rec_catches', 'rec_yards', 'rec_tds'] elif stat_type == 'def': stat_names = ['def_tackles', 'def_sacks', 'def_safeties', 'def_blocks', 'def_ffs', 'def_frs', 'def_tds', 'def_ints'] for stat_name in stat_names: setattr( aggstat, stat_name, sum(getattr(playerstat, stat_name) \ for playerstat in playerstats if playerstat.player.slug == player_slug) ) # add to list of aggregate stats aggstats.append(aggstat) # replace playerstats = aggstats # if not HTTP POST then set empty table else: form = QueryForm() playerstats = None stat_type = None combine_stats = None display_all = None # render context = {'playerstats' : playerstats, 'form' : form, 'stat_type' : stat_type, 'combine_stats' : combine_stats, 'display_all' : display_all, } return render_to_response('query_playerstats.html', context, context_instance=RequestContext(request))