On Jan 17, 11:39 pm, Robert Kluin <[email protected]> wrote:

> You sort-of offer the solution yourself, you need to store the fact
> that a user has voted for a specific article.  If they try to vote
> again, tell them 'no.'

Thanks. I changed the models like this

class SiteUser(db.Model):
    site_user = db.UserProperty()
    total_votes = db.IntegerProperty(default=1)
    liked_items = db.StringProperty()

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

and the VoteHandler like this (also available in pastebin
http://pastebin.com/Q1Y6vzSz):

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)))

            #--check if user voted on this item before--#
            query  = SiteUser.all()
            query.filter("liked_items =", (str(item.key().id())))
            already_voted = query.get()

            if already_voted:
                self.redirect("/onevote")
            else:
                query = SiteUser.all()
                query.filter("site_user =", users.get_current_user())

                data = query.get()

                data.total_votes += 1
                data.liked_items = str(item.key().id())
                data.site_user = users.get_current_user()

                db.put(data)

                item.points += 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("""<div id='Small'>%s</div>""" %
greeting)

And this is the /onevote page: http://sarah-for-president.appspot.com/onevote

Not sure if this is the right way to handle this. Any suggestions?

-- 
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