refactor part 1

This commit is contained in:
James Turk 2012-05-27 19:58:39 -04:00
parent 0c83377415
commit e5731e334b
4 changed files with 133 additions and 86 deletions

18
fowl/game/admin.py Normal file
View File

@ -0,0 +1,18 @@
from django.contrib import admin
from .models import Star, Match, Event, League, Team
class StarAdmin(admin.ModelAdmin):
list_display = ('name', 'division', 'active')
list_filter = ('division', 'active')
admin.site.register(Star, StarAdmin)
admin.site.register(Match)
admin.site.register(Event)
class TeamAdmin(admin.ModelAdmin):
list_display = ('name', 'league')
list_filter = ('league',)
admin.site.register(League)
admin.site.register(Team, TeamAdmin)

View File

@ -1,11 +1,13 @@
from collections import defaultdict from collections import defaultdict
from django.db import models from django.db import models
from django.contrib import admin
from django.contrib.auth.models import User from django.contrib.auth.models import User
# these things are independent of the game
WIN_TYPES = (('pin', 'pin'), WIN_TYPES = (('pin', 'pin'),
('DQ', 'DQ'), ('DQ', 'DQ'),
('submission', 'submission')) ('submission', 'submission'))
TITLES = (('wwe', 'WWE'), TITLES = (('wwe', 'WWE'),
('heavyweight', 'Heavyweight'), ('heavyweight', 'Heavyweight'),
('ic', 'Intercontinental'), ('ic', 'Intercontinental'),
@ -19,9 +21,19 @@ class Star(models.Model):
name = models.CharField(max_length=200) name = models.CharField(max_length=200)
photo_url = models.URLField() photo_url = models.URLField()
division = models.CharField(max_length=100) division = models.CharField(max_length=100)
title = models.CharField(max_length=20, choices=TITLES) title = models.CharField(max_length=20, choices=TITLES, null=True)
active = models.BooleanField() active = models.BooleanField()
def win_title(self, title, tag_partner=None):
Star.objects.filter(title=title).update(title=None)
self.title = title
self.save()
if tag_partner:
if title != 'tag':
raise ValueError("can't have tag partner w/ non-tag belt")
tag_partner.title = title
tag_partner.save()
def drafted(self, league): def drafted(self, league):
return self.teams.filter(league=league).count() >= 1 return self.teams.filter(league=league).count() >= 1
@ -29,66 +41,6 @@ class Star(models.Model):
return self.name return self.name
class StarAdmin(admin.ModelAdmin):
list_display = ('name', 'division', 'active')
list_filter = ('division', 'active')
admin.site.register(Star, StarAdmin)
class League(models.Model):
name = models.CharField(max_length=100)
raw_picks = models.IntegerField(default=3)
smackdown_picks = models.IntegerField(default=3)
diva_picks = models.IntegerField(default=2)
wildcard_picks = models.IntegerField(default=1)
oldtimer_picks = models.IntegerField(default=2)
def score_event(self, event):
ppv_bonus = 1 if event.name.lower() not in ('raw', 'smackdown') else 0
TeamPoints.objects.filter(match__event=event).delete()
for match in event.matches.all():
for star, points in match.points().iteritems():
try:
team = self.teams.get(stars=star)
TeamPoints.objects.create(points=points + ppv_bonus,
team=team,
star_id=star,
match=match)
except Team.DoesNotExist:
pass
def __unicode__(self):
return self.name
admin.site.register(League)
class Team(models.Model):
name = models.CharField(max_length=100)
login = models.OneToOneField(User, related_name='team')
league = models.ForeignKey(League, related_name='teams')
stars = models.ManyToManyField(Star, related_name='teams')
def add_star(self, pk):
member = Star.objects.get(pk=pk)
if member.drafted(self.league):
raise ValueError('cannot add {0}, already drafted in {1}'.format(
member, self.league))
self.stars.add(member)
def drop_star(self, pk):
member = Star.objects.get(pk=pk)
self.stars.remove(member)
def __unicode__(self):
return self.name
class TeamAdmin(admin.ModelAdmin):
list_display = ('name', 'league')
list_filter = ('league',)
admin.site.register(Team, TeamAdmin)
class Event(models.Model): class Event(models.Model):
name = models.CharField(max_length=100) name = models.CharField(max_length=100)
date = models.DateField() date = models.DateField()
@ -124,7 +76,6 @@ class Event(models.Model):
def __unicode__(self): def __unicode__(self):
return '{0} {1}'.format(self.name, self.date) return '{0} {1}'.format(self.name, self.date)
admin.site.register(Event)
class Match(models.Model): class Match(models.Model):
event = models.ForeignKey(Event, related_name='matches') event = models.ForeignKey(Event, related_name='matches')
@ -220,8 +171,6 @@ class Match(models.Model):
ret += ' (no contest)' ret += ' (no contest)'
return ret return ret
admin.site.register(Match)
class MatchTeam(models.Model): class MatchTeam(models.Model):
members = models.ManyToManyField(Star) members = models.ManyToManyField(Star)
match = models.ForeignKey(Match, related_name='teams') match = models.ForeignKey(Match, related_name='teams')
@ -236,6 +185,56 @@ class MatchTeam(models.Model):
ret += ' (v)' ret += ' (v)'
return ret return ret
# fantasy stuff
class League(models.Model):
name = models.CharField(max_length=100)
raw_picks = models.IntegerField(default=3)
smackdown_picks = models.IntegerField(default=3)
diva_picks = models.IntegerField(default=2)
wildcard_picks = models.IntegerField(default=1)
oldtimer_picks = models.IntegerField(default=2)
def score_event(self, event):
ppv_bonus = 1 if event.name.lower() not in ('raw', 'smackdown') else 0
TeamPoints.objects.filter(match__event=event).delete()
for match in event.matches.all():
for star, points in match.points().iteritems():
try:
team = self.teams.get(stars=star)
TeamPoints.objects.create(points=points + ppv_bonus,
team=team,
star_id=star,
match=match)
except Team.DoesNotExist:
pass
def __unicode__(self):
return self.name
class Team(models.Model):
name = models.CharField(max_length=100)
login = models.OneToOneField(User, related_name='team')
league = models.ForeignKey(League, related_name='teams')
stars = models.ManyToManyField(Star, related_name='teams')
def add_star(self, pk):
member = Star.objects.get(pk=pk)
if member.drafted(self.league):
raise ValueError('cannot add {0}, already drafted in {1}'.format(
member, self.league))
self.stars.add(member)
def drop_star(self, pk):
member = Star.objects.get(pk=pk)
self.stars.remove(member)
def __unicode__(self):
return self.name
class TeamPoints(models.Model): class TeamPoints(models.Model):
points = models.IntegerField() points = models.IntegerField()
team = models.ForeignKey(Team, related_name='points') team = models.ForeignKey(Team, related_name='points')

View File

@ -4,7 +4,33 @@ from django.contrib.auth.models import User
from .models import Match, Event, League, Team, TeamPoints, Star from .models import Match, Event, League, Team, TeamPoints, Star
def _give_belt(star, belt): def _give_belt(star, belt):
Star.objects.filter(pk=star).update(title=belt) Star.objects.get(pk=star).win_title(belt)
class StarTest(TestCase):
def test_win_belt(self):
cmpunk = Star.objects.create(pk='cmpunk', name='CM Punk', title='wwe')
dbry = Star.objects.create(pk='danielbryan', name='Daniel Bryan')
kofi = Star.objects.create(pk='kofi', name='Kofi Kingston',
title='tag')
rtruth = Star.objects.create(pk='rtruth', name='R Truth', title='tag')
swagger = Star.objects.create(pk='swagger', name='Jack Swagger')
ziggler = Star.objects.create(pk='ziggler', name='Dolph Ziggler')
# belt win takes it away from original holder
self.assertEqual(Star.objects.get(pk='cmpunk').title, 'wwe')
dbry.win_title('wwe')
self.assertEqual(Star.objects.get(pk='cmpunk').title, None)
self.assertEqual(Star.objects.get(pk='danielbryan').title, 'wwe')
# tag belt win
self.assertEqual(Star.objects.get(pk='kofi').title, 'tag')
self.assertEqual(Star.objects.get(pk='rtruth').title, 'tag')
ziggler.win_title('tag', tag_partner=swagger)
self.assertEqual(Star.objects.get(pk='kofi').title, None)
self.assertEqual(Star.objects.get(pk='rtruth').title, None)
self.assertEqual(Star.objects.get(pk='ziggler').title, 'tag')
self.assertEqual(Star.objects.get(pk='swagger').title, 'tag')
class MatchTest(TestCase): class MatchTest(TestCase):
fixtures = ['testdata'] fixtures = ['testdata']
@ -14,14 +40,14 @@ class MatchTest(TestCase):
def test_basics(self): def test_basics(self):
match = self.event.add_match('tripleh', 'undertaker') match = self.event.add_match('tripleh', 'undertaker')
self.assertEqual(unicode(match), 'Triple H vs. Undertaker') self.assertEqual(unicode(match),
'Triple H vs. Undertaker (no contest)')
match.record_win('undertaker', 'pin') match.record_win('undertaker', 'pin')
self.assertEqual(unicode(match), 'Triple H vs. Undertaker (v)') self.assertEqual(unicode(match), 'Triple H vs. Undertaker (v)')
_give_belt('cmpunk', 'wwe') _give_belt('cmpunk', 'wwe')
match = self.event.add_match('cmpunk', 'reymysterio') match = self.event.add_match('cmpunk', 'reymysterio', winner='cmpunk',
self.assertEqual(unicode(match), 'CM Punk (c) vs. Rey Mysterio') win_type='pin')
match.record_win('cmpunk', 'pin')
self.assertEqual(unicode(match), 'CM Punk (c) (v) vs. Rey Mysterio') self.assertEqual(unicode(match), 'CM Punk (c) (v) vs. Rey Mysterio')
@ -224,6 +250,9 @@ class LeagueTest(TestCase):
# rename the event and rescore # rename the event and rescore
event.name = 'Wrestlemania' event.name = 'Wrestlemania'
event.save() event.save()
# give cody rhodes his belt back for this one
_give_belt('codyrhodes', 'ic')
self.league.score_event(event) self.league.score_event(event)
# all should be one higher than before # all should be one higher than before
self.assertEqual(TeamPoints.objects.get(team=self.teddy, star__pk='reymysterio').points, 1) self.assertEqual(TeamPoints.objects.get(team=self.teddy, star__pk='reymysterio').points, 1)

View File

@ -1,3 +1,4 @@
Django Django
lxml lxml
scrapelib scrapelib
django-debug-toolbar