belt calculation on league page

This commit is contained in:
James Turk 2012-05-29 02:43:53 -04:00
parent 29543f8b1c
commit d0fdbe1763
2 changed files with 72 additions and 26 deletions

View File

@ -1,18 +1,17 @@
{% extends "base.html" %} {% extends "base.html" %}
{% load static %} {% load static %}
{% block title %} League {% endblock %} {% block title %} {{league}} - FOWL {% endblock %}
{% block content %} {% block content %}
<header class="jumbotron subhead" id="overview"> <header class="jumbotron subhead" id="overview">
<h1>League</h1> <h1>{{league}}</h1>
<p class="lead">An overview of our league</p> <p class="lead"></p>
</header> </header>
<br><br> <br><br>
<h2>Belts</h2> <h2>Belts</h2>
<div class="row-fluid"> <div class="row-fluid">
<!-- begin belt block --> {% for belt, holder in belts.iteritems %}
{% for belt in belts %}
<div class="span3"> <div class="span3">
<table class="table"> <table class="table">
<thead> <thead>
@ -22,25 +21,17 @@
</td> </td>
</tr> </tr>
</thead> </thead>
<tody> <tbody>
<tr> <tr> <td>Title holder:</td> <td>{{holder.name}}</td> </tr>
<td>Title holder:</td> <tr> <td>Held Since:</td> <td>{{holder.date}}</td> </tr>
<td>I'm AWEsome</td> <tr> <td>Reign:</td> <td>{{holder.date|timesince}}</td> </tr>
</tr> {% for team,points in holder.teams.iteritems %}
<tr> <tr><td>{{team}}</td><td>{{points}}</td></tr>
<td>Held Since:</td> {% endfor %}
<td>5/18/2012</td>
</tr>
<tr>
<td>Reign:</td>
<td>8</td>
</tr>
</tbody> </tbody>
</table> </table>
</div> </div>
{% endfor %} {% endfor %}
<!-- end belt block -->
</div> </div>
<br><br><br> <br><br><br>

View File

@ -1,6 +1,7 @@
from itertools import izip_longest from itertools import izip_longest
from collections import defaultdict from collections import defaultdict
from django.shortcuts import render, get_object_or_404 from django.shortcuts import render, get_object_or_404
from django.db.models import Sum
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from fowl.game.models import (Team, TeamPoints, Star, Event, League, from fowl.game.models import (Team, TeamPoints, Star, Event, League,
OUTCOMES, TITLES) OUTCOMES, TITLES)
@ -84,16 +85,70 @@ def edit_event(request, event):
def league(request, league_id): def league(request, league_id):
league = get_object_or_404(League, pk = league_id) league = get_object_or_404(League, pk = league_id)
leagues = League.objects.filter(teams__login=request.user) 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 = { context = {
'view': 'league', 'view': 'league',
'belts': ['ic', 'us', 'heavyweight', 'wwe'], 'belts': belts,
'league': league, 'league': league,
'leagues': leagues 'leagues': leagues
} }
teams = list(Team.objects.filter(league__id=league_id)) context['teams'] = teams.values()
context['teams'] = teams
context['star_sets'] = izip_longest(*(team.stars.all().order_by("division") 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) return render(request, "league.html", context)