Skip to content

Instantly share code, notes, and snippets.

@emilepetrone
Created September 1, 2010 19:24
Show Gist options
  • Save emilepetrone/561200 to your computer and use it in GitHub Desktop.
Save emilepetrone/561200 to your computer and use it in GitHub Desktop.
class VoteHandler(webapp.RequestHandler):
def get(self):
#See if logged in
self.Session = Session()
if not 'userkey' in self.Session:
doRender(
self,
'base/index.html',
{'error' : 'Please login to vote'})
return
#If user hasn't voted - if user doesn't have a vote on that image object
key = self.request.get('photo_id')
vurl = models.Image.get_by_id(int(key))
#pull current site vote total & add 1
#check user here
vurl.votes += 1
vurl.put()
logging.info('Adding a vote')
#Create a new Vote object
newvote = models.Vote(user=self.Session['userkey'], url=vurl)
newvote.put()
self.redirect('/', { })
@mbrenig
Copy link

mbrenig commented Sep 1, 2010

Three improvements to consider:

  • Check for existing votes before adding a vote and updating the counter.
  • Using a transaction to update the vote count and add the Vote object keeps the datastore in sync.. in case there are errors.
  • If you need to process more than one vote (on a url) per second then sharding the counter will be required for scalability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment