Authenticate the user, store their vote, if they vote a second time it
changes their vote.  Very easy.


-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Zeynel
Sent: Monday, January 17, 2011 12:43 PM
To: Google App Engine
Subject: [google-appengine] Stopping users from voting more than once

Hello,

I just finished a simple voting feature for my app:

class VoteHandler(webapp.RequestHandler):
    def get(self, id):
        id = int(id)
        item = Item.get_by_id(id)

        user = users.get_current_user()

        if user:
            greeting = ("%s (<a href='%s'>sign out</a>)" %
                           (user.nickname(),
users.create_logout_url(self.request.uri)))

            item.vote +=1
            item.put()

            if self.request.referrer == 'http://sarah-for-
president.appspot.com/newest':
                self.redirect('/newest')
            elif self.request.referrer == 'http://sarah-for-
president.appspot.com/hot':
                self.redirect('/hot')
            else:
                self.redirect("/")
        else:
            greeting = ("<a href='%s'>Sign in with your Google account or
register</a>." %
 
cgi.escape(users.create_login_url(self.request.uri)))


        self.response.out.write("""%s""" % greeting)

As you can see I only check if the voter is a user and add +1.

Now I want to add a feature so that the same user cannot vote for the same
article more than once.

What is the best way to handle this?

I checked the source of Hacker News view-source:http://
news.ycombinator.com/news and I see that they handle it with JavaScript.
They hide the arrows with something like

  // hide arrows
  byId('up_'   + item).style.visibility = 'hidden';
  byId('down_' + item).style.visibility = 'hidden';

This is my model:

class Item(db.Model):
    title = db.StringProperty()
    url = db.StringProperty()
    date = db.DateTimeProperty(auto_now_add=True)
    author = db.UserProperty()
    vote = db.IntegerProperty(default=1)

And this is the link <a href="/vote/%s"> ^ </a> with a caret (for up
arrow) that sends the request to VoteHandler.

I put the 2 relevant handlers in Pastebin:

VoteHandler: http://pastebin.com/0zCZqHia Hot (has the vote link):
http://pastebin.com/YFc5CwrW

Thanks for your advice.

--
You received this message because you are subscribed to the Google Groups
"Google App Engine" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-appengine?hl=en.


-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en.

Reply via email to