-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblog_handler.py
38 lines (28 loc) · 1.19 KB
/
blog_handler.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
import webapp2
from handler import Handler
from cookie import make_cookie, check_cookie
from user import User
class BlogHandler(Handler):
COOKIE_USER_ID = 'user_id'
def render(self, template_filename, **kw):
kw['user'] = self.user
Handler.render(self, template_filename, **kw)
def set_secure_cookie(self, name, value):
cookie = make_cookie(name, value)
self.response.headers.add_header('Set-Cookie', cookie)
def read_secure_cookie(self, name):
cookie_value = self.request.cookies.get(name)
return check_cookie(cookie_value)
def login(self, user):
self.set_secure_cookie(BlogHandler.COOKIE_USER_ID, str(user.key.id()))
def logout(self):
self.set_secure_cookie(BlogHandler.COOKIE_USER_ID, '')
def initialize(self, *a, **kw):
webapp2.RequestHandler.initialize(self, *a, **kw)
uid = self.read_secure_cookie(BlogHandler.COOKIE_USER_ID)
self.user = uid and User.get_by_id(int(uid))
class BlogRegisteredOnlyHandler(BlogHandler):
def initialize(self, *a, **kw):
BlogHandler.initialize(self, *a, **kw)
if not self.user:
self.redirect(self.uri_for("signup"), abort=True)