我试图让这个功能限制用户每张图片只能投一票。但是,它目前允许所有投票通过。如果我将“if existing_vote != 0:”更改为“if existing_vote == 0:”,它不会让任何投票通过。想法?
类投票处理程序(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('/', { })
对于模型:
类用户(db.Model):
帐户 = db.StringProperty()
密码 = db.StringProperty()
名称 = db.StringProperty()
创建 = db.DateTimeProperty(auto_now=True)
类图像(db.Model):
用户 = db.ReferenceProperty(用户)
photo_key = db.BlobProperty()
网站 = db.StringProperty()
文本 = db.StringProperty()
创建 = db.DateTimeProperty(auto_now=True)
票= db.IntegerProperty(默认= 1)
类投票(db.Model):
user = db.ReferenceProperty(User) #查看是否在本站投票
photo = db.ReferenceProperty(Image) #对正确的 URL 应用投票
upvote = db.IntegerProperty(default=1)
创建 = db.DateTimeProperty(auto_now=True)