-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvote.py
34 lines (24 loc) · 1.01 KB
/
vote.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import webapp2
from google.appengine.ext import ndb
from google.appengine.api import users
from models.content import Article
from models.vote import Vote
from models.vote import UPVOTE, DOWNVOTE
from models.auth import JedditUser
class AddVoteHandler(webapp2.RequestHandler):
def post(self, article_id, vote_type):
article = Article.get_by_id(int(article_id))
user = users.get_current_user()
if user:
user_key = JedditUser.key_from_user(user)
# TODO Votes are now being created properly, Add update requests to a pull queue
if vote_type == 'down':
vote = Vote.create(article_key=article.key, user_key=user_key, value=DOWNVOTE)
article.rating = article.rating - 1.0
else:
vote = Vote.create(article_key=article.key, user_key=user_key, value=UPVOTE)
article.rating = article.rating + 1.0
ndb.put_multi([article, vote])
return self.redirect('/', body="Thanks for your vote!")
def get(self, article_id, vote_type):
return self.post(article_id, vote_type)