diff --git a/fowl/game/models.py b/fowl/game/models.py index fa76a4b..90f6ef6 100644 --- a/fowl/game/models.py +++ b/fowl/game/models.py @@ -50,14 +50,20 @@ class Event(models.Model): date = models.DateField() def to_dict(self): - d = {'name': self.name, 'date': self.date, + d = {'id': self.id, 'name': self.name, 'date': self.date, 'matches': [m.to_dict() for m in self.matches.all()] } return d @staticmethod def from_dict(d): - event = Event.objects.create(name=d['name'], date=d['date']) + if d.get('id'): + event = Event.objects.get(pk=d['id']) + event.name = d['name'] + event.date = d['date'] + event.matches.all().delete() + else: + event = Event.objects.create(name=d['name'], date=d['date']) for match in d['matches']: event.add_match(*match['teams'], winner=match['winner'], diff --git a/fowl/game/templates/edit_event.html b/fowl/game/templates/edit_event.html index 29ab47d..84131af 100644 --- a/fowl/game/templates/edit_event.html +++ b/fowl/game/templates/edit_event.html @@ -1,56 +1,63 @@ {% extends "base.html" %} {% block content %} -
{% endblock %} diff --git a/fowl/game/tests.py b/fowl/game/tests.py index ce38ddf..42e8f1c 100644 --- a/fowl/game/tests.py +++ b/fowl/game/tests.py @@ -45,7 +45,7 @@ class EventTest(TestCase): event.add_match('jimross', 'jerrylawler', winner='jimross', outcome='submission') event = Event.objects.get() - expected = {'name': 'RAW', 'date': datetime.date(2012,1,1), + expected = {'id': 1, 'name': 'RAW', 'date': datetime.date(2012,1,1), 'matches': [ {'teams': [[u'jimross'],[u'jerrylawler']], 'winner': u'jimross', @@ -80,6 +80,28 @@ class EventTest(TestCase): self.assertEqual(match_one.notes, '') self.assertEqual(match_one.title_at_stake, None) + # test updating a dict + edict = {'id': 1, 'name': 'Smackdown', 'date': datetime.date(2013,1,1), + 'matches': [ + {'teams': [[u'jimross'],[u'jerrylawler']], + 'winner': u'jimross', + 'outcome': u'submission', + 'notes': '', + 'title_at_stake': None, + }, + {'teams': [[u'michaelcole']], + 'outcome': u'appearance', + 'winner': None, + 'notes': '', + 'title_at_stake': None, + } + ] + } + event = Event.from_dict(edict) + self.assertEqual(event.name, 'Smackdown') + self.assertEqual(event.date, datetime.date(2013,1,1)) + self.assertEqual(event.matches.count(), 2) + class MatchTest(TestCase): fixtures = ['testdata'] diff --git a/fowl/game/urls.py b/fowl/game/urls.py index 5846039..554a911 100644 --- a/fowl/game/urls.py +++ b/fowl/game/urls.py @@ -5,6 +5,8 @@ urlpatterns = patterns('', name='events'), url(r'league/(?P