This repository has been archived by the owner on Jun 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
special_controllers.py
73 lines (57 loc) · 1.89 KB
/
special_controllers.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""
Controller classes for special functions.
Currently only includes "likes" and the 404 handler.
Exports classes:
Like
Unlike
NotFound
Redirects all unknown URLs to "/"
"""
import models
import os
from blog_controllers import Handler
from google.appengine.ext import db
class Like(Handler):
""" Adds logged-in user's name to the likes list for a given post """
def get(self):
self.redirect("/")
def post(self):
message = ""
if not self.validate_cookie():
self.redirect("/")
return
username = self.request.cookies.get("user")
post_id = self.request.get("post_id")
post = models.Post.get_by_id(int(post_id))
if username == post.author:
message = ("You can't like your own posts."
"This is a bug. Please notify admin.")
else:
post.likes.append(username)
post.put()
message = ("Success! <a href='/p/" + post_id + "'>Return</a>")
self.write(message)
class Unlike(Handler):
""" Removes logged-in user's name from the post likes list """
def get(self):
self.redirect("/")
def post(self):
message = ""
if not self.validate_cookie():
self.redirect("/")
return
username = self.request.cookies.get("user")
post_id = self.request.get("post_id")
post = models.Post.get_by_id(int(post_id))
if username not in post.likes:
message = ("! The site doesn't think you can unlike this."
"This is a bug. Please notify admin.")
else:
post.likes.remove(username)
post.put()
message = ("Success! <a href='/p/" + post_id + "'>Return</a>")
self.write(message)
class NotFound(Handler):
""" Catch all invalid URLs and redirect to index """
def get(self):
self.redirect("/")