def show_category(self, Request): """ Controller to show poss by category""" category = Request.param('id') posts = Post.where('category', category).where('is_live', 1).get() return view('blog/category', {'author': User, 'category': category, 'posts': posts})
def show_one(self, Request, RenderEngine): """ Controller to show single post""" # Get post via slug slug = Request.param('id') posts = Post.where('slug', slug).where('is_live', 1).get() post = posts[0] post.body = RenderEngine(post.body) # Get current author user = User.where('id', post.author_id).get() # Get recent posts recent_posts = Post.where('is_live', 1).order_by('updated_at', 'desc').take(5).get() return view('blog/post', {'user': user[0], 'post': post, 'recent': recent_posts })
def show_author(self, Request): """ Controller to show posts by author""" user_name = Request.param('id') author = User.where('user_name', user_name).get() posts = Post.where('author_id', author[0].id).where('is_live', 1).get() return view('blog/author', {'author': author[0], 'posts': posts})
def activate(self, Request): """ Activates post to be displayed """ if not Auth(Request).user(): Request.redirect('dashboard') slug = Request.param('id') posts = Post.where('slug', slug).get() post = posts[0] post.is_live = True post.save() return Request.redirect('dashboard/blog', {'Auth': Auth(Request)})
def delete(self, Request): """ Delete Post Controller """ if not Auth(Request).user(): Request.redirect('/dashboard') slug = Request.param('id') posts = Post.where('slug', slug).get() post = posts[0] post.delete() return Request.redirect('dashboard/blog', {'Auth': Auth(Request)})
def deactivate(self, Request): """ Removes post from active list """ if not Auth(Request).user(): Request.redirect('dashboard') slug = Request.param('id') posts = Post.where('slug', slug).get() post = posts[0] post.is_live = False post.save() return Request.redirect('dashboard/blog', {'Auth': Auth(Request)})
def show_delete(self, Request): """ Display Post Delete page """ if not Auth(Request).user(): Request.redirect('/dashboard') slug = Request.param('id') posts = Post.where('slug', slug).get() post = posts[0] return view('dashboard/post/delete', { 'post': post, 'Auth': Auth(Request) })
def preview(self, Request, RenderEngine): """ Display all posts in blog editor """ if not Auth(Request).user(): Request.redirect('dashboard') # Get post via slug slug = Request.param('id') posts = Post.where('slug', slug).get() post = posts[0] post.body = RenderEngine(post.body) return view('dashboard/post/preview', { 'author': User, 'Auth': Auth(Request), 'posts': post })
def update(self, Request): """ Update Post Controller """ if not Auth(Request).user(): Request.redirect('/dashboard') slug = Request.param('id') posts = Post.where('slug', slug).get() post = posts[0] # Updates Post post.title = remove_whitespaces(Request.input('title')) post.slug = slugify(post.title) post.body = remove_whitespaces(Request.input('body')) post.category = remove_whitespaces(Request.input('category')) post.save() return Request.redirect('dashboard/post/{}/update'.format(post.slug), {'Auth': Auth(Request)})
def show_all(self): """ Controller to show all posts""" posts = Post.where('is_live', 1).get() return view('blog', {'author': User, 'posts': posts})