Commits

David Krauth  committed 9955a7f

Incremental update

  • Participants
  • Parent commits 980af2f

Comments (0)

Files changed (3)

File management/commands/football_ops.py

     else:
         week = football.GameWeek.objects.current_week()
         
+    print 'NFL', week
     for place, pick in football.sorted_standings(
         list(football.WeeklyPick.objects.select_related().filter(week=week))
     ):
         print '%2d %2d %2d %s' % (place, pick.correct, pick.points, pick.user)
 
 
+#-------------------------------------------------------------------------------
+@register
+def ncaa_standings(**options):
+    wk = options.get('week')
+    if wk:
+        week = football.Week.objects.get(week=wk)
+    else:
+        week = football.Week.objects.current_week()
+        
+    res = football.Picks.objects.results(week)
+    
+    weighted = []
+    prev_place, prev_results = 1, (0,0)
+    
+    items = football.Picks.objects.standings(week)
+    for pick in items:
+        print '%2d %2d %s' % (pick.score, pick.points, pick.user)
+    
+    for i, item in enumerate(items, 1): 
+        results = (item.score, abs(item.points - res.points))
+        place = prev_place if results == prev_results else i
+        prev_place, prev_results = place, results
+        weighted.append((place, item))
+        
+    
+    
+    print 'NCAA', week, res.points
+    for i, pick in weighted:
+        print '%2d %2d %2d %s' % (i, pick.score, pick.points, pick.user)
+
+
 #===============================================================================
 class Command(BaseCommand):
     option_list = BaseCommand.option_list + (
 
 
 #===============================================================================
+class TeamManager(models.Manager):
+    
+    #---------------------------------------------------------------------------
+    def missing(self, items):
+        teams = self.team_names_dict()
+        return set([item for item in items if item not in teams])
+    
+    #---------------------------------------------------------------------------
+    def team_names_dict(self):
+        teams = dict([(t.name, t) for t in self.all()])
+        teams.update([(a.name, a.team) for a in Alias.objects.all()])
+        return teams
+        
+    #---------------------------------------------------------------------------
+    def find(self, name):
+        team = self.filter(name=name)
+        if team:
+            return team[0]
+            
+        alias = Alias.objects.filter(name=name)
+        if alias:
+            return alias[0].team
+
+
+#===============================================================================
+class Team(models.Model):
+    name     = models.CharField(max_length=50, unique=True)
+    abbr     = models.CharField(max_length=8, blank=True)
+    nickname = models.CharField(max_length=50)
+    location = models.CharField(max_length=100)
+    image    = models.CharField(max_length=50)
+    conf     = models.ForeignKey(Conference)
+    division = models.CharField(max_length=12, blank=True)
+    
+    objects = TeamManager()
+    
+    #===========================================================================
+    class Meta:
+        ordering = ('name',)
+        
+    #---------------------------------------------------------------------------
+    def __unicode__(self):
+        return self.name
+    
+    #---------------------------------------------------------------------------
+    def _get_aliases(self):
+        return ','.join(self.alias_set.values_list('name', flat=True))
+
+    #---------------------------------------------------------------------------
+    def _set_aliases(self, values):
+        self.alias_set.all().delete()
+        for value in values.split(','):
+            self.alias_set.create(name=value.strip())
+        
+    aliases = property(_get_aliases, _set_aliases)
+
+    #---------------------------------------------------------------------------
+    @property
+    def image_url(self):
+        return '%simg/ncaaf/%s ' % (settings.MEDIA_URL, self.image)
+        
+    #---------------------------------------------------------------------------
+    def ranking(self, week):
+        return Ranking.objects.filter(team=self, week__week=week)
+        
+    #---------------------------------------------------------------------------
+    @property
+    def schedule(self):
+        games = []
+        for game in  Game.objects.filter(
+            models.Q(home=self) | models.Q(away=self)
+        ).order_by('dt'):
+            games.append((game.home if game.away.id == self.id else game.away, game))
+        
+        return games
+
+
+#===============================================================================
+class Alias(models.Model):
+    team = models.ForeignKey(Team)
+    name = models.CharField(max_length=50, unique=True)
+
+
+#===============================================================================
+class WeekManager(models.Manager):
+    
+    #---------------------------------------------------------------------------
+    def available_weeks(self):
+        now = datetime_now()
+        return self.filter(
+            models.Q(ends__lte=now) | models.Q(starts__lte=now),
+            season=CURRENT_SEASON
+        )
+
+    #---------------------------------------------------------------------------
+    def open_weeks(self):
+        now = datetime_now()
+        return self.filter(ends__gt=now, season=CURRENT_SEASON)
+
+    #---------------------------------------------------------------------------
+    def current_week(self):
+        now = datetime_now()
+        try:
+            return self.get(starts__lte=now, ends__gt=now)
+        except:
+            return self.latest('ends')
+
+
+#===============================================================================
+class Week(models.Model):
+    season  = models.IntegerField(default=CURRENT_SEASON)
+    week    = models.IntegerField()
+    starts  = models.DateTimeField()
+    kickoff = models.DateTimeField()
+    ends    = models.DateTimeField()
+
+    objects = WeekManager()
+    
+    #---------------------------------------------------------------------------
+    def __unicode__(self):
+        return 'Wk %d/%d' % (self.week, self.season)
+        
+    #---------------------------------------------------------------------------
+    def update_kickoff(self):
+        game = self.game_set.filter(tm__isnull=False).order_by('dt', 'tm')[0]
+        self.kickoff = datetime.combine(game.dt, game.tm)
+        self.save()
+
+    #---------------------------------------------------------------------------
+    @property
+    def is_open(self):
+        return datetime_now() < self.kickoff
+
+
+#===============================================================================
+class Attr(object):
+    
+    #---------------------------------------------------------------------------
+    def __init__(self, **kws):
+        self.__dict__.update(kws)
+    
+    #---------------------------------------------------------------------------
+    def __getitem__(self, key):
+        return getattr(self, key)
+
+
+#===============================================================================
+class GameManager(models.Manager):
+    
+    #---------------------------------------------------------------------------
+    def top_25_info(self, week=None, pick_ids=None):
+        week         = week or Week.objects.current_week()
+        is_bowl_week = week.week == 16
+        ranking      = list(week.ranking_set.all())
+        game_details = []
+        ranking_dict = dict([(r.team.id, r.rank) for r in ranking])
+        team_ids     = ranking_dict.keys()
+        bonus_ids    = set([])
+        now          = datetime_now()
+        master_ids   = Picks.objects.results(week).team_ids
+        games        = self.filter(week=week)
+        
+        if not is_bowl_week:
+            games = games.filter(models.Q(home__in=team_ids) | models.Q(away__in=team_ids))
+
+        if pick_ids is None:
+            pick_ids = Picks.objects.fetch(admin_user, week).team_ids
+            
+        playing = []
+        games = list(games)
+        # print '***', len(games)
+        for game in games:
+            if game.home.id in ranking_dict:
+                playing.append((ranking_dict[game.home.id], game.home.id))
+
+            if game.away.id in ranking_dict:
+                playing.append((ranking_dict[game.away.id], game.away.id))
+        
+        highest_ranked = sorted(playing, cmp=lambda a,b: cmp(a[0], b[0]))[0][1]
+        
+        for game in games:
+            home_rank = ranking_dict.get(game.home.id, None)
+            away_rank = ranking_dict.get(game.away.id, None)
+            
+            if away_rank and away_rank <= 10 and not home_rank:
+                bonus_ids.add(game.home.id)
+                
+            if home_rank and home_rank <= 10 and not away_rank:
+                bonus_ids.add(game.away.id)
+            
+            
+            game_details.append(Attr(
+                is_correct=(
+                    (game.home.id in master_ids and game.home.id in pick_ids) or
+                    (game.away.id in master_ids and game.away.id in pick_ids)
+                ),
+                home=Attr(team=game.home, rank=home_rank, is_picked=game.home.id in pick_ids),
+                away=Attr(team=game.away, rank=away_rank, is_picked=game.away.id in pick_ids),
+                date=game.dt,
+                time=game.tm,
+                highest_ranked=game.home.id == highest_ranked or game.away.id == highest_ranked,
+                is_open=now < game.kickoff,
+                id='%05d' % game.id
+            ))
+            
+        return Attr(games=game_details, ranking=ranking, bonus_ids=bonus_ids)
+
+
+#===============================================================================
+class Game(models.Model):
+    
+    #===========================================================================
+    class Result(ChoiceEnumeration):
+        HOME = ChoiceEnumeration.Option('HOME', 'Home team won')
+        AWAY = ChoiceEnumeration.Option('AWAY', 'Away team won')
+        TIE  = ChoiceEnumeration.Option('TIE',  'Tie')
+        TBP  = ChoiceEnumeration.Option('TBP',  'To be played', default=True)
+    
+    home   = models.ForeignKey(Team, related_name='home_game_set')
+    away   = models.ForeignKey(Team, related_name='away_game_set')
+    week   = models.ForeignKey(Week)
+    dt     = models.DateField()
+    tm     = models.TimeField(blank=True, null=True)
+
+    status = models.CharField(
+        max_length=4, 
+        choices=Result.CHOICES, 
+        default=Result.DEFAULT
+    )
+    
+    objects = GameManager()
+    
+    #===========================================================================
+    class Meta:
+        ordering = ('dt', 'tm')
+
+    #---------------------------------------------------------------------------
+    def __unicode__(self):
+        return '%s @ %s' % (self.away, self.home)
+        
+    #---------------------------------------------------------------------------
+    @property
+    def kickoff(self):
+        return datetime.combine(self.dt, self.tm or time(12))
+        
+
+#===============================================================================
+class RankingManager(models.Manager):
+
+    #---------------------------------------------------------------------------
+    def parse_espn(self, text):
+        '''\
+        1\tUSC (25)\t1-0\t1445
+        2\tAlabama (17)\t0-0\t1411
+        3\tLSU (16)\t2-0-1\t1402
+        4\tOklahoma (1)\t0-0\t1286
+        5\tOregon\t0-0\t1274
+        6\tGeorgia\t0-0\t1107
+        7\tFlorida State\t0-0\t1093
+        8\tMichigan (1)\t0-0\t1000
+        9\tSouth Carolina\t0-0\t994
+        10\tArkansas\t0-0\t963
+        11\tWest Virginia\t0-0\t856
+        12\tWisconsin\t0-0\t838
+        13\tMichigan State\t0-0\t742
+        14\tClemson\t0-0\t615
+        15\tTexas\t0-0\t569
+        16\tVirginia Tech\t0-0\t548
+        17\tNebraska\t0-0\t485
+        18\tOhio State\t0-0\t474
+        19\tOklahoma State\t0-0\t430
+        20\tTCU\t0-0\t397
+        21\tStanford\t0-0\t383
+        22\tKansas State\t0-0\t300
+        23\tFlorida\t0-0\t214
+        24\tBoise State\t0-0\t212
+        25\tLouisville\t0-0\t105'''
+        items = []
+        for line in text.splitlines():
+            rank, team, record, points = line.split('\t')
+            paren = team.find('(')
+            if paren > -1:
+                team = team[:paren].strip()
+                
+            items.append((
+                int(rank),
+                Team.objects.find(team),
+                record
+            ))
+
+        return items
+
+    #---------------------------------------------------------------------------
+    def new_ranking(self, entries, week_no, poll):
+        return [
+            self.create(
+                team=team,
+                rank=rank,
+                week=Week.objects.get(week=week_no),
+                record=record,
+                poll=poll
+            )
+            for rank, team, record in entries
+        ]
+
+    #---------------------------------------------------------------------------
+    def new_ranking_from_text(self, text, week_no, poll):
+        self.new_ranking(self.parse_espn(text), week_no, poll)
+    
+    #---------------------------------------------------------------------------
+    def for_week(week):
+        return self.filter(week=week)
+
+
+#===============================================================================
+class Ranking(models.Model):
+    poll = models.CharField(max_length=8)
+    team = models.ForeignKey(Team)
+    rank = models.IntegerField()
+    week = models.ForeignKey(Week)
+    record = models.CharField(max_length=8)
+    
+    objects = RankingManager()
+    
+    #===========================================================================
+    class Meta:
+        ordering = ('rank',)
+    
+    #---------------------------------------------------------------------------
+    def __unicode__(self):
+        return '%d %s W%d' % (self.rank, self.team, self.week.week)
+
+    #---------------------------------------------------------------------------
+    def top_10(self, week):
+        return [r.team for r in self.filter(week=week, rank__lte=10).select_related()]
+
+
+
+#===============================================================================
+class PicksMangager(models.Manager):
+    
+    #---------------------------------------------------------------------------
+    def fetch(self, user, week):
+        picks, _ = self.get_or_create(user=user, week=week)
+        return picks
+        
+    #---------------------------------------------------------------------------
+    def results(self, week):
+        results, created = self.get_or_create(user=admin_user, week=week)
+        return results
+
+    #---------------------------------------------------------------------------
+    def user_picks(self, week):
+        return self.filter(week=week).exclude(user__id=ADMIN_ID)
+        
+    #---------------------------------------------------------------------------
+    def fix_scores(self, week=None):
+        week = week or Week.objects.current_week()
+        master = Picks.objects.fetch(admin_user, week)
+        self.update_scores(master)
+        
+    #---------------------------------------------------------------------------
+    def update_scores(self, master):
+        master_ids = master.team_ids
+        info       = Game.objects.top_25_info(master.week, master_ids)
+        bonus_ids  = master_ids & info.bonus_ids
+        for pick in self.user_picks(master.week):
+            team_ids   = pick.team_ids
+            pick.score = len(master_ids & team_ids)
+            pick.score += len(bonus_ids & team_ids)
+            pick.save()
+            
+    #---------------------------------------------------------------------------
+    def standings(self, week):
+        results = self.results(week)
+        
+        def _cmp(a, b):
+            return -cmp(a.score, b.score) or (
+                0 if not results.points else cmp(
+                    abs(a.points - results.points),
+                    abs(b.points - results.points)
+                )
+            )
+            
+        return sorted(self.user_picks(week), _cmp)
+
+
+
+#===============================================================================
+class Picks(models.Model):
+    user    = models.ForeignKey(User)
+    week    = models.ForeignKey(Week)
+    picks   = models.CharField(blank=True, max_length=200)
+    score   = models.IntegerField(default=0)
+    points  = models.IntegerField(default=0)
+    created = models.DateTimeField(auto_now_add=True)
+    updated = models.DateTimeField(auto_now=True)
+    
+    objects = PicksMangager()
+    
+    #===========================================================================
+    class Meta:
+        ordering = ('-score',)
+    
+    #---------------------------------------------------------------------------
+    def set_picks(self, items, points=0):
+        self.picks = ','.join(items)
+        self.points = points
+        self.save()
+        if self.user.id == ADMIN_ID:
+            Picks.objects.update_scores(self)
+    
+    #---------------------------------------------------------------------------
+    @property
+    def team_ids(self):
+        return set([int(t, 10) for t in self.picks.split(',') if t])
+    
+    #---------------------------------------------------------------------------
+    @property
+    def teams(self):
+        return Team.objects.filter(id__in=self.team_ids)
+
+
+
+################################################################################
+# NFL
+################################################################################
+
+
+#===============================================================================
 class NFLTeamManager(models.Manager):
     
     ALIASES = dict(
 
 
 #===============================================================================
-class TeamManager(models.Manager):
-    
-    #---------------------------------------------------------------------------
-    def missing(self, items):
-        teams = self.team_names_dict()
-        return set([item for item in items if item not in teams])
-    
-    #---------------------------------------------------------------------------
-    def team_names_dict(self):
-        teams = dict([(t.name, t) for t in self.all()])
-        teams.update([(a.name, a.team) for a in Alias.objects.all()])
-        return teams
-        
-    #---------------------------------------------------------------------------
-    def find(self, name):
-        team = self.filter(name=name)
-        if team:
-            return team[0]
-            
-        alias = Alias.objects.filter(name=name)
-        if alias:
-            return alias[0].team
-
-
-#===============================================================================
-class Team(models.Model):
-    name     = models.CharField(max_length=50, unique=True)
-    abbr     = models.CharField(max_length=8, blank=True)
-    nickname = models.CharField(max_length=50)
-    location = models.CharField(max_length=100)
-    image    = models.CharField(max_length=50)
-    conf     = models.ForeignKey(Conference)
-    division = models.CharField(max_length=12, blank=True)
-    
-    objects = TeamManager()
-    
-    #===========================================================================
-    class Meta:
-        ordering = ('name',)
-        
-    #---------------------------------------------------------------------------
-    def __unicode__(self):
-        return self.name
-    
-    #---------------------------------------------------------------------------
-    def _get_aliases(self):
-        return ','.join(self.alias_set.values_list('name', flat=True))
-
-    #---------------------------------------------------------------------------
-    def _set_aliases(self, values):
-        self.alias_set.all().delete()
-        for value in values.split(','):
-            self.alias_set.create(name=value.strip())
-        
-    aliases = property(_get_aliases, _set_aliases)
-
-    #---------------------------------------------------------------------------
-    @property
-    def image_url(self):
-        return '%simg/ncaaf/%s ' % (settings.MEDIA_URL, self.image)
-        
-    #---------------------------------------------------------------------------
-    def ranking(self, week):
-        return Ranking.objects.filter(team=self, week__week=week)
-        
-    #---------------------------------------------------------------------------
-    @property
-    def schedule(self):
-        games = []
-        for game in  Game.objects.filter(
-            models.Q(home=self) | models.Q(away=self)
-        ).order_by('dt'):
-            games.append((game.home if game.away.id == self.id else game.away, game))
-        
-        return games
-
-
-#===============================================================================
-class Alias(models.Model):
-    team = models.ForeignKey(Team)
-    name = models.CharField(max_length=50, unique=True)
-
-
-#===============================================================================
-class WeekManager(models.Manager):
-    
-    #---------------------------------------------------------------------------
-    def available_weeks(self):
-        now = datetime_now()
-        return self.filter(
-            models.Q(ends__lte=now) | models.Q(starts__lte=now),
-            season=CURRENT_SEASON
-        )
-
-    #---------------------------------------------------------------------------
-    def open_weeks(self):
-        now = datetime_now()
-        return self.filter(ends__gt=now, season=CURRENT_SEASON)
-
-    #---------------------------------------------------------------------------
-    def current_week(self):
-        now = datetime_now()
-        try:
-            return self.get(starts__lte=now, ends__gt=now)
-        except:
-            return self.latest('ends')
-
-
-#===============================================================================
-class Week(models.Model):
-    season  = models.IntegerField(default=CURRENT_SEASON)
-    week    = models.IntegerField()
-    starts  = models.DateTimeField()
-    kickoff = models.DateTimeField()
-    ends    = models.DateTimeField()
-
-    objects = WeekManager()
-    
-    #---------------------------------------------------------------------------
-    def __unicode__(self):
-        return 'Wk %d/%d' % (self.week, self.season)
-        
-    #---------------------------------------------------------------------------
-    def update_kickoff(self):
-        game = self.game_set.filter(tm__isnull=False).order_by('dt', 'tm')[0]
-        self.kickoff = datetime.combine(game.dt, game.tm)
-        self.save()
-
-    #---------------------------------------------------------------------------
-    @property
-    def is_open(self):
-        return datetime_now() < self.kickoff
-
-
-#===============================================================================
-class Attr(object):
-    
-    #---------------------------------------------------------------------------
-    def __init__(self, **kws):
-        self.__dict__.update(kws)
-    
-    #---------------------------------------------------------------------------
-    def __getitem__(self, key):
-        return getattr(self, key)
-
-
-#===============================================================================
-class GameManager(models.Manager):
-    
-    #---------------------------------------------------------------------------
-    def top_25_info(self, week, pick_ids):
-        is_bowl_week = week.week == 16
-        ranking      = list(week.ranking_set.all())
-        game_details = []
-        ranking_dict = dict([(r.team.id, r.rank) for r in ranking])
-        team_ids     = ranking_dict.keys()
-        bonus_ids    = set([])
-        now          = datetime_now()
-        master_ids   = Picks.objects.results(week).team_ids
-        games        = self.filter(week=week)
-        
-        if not is_bowl_week:
-            games = games.filter(models.Q(home__in=team_ids) | models.Q(away__in=team_ids))
-
-        
-        playing = []
-        games = list(games)
-        print '***', len(games)
-        for game in games:
-            if game.home.id in ranking_dict:
-                playing.append((ranking_dict[game.home.id], game.home.id))
-
-            if game.away.id in ranking_dict:
-                playing.append((ranking_dict[game.away.id], game.away.id))
-        
-        highest_ranked = sorted(playing, cmp=lambda a,b: cmp(a[0], b[0]))[0][1]
-        
-        for game in games:
-            home_rank = ranking_dict.get(game.home.id, None)
-            away_rank = ranking_dict.get(game.away.id, None)
-            
-            if away_rank <= 10 and not home_rank:
-                bonus_ids.add(game.home.id)
-                
-            if home_rank <= 10 and not away_rank:
-                bonus_ids.add(game.away.id)
-            
-            
-            game_details.append(Attr(
-                is_correct=(
-                    (game.home.id in master_ids and game.home.id in pick_ids) or
-                    (game.away.id in master_ids and game.away.id in pick_ids)
-                ),
-                home=Attr(team=game.home, rank=home_rank, is_picked=game.home.id in pick_ids),
-                away=Attr(team=game.away, rank=away_rank, is_picked=game.away.id in pick_ids),
-                date=game.dt,
-                time=game.tm,
-                highest_ranked=game.home.id == highest_ranked or game.away.id == highest_ranked,
-                is_open=now < game.kickoff,
-                id='%05d' % game.id
-            ))
-            
-        return Attr(games=game_details, ranking=ranking, bonus_ids=bonus_ids)
-
-
-#===============================================================================
-class Game(models.Model):
-    
-    #===========================================================================
-    class Result(ChoiceEnumeration):
-        HOME = ChoiceEnumeration.Option('HOME', 'Home team won')
-        AWAY = ChoiceEnumeration.Option('AWAY', 'Away team won')
-        TIE  = ChoiceEnumeration.Option('TIE',  'Tie')
-        TBP  = ChoiceEnumeration.Option('TBP',  'To be played', default=True)
-    
-    home   = models.ForeignKey(Team, related_name='home_game_set')
-    away   = models.ForeignKey(Team, related_name='away_game_set')
-    week   = models.ForeignKey(Week)
-    dt     = models.DateField()
-    tm     = models.TimeField(blank=True, null=True)
-
-    status = models.CharField(
-        max_length=4, 
-        choices=Result.CHOICES, 
-        default=Result.DEFAULT
-    )
-    
-    objects = GameManager()
-    
-    #===========================================================================
-    class Meta:
-        ordering = ('dt', 'tm')
-
-    #---------------------------------------------------------------------------
-    def __unicode__(self):
-        return '%s @ %s' % (self.away, self.home)
-        
-    #---------------------------------------------------------------------------
-    @property
-    def kickoff(self):
-        return datetime.combine(self.dt, self.tm or time(12))
-        
-
-#===============================================================================
-class RankingManager(models.Manager):
-
-    #---------------------------------------------------------------------------
-    def parse_espn(self, text):
-        '''\
-        1\tUSC (25)\t1-0\t1445
-        2\tAlabama (17)\t0-0\t1411
-        3\tLSU (16)\t2-0-1\t1402
-        4\tOklahoma (1)\t0-0\t1286
-        5\tOregon\t0-0\t1274
-        6\tGeorgia\t0-0\t1107
-        7\tFlorida State\t0-0\t1093
-        8\tMichigan (1)\t0-0\t1000
-        9\tSouth Carolina\t0-0\t994
-        10\tArkansas\t0-0\t963
-        11\tWest Virginia\t0-0\t856
-        12\tWisconsin\t0-0\t838
-        13\tMichigan State\t0-0\t742
-        14\tClemson\t0-0\t615
-        15\tTexas\t0-0\t569
-        16\tVirginia Tech\t0-0\t548
-        17\tNebraska\t0-0\t485
-        18\tOhio State\t0-0\t474
-        19\tOklahoma State\t0-0\t430
-        20\tTCU\t0-0\t397
-        21\tStanford\t0-0\t383
-        22\tKansas State\t0-0\t300
-        23\tFlorida\t0-0\t214
-        24\tBoise State\t0-0\t212
-        25\tLouisville\t0-0\t105'''
-        items = []
-        for line in text.splitlines():
-            rank, team, record, points = line.split('\t')
-            paren = team.find('(')
-            if paren > -1:
-                team = team[:paren].strip()
-                
-            items.append((
-                int(rank),
-                Team.objects.find(team),
-                record
-            ))
-
-        return items
-
-    #---------------------------------------------------------------------------
-    def new_ranking(self, entries, week_no, poll):
-        return [
-            self.create(
-                team=team,
-                rank=rank,
-                week=Week.objects.get(week=week_no),
-                record=record,
-                poll=poll
-            )
-            for rank, team, record in entries
-        ]
-
-    #---------------------------------------------------------------------------
-    def new_ranking_from_text(self, text, week_no, poll):
-        self.new_ranking(self.parse_espn(text), week_no, poll)
-    
-    #---------------------------------------------------------------------------
-    def for_week(week):
-        return self.filter(week=week)
-
-
-#===============================================================================
-class Ranking(models.Model):
-    poll = models.CharField(max_length=8)
-    team = models.ForeignKey(Team)
-    rank = models.IntegerField()
-    week = models.ForeignKey(Week)
-    record = models.CharField(max_length=8)
-    
-    objects = RankingManager()
-    
-    #===========================================================================
-    class Meta:
-        ordering = ('rank',)
-    
-    #---------------------------------------------------------------------------
-    def __unicode__(self):
-        return '%d %s W%d' % (self.rank, self.team, self.week.week)
-
-    #---------------------------------------------------------------------------
-    def top_10(self, week):
-        return [r.team for r in self.filter(week=week, rank__lte=10).select_related()]
-
-
-
-#===============================================================================
-class PicksMangager(models.Manager):
-    
-    #---------------------------------------------------------------------------
-    def fetch(self, user, week):
-        picks, _ = self.get_or_create(user=user, week=week)
-        return picks
-        
-    #---------------------------------------------------------------------------
-    def results(self, week):
-        results, created = self.get_or_create(user=admin_user, week=week)
-        return results
-
-    #---------------------------------------------------------------------------
-    def user_picks(self, week):
-        return self.filter(week=week).exclude(user__id=ADMIN_ID)
-        
-    #---------------------------------------------------------------------------
-    def update_scores(self, master):
-        master_ids = master.team_ids
-        info       = Game.objects.top_25_info(master.week, master_ids)
-        bonus_ids  = master_ids & info.bonus_ids
-        for pick in self.user_picks(master.week):
-            team_ids   = pick.team_ids
-            pick.score = len(master_ids & team_ids)
-            pick.score += len(bonus_ids & team_ids)
-            pick.save()
-            
-    #---------------------------------------------------------------------------
-    def standings(self, week):
-        results = self.results(week)
-        picks = self.user_picks(week)
-        
-    
-
-#===============================================================================
-class Picks(models.Model):
-    user    = models.ForeignKey(User)
-    week    = models.ForeignKey(Week)
-    picks   = models.CharField(blank=True, max_length=200)
-    score   = models.IntegerField(default=0)
-    points  = models.IntegerField(default=0)
-    created = models.DateTimeField(auto_now_add=True)
-    updated = models.DateTimeField(auto_now=True)
-    
-    objects = PicksMangager()
-    
-    #===========================================================================
-    class Meta:
-        ordering = ('-score',)
-    
-    #---------------------------------------------------------------------------
-    def set_picks(self, items, points=0):
-        self.picks = ','.join(items)
-        self.points = points
-        self.save()
-        if self.user.id == ADMIN_ID:
-            Picks.objects.update_scores(self)
-        
-    #---------------------------------------------------------------------------
-    @property
-    def team_ids(self):
-        return set([int(t, 10) for t in self.picks.split(',') if t])
-        
-    #---------------------------------------------------------------------------
-    @property
-    def teams(self):
-        return Team.objects.filter(id__in=self.team_ids)
-        
-
-################################################################################
-# NFL
-################################################################################
-
-
-#===============================================================================
 class GameManager(models.Manager):
 
     use_for_related_fields = True
 def sorted_standings(items):
     weighted = []
     prev_place, prev_results = 1, (0,0)
-    for i, item in itertools.izip(itertools.count(1), sorted(items)): 
+    for i, item in enumerate(sorted(items), 1): 
         results = (item.correct, item.points_delta)
         place = prev_place if results == prev_results else i
         prev_place, prev_results = place, results

File templates/football/ncaa/results.html

     <h2>Results</h2>{% load accounts %}
     <ul class="weeks">
         <li>Previous weeks:</li>{% for week in weeks %}
-        <li><a href="{% url football-results week.week%}">{{ week.week }}</a></li>
+        <li><a href="{% url football-results week.week %}">{{ week.week }}</a></li>
     {% endfor %}</ul>
     <table class="listing" id="results"> 
         <thead>
             <td>
                 <img class="gravatar" src="{{ pick.user.email|gravatar:"32" }}" />
                 {% if user.is_superuser %}
-                <a href="./user/{{ pick.user.username }}/">{{ pick.user }}</a>
+                <a href="./{{ week.week }}/user/{{ pick.user.username }}/">{{ pick.user }}</a>
                 {% else %}{{ pick.user }}
                 {% endif %}
             </td>