def initialize(self, *a, **kw): """ Auth middleware to set self.user if set in cookie """ Handler.initialize(self, *a, **kw) uid = self.read_secure_cookie('user_id') if uid and User.get_by_id(int(uid)): self.user = uid self.blog_user = User.get_by_id(int(uid)) else: self.user = None self.blog_user = None
def post(self): """ To process ans store blog post information into database """ title = self.request.get("title") content = self.request.get("content") if self.user: if title and content: user_obj = User.get_by_id(int(self.user)) # adding publish date directly for now post = Post(title=title, content=content, user=user_obj, published_at=datetime.now()) new_post = post.put() self.redirect("/blog/{}".format(db.get(new_post).slug)) else: error = "Both title and art required for submitting !" self.render("add_blog.html", subject=title, content=content, error=error) else: cookie_error = "Your session has expired please login again to continue!" self.render('login.html', error=cookie_error)
def get(self): posts = db.GqlQuery("SELECT * FROM Post WHERE is_draft=False ORDER BY created_at DESC LIMIT 10") if self.user: user = User.get_by_id(int(self.user)) self.render('blog_list.html', posts=posts, username=user.username) else: self.render('blog_list.html', posts=posts)
def get(self): posts = db.GqlQuery("SELECT * FROM Post WHERE is_draft=False " "ORDER BY created_at DESC LIMIT 10") if self.user: user = User.get_by_id(int(self.user)) self.render('blog_list.html', posts=posts, username=user.username) else: self.render('blog_list.html', posts=posts)
def post(self, post_id): """ Create a new comment for the post with post_id as in params """ if not self.user: error = "You must be logged in to comment" self.render('login.html', error=error) else: post = Post.get_by_id(int(post_id)) user_obj = User.get_by_id(int(self.user)) comment = self.request.get('comment') comment = Comment(comment=comment, user=user_obj, post=post) comment.put() self.redirect("/blog/{}".format(post.slug))
def get(self, post_id): """ """ user_obj = User.get_by_id(int(self.user)) if not self.user: cookie_error = "Your session has expired please login again to continue!" self.render('login.html', error=cookie_error) else: post = Post.get_by_id(int(post_id)) author = post.user.key().id() if author == int(self.user) or Like.all().filter('post =', post).filter('user ='******'/like/error') else: like = Like(post=post, user=user_obj) like.put() self.redirect("/blog/{}".format(post.slug))