-
Notifications
You must be signed in to change notification settings - Fork 0
/
blog.py
70 lines (53 loc) · 2.38 KB
/
blog.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
""" Main blog file """
import os
import webapp2
import jinja2
from handlers import *
from google.appengine.ext import db
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_dir),
autoescape=True)
def render_str(template, **params):
t = jinja_env.get_template(template)
return t.render(params)
class MainPage(BaseHandler):
def get(self):
self.redirect("/blog")
def blog_key(name='default'):
return db.Key.from_path('blogs', name)
class FrontPage(BaseHandler):
def get(self):
posts = db.GqlQuery("SELECT * FROM Post ORDER BY created DESC LIMIT 10") # NOQA
comments = db.GqlQuery("SELECT * FROM Comment ORDER BY created ASC limit 10") # NOQA
self.render('front.html', posts=posts, comments=comments)
def post(self):
newpost = self.request.get("newPost")
if newpost:
self.redirect("/blog/newpost")
class PostPage(BaseHandler):
def get(self, post_id):
key = db.Key.from_path('Post', int(post_id), parent=blog_key())
post = db.get(key)
if not post:
return self.error(404)
self.render("permalink.html", post=post)
app = webapp2.WSGIApplication([('/', MainPage),
('/blog/?', FrontPage),
('/blog/([0-9]+)', PostPage),
('/blog/newpost', NewPost),
('/signup', Register),
('/blog/([0-9]+)/editpost', EditPost),
('/blog/([0-9]+)/like', LikePost),
('/blog/([0-9]+)/dislike', DislikePost),
('/login', Login),
('/error', Error),
('/logout', Logout),
('/blog/([0-9]+)/comment', NewComment),
('/blog/([0-9]+)/editcomment/([0-9]+)',
EditComment),
('/blog/([0-9]+)/deletecomment/([0-9]+)',
DeleteComment),
('/commenterror', CommentError),
('/blog/([0-9]+)/deletepost', DeletePost)
],
debug=True)