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
/
comment_controller.py
139 lines (121 loc) · 5.02 KB
/
comment_controller.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"""
Controller classes for fetching, creating, and rendering blog comments.
Exports classes:
PostComment
EditComment
DeleteComment
"""
# Auto formatted with Sublime Text plugin
import models
import os
from blog_controllers import Handler
from google.appengine.ext import db
class PostComment(Handler):
""" Handles submission to "/newcomment" and posts to db """
def get(self):
self.redirect("/")
def post(self):
message = ""
if not self.validate_cookie():
self.redirect("/login")
else:
username = self.request.cookies.get("user")
text = self.request.get("content")
# Only post comments if the text field is not empty
if text:
js = self.request.get("js")
text = self.strip_tags(text)
# Format text for appropriate display
# Remove all tags and then replace newlines with <br /> tags
text = text.replace('\n', '<br />')
post_id = self.request.get("parent")
post = models.Post.get_by_id(int(post_id))
new_comment = models.Comment(parent=post,
author=username,
text=text)
new_comment.put()
post.comment_count += 1
post.put()
# Retrieve id of new comment for AJAX function
q = db.Query(models.Comment).ancestor(post)
comment_id = q.run(limit=1).next().key().id()
# If client is using JavaScript, return the id
if js:
message = str(comment_id) + ' ' + username
# If no JavaScript on client, return a human readable message
else:
message = ("Success! Comment posted. <a href='p/" +
post_id + "'>Return to the post.</a>")
else:
message = ("! Comment field was empty. <a href='p/" +
post_id + "'>Go back and try again.</a>")
self.write(message)
class EditComment(Handler):
""" Handles submission to "/editcomment" and updates db
Renders comment edit form if necessary
Only expect get requests from a browser with no JavaScript
"""
def get(self):
if not self.validate_cookie():
self.redirect("/")
return
comment_id = self.request.get("comment_id")
parent_id = self.request.get("parent")
parent = models.Post.get_by_id(int(parent_id))
comment = models.Comment.get_by_id(int(comment_id), parent=parent)
# Reformat text from page display to textarea display
text = comment.text.replace('<br />', '\n')
self.render('editcomment.html',
id=comment_id,
parent=parent_id,
text=comment.text)
def post(self):
if not self.validate_cookie():
self.redirect("/")
return
username = self.request.cookies.get("user")
text = self.request.get("content")
# Format text for appropriate display
# Remove all tags and then replace newlines with <br /> tags
text = self.strip_tags(text)
text = text.replace('\n', '<br />')
comment_id = self.request.get("comment_id")
parent_id = self.request.get("parent")
parent = models.Post.get_by_id(int(parent_id))
comment = models.Comment.get_by_id(int(comment_id), parent=parent)
if username != comment.author:
message = ("! You can't edit someone else's comment. "
"This is a bug. Please report it to admin.")
else:
comment.text = text
comment.edited = True
comment.put()
message = ("Success! Comment edited. <a href='p/" +
parent_id + "'>Return to the post.</a>")
self.write(message)
class DeleteComment(Handler):
""" Deletes comments """
def get(self):
self.redirect("/")
def post(self):
if not self.validate_cookie():
self.redirect("/")
return
username = self.request.cookies.get("user")
comment_id = self.request.get("comment_id")
parent_id = self.request.get("parent")
parent = models.Post.get_by_id(int(parent_id))
comment = models.Comment.get_by_id(int(comment_id), parent=parent)
if username != comment.author:
message = ("! You can't delete someone else's comment. "
"This is a bug. Please report it to admin.")
elif parent and comment:
db.delete(comment)
parent.comment_count -= 1
parent.put()
message = ("Success! Comment deleted. <a href='p/" +
parent_id + "'>Return to the post.</a>")
else:
message = ("! Something went wrong." + comment_id + " "
"\n " + parent_id + "\n" + username)
self.write(message)