Exemple #1
0
  def post(self):
    thing = db.get(self.request.get("thingToDoKey"))
    query = Vote.gql("WHERE user = :1 AND thingToDo = :2",
      users.get_current_user(), thing)
    if (query.get()):
      # this user already voted on this thing; display an error
      # App Engine question:
      # Any way to do this validation client-side?
      self.redirect("/main?gameKey=" + str(self.request.get('gameKey')) +
        "&message=You've already voted on " + thing.name + ".")
      return
    try:
      score = int(self.request.get("score"))
      if (score < 1 or score > 5):
        raise ValueError, "Vote must be between 1 and 5"
      # if it can't parse your vote, it will also raise ValueError
    except ValueError:
      self.redirect("/main?gameKey=" + str(self.request.get('gameKey')) +
        "&message=Vote must be between 1 and 5")
      return

    vote = Vote(user = users.get_current_user(), thingToDo = thing,
      score = score)
    vote.put()
    message = "Recorded your vote of " + str(score) + " for " + thing.name
    self.redirect("/main?gameKey=" + str(self.request.get('gameKey')) +
      "&message=" + message)
Exemple #2
0
 def get(self):
   current_game = db.get(self.request.get("gameKey"))
   scores = {}
   for thing in ThingToDo.all().filter("game = ", current_game):
     # App Engine question:
     # I use GQL sometimes and .filter() etc sometimes.  They're both
     # pretty intuitive to me.  Is either way faster or otherwise preferred?
     allVotes = Vote.gql("WHERE thingToDo = :1", thing)
     scores[thing] = 0
     for vote in allVotes.fetch(limit = 1000): # 1000 is the max limit.
     # App Engine question:
     # is there any way to say "give me everything in the data store, I don't
     # care how long it takes?"  The 1000 limit scares me; what if 1001 people
     # are voting on the same thing?  This will just miss 1 vote, silently.
       scores[thing] += vote.score
   self.response.out.write("Here are the totals:")
   for (thing, score) in scores.items():
     self.response.out.write("<br/>" + thing.name + " - " + str(score))