From d0fdbe17638d8dcae480a654cb5b8d5c898b71bc Mon Sep 17 00:00:00 2001 From: James Turk Date: Tue, 29 May 2012 02:43:53 -0400 Subject: [PATCH] belt calculation on league page --- fowl/game/templates/league.html | 35 +++++++----------- fowl/game/views.py | 63 ++++++++++++++++++++++++++++++--- 2 files changed, 72 insertions(+), 26 deletions(-) diff --git a/fowl/game/templates/league.html b/fowl/game/templates/league.html index fdf670e..6e4fd61 100644 --- a/fowl/game/templates/league.html +++ b/fowl/game/templates/league.html @@ -1,18 +1,17 @@ {% extends "base.html" %} {% load static %} -{% block title %} League {% endblock %} +{% block title %} {{league}} - FOWL {% endblock %} {% block content %}
-

League

-

An overview of our league

+

{{league}}

+



Belts

-
- - {% for belt in belts %} +
+ {% for belt, holder in belts.iteritems %}
@@ -22,25 +21,17 @@ - - - - - - - - - - - - - + + + + + {% for team,points in holder.teams.iteritems %} + + {% endfor %}
Title holder:I'm AWEsome
Held Since:5/18/2012
Reign:8
Title holder: {{holder.name}}
Held Since: {{holder.date}}
Reign: {{holder.date|timesince}}
{{team}}{{points}}
{% endfor %} - -



@@ -50,7 +41,7 @@ {% for team in teams %} - {{team}} + {{team}} {% endfor %} diff --git a/fowl/game/views.py b/fowl/game/views.py index 3239c02..2f83690 100644 --- a/fowl/game/views.py +++ b/fowl/game/views.py @@ -1,6 +1,7 @@ from itertools import izip_longest from collections import defaultdict from django.shortcuts import render, get_object_or_404 +from django.db.models import Sum from django.contrib.auth.decorators import login_required from fowl.game.models import (Team, TeamPoints, Star, Event, League, OUTCOMES, TITLES) @@ -84,16 +85,70 @@ def edit_event(request, event): def league(request, league_id): league = get_object_or_404(League, pk = league_id) leagues = League.objects.filter(teams__login=request.user) + teams = {team.name: team for team in + Team.objects.filter(league__id=league_id)} + + # grab the show totals for all shows + show_totals = TeamPoints.objects.all().values( + 'match__event__id', 'match__event__name', 'team__name').annotate( + Sum('points')) + belts = {'us': {'name': None, 'points': 0, 'date': None, + 'teams': {team: 0 for team in teams}}, + 'ic': {'name': None, 'points': 0, 'date': None, + 'teams': {team: 0 for team in teams}}, + 'heavyweight': {'name': None, 'points': 0, 'date': None, + 'teams': {team: 0 for team in teams}}, + 'wwe': {'name': None, 'points': 0, 'date': None, + 'teams': {team: 0 for team in teams}}, + } + belt_mapping = {'smackdown': 'ic', 'raw': 'us'} + + # go over all events in order to determine belt holders + for event in Event.objects.all().order_by('date'): + # determine which belt is being competed for + belt_name = belt_mapping.get(event.name.lower(), 'heavyweight') + # get team scores for this event + team_points = TeamPoints.objects.filter(match__event=event + ).values('team__name').annotate(points=Sum('points')) + # figure out who won event, also tally wwe belt points + max_points = 0 + event_winner = [] + for tp in team_points: + if tp['points'] > max_points: + max_points = tp['points'] + event_winner = [tp['team__name']] + elif tp['points'] == max_points: + event_winner.append(tp['team__name']) + belts['wwe']['teams'][tp['team__name']] += tp['points'] + + # add a point to all event winners + for ew in event_winner: + belts[belt_name]['teams'][ew] += 1 + # loop again after adding points to check for new holder + for ew in event_winner: + if belts[belt_name]['teams'][ew] > belts[belt_name]['points']: + belts[belt_name]['points'] = belts[belt_name]['teams'][ew] + if belts[belt_name]['name'] != ew: + belts[belt_name]['name'] = ew + belts[belt_name]['date'] = event.date + # do WWE belt check on PPVs + if belt_name == 'heavyweight': + belt_name = 'wwe' + if belts[belt_name]['teams'][ew] > belts[belt_name]['points']: + belts[belt_name]['points'] = belts[belt_name]['teams'][ew] + if belts[belt_name]['name'] != ew: + belts[belt_name]['name'] = ew + belts[belt_name]['date'] = event.date + context = { 'view': 'league', - 'belts': ['ic', 'us', 'heavyweight', 'wwe'], + 'belts': belts, 'league': league, 'leagues': leagues } - teams = list(Team.objects.filter(league__id=league_id)) - context['teams'] = teams + context['teams'] = teams.values() context['star_sets'] = izip_longest(*(team.stars.all().order_by("division") - for team in teams)) + for team in teams.itervalues())) return render(request, "league.html", context)