Browse files

Fix voting form postback

Radio buttons are no longer pre-selected after a successful vote.
  • Loading branch information...
1 parent e1b6d2a commit 100a4dea459025f1d9944bacd831d059f8869616 @georgevreilly committed Feb 2, 2013
Showing with 15 additions and 11 deletions.
  1. +12 −10 gae_flask_app/AdYouLation/handlers.py
  2. +3 −1 gae_flask_app/templates/vote.html
View
22 gae_flask_app/AdYouLation/handlers.py
@@ -1,5 +1,5 @@
from ..AdYouLation import AdYouLation
-from flask import render_template, current_app
+from flask import render_template, current_app, url_for, redirect
from flask.ext.wtf import Form, TextField, RadioField, Required, Email
from gaesessions import get_current_session
import random
@@ -11,13 +11,17 @@ def create_playlist():
random.shuffle(keys)
return keys
-def update_playlist():
+def get_choices_remaining(index=2):
session = get_current_session()
playlist = session.get('playlist')
if not playlist:
playlist = create_playlist()
- choices = playlist[:2]
- session['playlist'] = remaining = playlist[2:]
+ return playlist[:2], playlist[2:]
+
+def update_playlist():
+ (choices, remaining) = get_choices_remaining()
+ session = get_current_session()
+ session['playlist'] = remaining
return (choices, remaining)
@AdYouLation.route('/start')
@@ -41,11 +45,9 @@ class VoteForm(Form):
@AdYouLation.route('/vote', methods=('GET', 'POST'))
def vote():
- choices, remaining = update_playlist()
+ choices, remaining = get_choices_remaining()
form = vote_form(choices)
- logging.info(dir(form))
if form.validate_on_submit():
- flash("Success")
- return redirect(url_for("start"))
- else:
- return render_template("vote.html", form=form)
+ update_playlist()
+ return redirect(url_for(".vote"))
+ return render_template("vote.html", form=form, remaining=remaining)
View
4 gae_flask_app/templates/vote.html
@@ -2,7 +2,8 @@
{% block body %}
<h1>Vote</h1>
{% from "forms/macros.html" import render_field, render_video %}
- <form method="POST" action="." class="form">
+ <form method="POST" action="{{ url_for(".vote") }}" class="form">
+ {{ form.csrf_token }}
<table>
<tr>
<td class="video-choice">{{ render_video(form.video_1) }}</td>
@@ -11,4 +12,5 @@
<table>
<input type="submit" value="Vote" class="button green">
</form>
+ <p>{{ remaining }}</p>
{% endblock %}

0 comments on commit 100a4de

Please sign in to comment.