-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
51 lines (35 loc) · 1.38 KB
/
main.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
import webapp2
from google.appengine.ext import ndb
from handler import Handler
from comment import Comment
DEFAULT_KEY = ndb.Key('KEY', "DEFAULT_KEY")
class PageHandler(Handler):
# Status for the most recently added comment to be valid or invalid
invalid = False
def get(self):
# Get all the comments, sorted by date
comment_query = Comment.query(ancestor=DEFAULT_KEY).order(-Comment.date)
comments = comment_query.fetch()
self.render("main.html", comments=comments, invalid=PageHandler.invalid)
class CommentHandler(Handler):
def post(self):
message = self.request.get("message")
name = self.request.get("name")
# Check if message or name is invalid
if len(name.strip()) == 0 or len(message.strip()) == 0:
PageHandler.invalid = True
else:
PageHandler.invalid = False
# create a new comment object
# set a message and name to it
new_comment = Comment(parent=DEFAULT_KEY)
new_comment.message = message
new_comment.name = name
# Save in the data-store
new_comment.put()
self.redirect("/#comments")
# https://webapp-improved.appspot.com/guide/routing.html
app = webapp2.WSGIApplication([
("/add_comment", CommentHandler),
("/", PageHandler)
], debug=True)