Esempio n. 1
0
    def get(self):
        default_size = 10
        summary_length = 200
        need_pagination = False
        cate_id = self.get_query_argument('cate', None)
        size = self.get_query_argument('size', default_size)
        categories = Category.list(self.current_user.id)
        user_id = self.current_user.id
        if cate_id:
            posts = Post.list_by_category(user_id, int(cate_id), 0, int(size))
            count = Post.count_posts_by_category(user_id, cate_id)
        else:
            count = Post.count_posts(user_id)
            posts = Post.list(user_id, 0, int(size))
        if posts:
            need_pagination = count > len(posts)
            for post in posts:
                _html = markdown.markdown(post.content)
                soup = BeautifulSoup(_html, 'html.parser')
                img = soup.find('img')
                last_modified = post.last_modified
                if img:
                    img['class'] = 'inner-img-limit'
                _text = soup.get_text()
                if _text and len(_text) > summary_length:
                    _text = _text[0:summary_length] + '...'
                post['cover'] = img
                post['summary'] = _text
                post['author'] = self.current_user

        self.render('posts.html', cate_id=cate_id, categories=categories,
            posts=posts, page_size=size, need_pagination=int(need_pagination))