I am trying to create a vote function that increases the class URL.votes +1 when clicked. This is a two part question:
How do you pull the entity key? (I think you need the key to distinguish which vote property is being modified?)
How do you then write the 'a href' for the link to perform the vote?
Thanks!
Models:
class URL(db.Model):
user = db.ReferenceProperty(User)
website = 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
url = db.ReferenceProperty(URL) #To apply vote to right URL
upvote = db.IntegerProperty(default=1)
created = db.DateTimeProperty(auto_now=True)
Controller
class VoteHandler(webapp.RequestHandler):
def get(self):
doRender(self, 'base/index.html')
def post(self):
#See if logged in
self.Session = Session()
if not 'userkey' in self.Session:
doRender(
self,
'/',
{'error' : 'Please login to vote'})
return
#Get current vote total
url = db.URL.get() #pull current site. This is where I think I need the help
url.votes += 1 #pull current site vote total & add 1
url.put();
logging.info('Adding a vote'+nurl)
#Create a new Vote object
newvote = models.Vote(user=self.Session['userkey'], url=url)
newvote.put();
self.get();
self.redirect('/', { })
View
a href="/vote" {{ url.votes }} votes - {{ url.website }}