def render_tips(request, selected_round, game_tip_data): """ View tips """ if not game_tip_data: return b'' # Summarise tips for each game data = [] games = [] byes = selected_round.bye_clubs for game, tips in game_tip_data: # Group tips according to legends games and byes if byes: game_tips = [t for t in tips if t.club not in byes] bye_tips = [t for t in tips if t.club in byes] else: game_tips = tips[:] bye_tips = [] game_tips = chunks(game_tips) games.append(game) if game.status == constants.Game.SCHEDULED: summary = tip_summary(game, tips) else: summary = tip_result_summary(game, tips) data.append( { 'game': game, 'game_tips': game_tips, 'bye_tips': bye_tips, 'summary': summary } ) context = { 'round': selected_round, 'data_type': 'tip', 'show_form': False, 'games': games, 'data': data, } content = render( request, 'tips.html', context ) return content.content
def render_results(request, selected_round, games): """ Render all results """ if not games: return b'' round_values = { 'bonus_score': [], 'crowds_score': [], 'margins_score': [], 'score': [], 'supercoach_score': [], 'winners_score': [], } score_attrs = ( 'winners_score', 'margins_score', 'crowds_score', 'supercoach_score', 'score' ) legends_games = selected_round.games.all() for game in legends_games: # Update round scores for team in ('legends_away', 'legends_home'): for attr in score_attrs: _attr = '{}_{}'.format(team, attr) round_values[attr].append(getattr(game, _attr)) # Winners bonus round_values['bonus_score'].append(game.legends_away_winners_bonus) round_values['bonus_score'].append(game.legends_home_winners_bonus) # Handle byes byes = selected_round.byes.all() if byes: for bye in byes: for attr in score_attrs: round_values[attr].append(getattr(bye, attr)) # Winners bonus round_values['bonus_score'].append(bye.winners_bonus) # Total summary for the round total_summary = {} for attr in score_attrs: summary = { 'max': max(round_values[attr]), 'min': min(round_values[attr]), 'avg': mean(round_values[attr]) } total_summary[attr] = summary # Winners bonus total_summary['winners_bonus'] = { 'max': max(round_values['bonus_score']), 'min': min(round_values['bonus_score']), 'avg': mean(round_values['bonus_score']) } # Split games into two chunks if selected_round.is_finals: size, remainder = divmod(selected_round.num_games, 2) if remainder: size += 1 elif selected_round.has_byes: size, remainder = divmod(len(selected_round.bye_clubs), 2) if remainder: size += 1 else: size = 5 grouped_games = chunks((g for g in legends_games), size) # Split byes into two chunks size, remainder = divmod(selected_round.num_games, 2) if remainder: size += 1 byes = [byes[:size], byes[size:]] context = { 'round': selected_round, 'byes': byes, 'grouped_games': grouped_games, 'total_summary': total_summary } content = render( request, 'legends_result.html', context ) return content.content