From 559ee8b304b9cbee43cd0295befe5c7c74f91df1 Mon Sep 17 00:00:00 2001 From: James Turk Date: Mon, 28 May 2012 19:34:38 -0400 Subject: [PATCH] add a lot of stuff for edit_event --- fowl/game/models.py | 10 +++- fowl/game/templates/edit_event.html | 84 ++++++++++++++++------------- fowl/game/tests.py | 24 ++++++++- fowl/game/urls.py | 3 +- fowl/game/views.py | 44 ++++++++++++--- 5 files changed, 116 insertions(+), 49 deletions(-) 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 %} -
+ - + - + + {% for match in event.matches %}
-

Match #1

+

Match #{{forloop.counter}}

- - - - - - - - + {% for team in match.teams %} - - - - - - - - - - - - - - - - + + {% for member in team %} + + {% endfor %} + {% endfor %}
 member #1member #2member #3
team #1
team #2
team #3team #{{forloop.counter}} +
- - - - + {% for val, name in OUTCOMES %} + + {% endfor %} + + + + +
+ + +
- + {% endfor %} + + /match clean -->
- + {% csrf_token %} +
{% 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 8fabf2c..de06b97 100644 --- a/fowl/game/urls.py +++ b/fowl/game/urls.py @@ -5,6 +5,7 @@ urlpatterns = patterns('', name='events'), url(r'league/(?P\d+)/$', 'fowl.game.views.league', name='league'), - url(r'^edit_event/$', 'fowl.game.views.edit_event', name='edit_event'), + url(r'^edit_event/(?P\d+|new)/$', 'fowl.game.views.edit_event', + name='edit_event'), url(r'^roster/$', 'fowl.game.views.roster', name='roster'), ) diff --git a/fowl/game/views.py b/fowl/game/views.py index fec29e9..b75aa9c 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 fowl.game.models import Team, TeamPoints, Star, Event +from fowl.game.models import Team, TeamPoints, Star, Event, OUTCOMES, TITLES def events(request, league_id): @@ -21,14 +22,43 @@ def events(request, league_id): return render(request, "events.html", {'events': events, 'view': 'events'}) -def edit_event(request, event_id=None): - if event_id: - event = get_object_or_404(Event, pk=event_id) - else: +def edit_event(request, event): + if event == 'new': event = None - if request.method == 'GET': - return render(request, "edit_event.html", {"event": event}) + else: + event = get_object_or_404(Event, pk=event).to_dict() + if request.method == 'POST': + edict = {} + edict['id'] = request.POST.get('id') + edict['name'] = request.POST.get('name') + edict['date'] = request.POST.get('date') + edict['matches'] = [] + + outcomes = request.POST.getlist('outcome') + winners = request.POST.getlist('winner') + titles = request.POST.getlist('title') + notes = request.POST.getlist('notes') + for i, note in enumerate(notes): + edict['matches'].append({'outcome': outcomes[i], + 'winner': winners[i], + 'title_at_stake': titles[i], + 'notes': notes[i], + 'teams': [], + }) + + for k,v in request.POST.iterlists(): + if k.startswith('members'): + _, match, team = k.split('-') + edict['matches'][int(match)-1]['teams'].append(v) + + event = Event.from_dict(edict) + + return render(request, "edit_event.html", + {'event': event, + 'OUTCOMES': OUTCOMES, + 'TITLES': TITLES} + ) def league(request, league_id): context = {