I am trying to have this function limit a user to only one vote per image. However it currently lets all votes through. If I change "if existing_vote != 0:" to "if existing_vote == 0:" it lets no votes through. Thoughts?
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
existing_vote = models.Vote.all().filter('user=', self.Session['userkey']).filter('photo=',vurl).count()
if existing_vote != 0:
self.redirect('/', { })
else:
newvote = models.Vote(user=self.Session['userkey'], url=vurl)
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('/', { })
For the Models:
class User(db.Model):
account = db.StringProperty()
password = db.StringProperty()
name = db.StringProperty()
created = db.DateTimeProperty(auto_now=True)
class Image(db.Model):
user = db.ReferenceProperty(User)
photo_key = db.BlobProperty()
website = db.StringProperty()
text = db.StringProperty()
created = db.DateTimeProperty(auto_now=True)
votes = db.IntegerProperty(default=1)
class Vote(db.Model):
user = db.ReferenceProperty(User) #See if voted on this site yet
photo = db.ReferenceProperty(Image) #To apply vote to right URL
upvote = db.IntegerProperty(default=1)
created = db.DateTimeProperty(auto_now=True)