Skip to content

Commit

Permalink
Record Votes
Browse files Browse the repository at this point in the history
  • Loading branch information
georgevreilly committed Feb 3, 2013
1 parent 21d723a commit fae0530
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions gae_flask_app/AdYouLation/handlers.py
Expand Up @@ -53,26 +53,39 @@ def start():
session['id'] = uuid.uuid4()
return render_template("start.html")

class VideoVote(RadioField):
CHOICES = (("UP", "Yes!"), ("down", "No!"))
class VideoChoice(RadioField):
CHOICES = (("up", "Yes!"), ("down", "No!"))

def vote_form(videos):
class VoteForm(Form):
pass

for index, video in enumerate(videos):
name = "video_{}".format(index+1)
field = VideoVote(video, validators=[Required()], choices=VideoVote.CHOICES)
field = VideoChoice(video, validators=[Required()], choices=VideoChoice.CHOICES)
setattr(VoteForm, name, field)

return VoteForm()

def record_vote(form):
for index in [1, 2]:
attrname = "video_{}".format(index)
name = getattr(form, attrname).label.text
data = getattr(form, attrname).data
vv = VideoVotes.gql("WHERE name = :1", name).get()
if data == "up":
vv.up_votes += 1
else:
vv.down_votes += 1
vv.put()

@AdYouLation.route('/vote', methods=('GET', 'POST'))
def vote():
choices, remaining = get_choices_remaining()
id = get_current_session()['id']
form = vote_form(choices)
if form.validate_on_submit():
update_playlist()
record_vote(form)
return redirect(url_for(".vote"))
return render_template("vote.html", form=form, remaining=remaining, id=id)

0 comments on commit fae0530

Please sign in to comment.