def category(category_id): # get category information category_info = (Categories.get(Categories.category_id == category_id)) # get posts list. article_list = (BasketArticleList.select().join(Authors, on=(Authors.author_id == BasketArticleList.author)) .where(BasketArticleList.category == category_info.category_id, BasketArticleList.show_status == ShowStatusEnum.PUBLIC_POST.value) .order_by(-BasketArticleList.updated_at).limit(10)) for post in article_list: post.category = category_info # get page number page_num = math.ceil(len(BasketArticleList.filter( BasketArticleList.category == category_info.category_id, BasketArticleList.show_status == ShowStatusEnum.PUBLIC_POST.value)) / 10) # get tags tags = Tags.select() # get popular articles. popular_articles = (BasketArticleList.select().join(Authors, on=(Authors.author_id == BasketArticleList.author)) .where(BasketArticleList.show_status == ShowStatusEnum.PUBLIC_POST.value) .order_by(-BasketArticleList.updated_at).limit(5)) return { 'article_list': basket_article_list_serializer.dump(article_list, many=True).data, 'current_page': 1, 'max_page': page_num, 'category': category_info, 'tags': tags, 'popular_posts': popular_articles, }
def posts(page): # if get the first page of the posts, # redirect to the index page. if page == 1: redirect('/') # get current page posts. article_list = (BasketArticleList.select().join(Authors, on=(Authors.author_id == BasketArticleList.author)) .join(Categories, on=(Categories.category_id == BasketArticleList.category)) .where(BasketArticleList.show_status == ShowStatusEnum.PUBLIC_POST.value) .order_by(-BasketArticleList.updated_at).paginate(page, 10)) # get page number page_num = math.ceil(len(BasketArticleList.filter( BasketArticleList.show_status == ShowStatusEnum.PUBLIC_POST.value)) / 10) # get tags tags = Tags.select() # get popular articles. popular_articles = (BasketArticleList.select().join(Authors, on=(Authors.author_id == BasketArticleList.author)) .where(BasketArticleList.show_status == ShowStatusEnum.PUBLIC_POST.value) .order_by(-BasketArticleList.updated_at).limit(5)) return { 'article_list': basket_article_list_serializer.dump(article_list, many=True).data, 'current_page': page, 'max_page': page_num, 'tags': tags, 'popular_posts': popular_articles }
def post(post_id): post_ = (PoolArticle.select().join(Authors, on=(Authors.author_id == PoolArticle.author)) .join(Categories, on=(Categories.category_id == PoolArticle.category)) .join(BasketArticleList, on=(BasketArticleList.post_id == PoolArticle.post_id)) .where(PoolArticle.post_id == post_id, BasketArticleList.show_status == ShowStatusEnum.PUBLIC_POST.value)) if not post_: abort(404, 'Page Not Found') return { 'post': basket_article_list_serializer.dump(post_[0]).data, }
def author(author_id, page): if page == 1: redirect('/author/' + quote(author_id)) # get author information author_info = (Authors.get(Authors.author_id == author_id)) # get posts list. article_list = (BasketArticleList.select().join( Categories, on=(Categories.category_id == BasketArticleList.category)).where( BasketArticleList.author == author_info.author_id, BasketArticleList.show_status == ShowStatusEnum.PUBLIC_POST.value).order_by( BasketArticleList.updated_at).paginate(page, 10)) for post in article_list: post.author = author_info # get page number page_num = math.ceil( len( BasketArticleList.filter( BasketArticleList.author == author_id, BasketArticleList.show_status == ShowStatusEnum.PUBLIC_POST.value)) / 10) # get tags tags = Tags.select() # get popular articles. popular_articles = (BasketArticleList.select().join( Authors, on=(Authors.author_id == BasketArticleList.author)).where( BasketArticleList.show_status == ShowStatusEnum.PUBLIC_POST.value). order_by(-BasketArticleList.updated_at).limit(5)) return { 'article_list': basket_article_list_serializer.dump(article_list, many=True).data, 'current_page': page, 'max_page': page_num, 'author': author_info, 'tags': tags, 'popular_posts': popular_articles, }
def category(year, page): if page == 1: redirect('/archive/' + str(year)) # get posts list. article_list = (BasketArticleList.select().join( Authors, on=(Authors.author_id == BasketArticleList.author)).join( Categories, on=(Categories.category_id == BasketArticleList.category)).where( BasketArticleList.updated_at.year == year, BasketArticleList.show_status == ShowStatusEnum.PUBLIC_POST. value).order_by(-BasketArticleList.updated_at).paginate( page, 10)) # get page number page_num = math.ceil( len( BasketArticleList.filter( BasketArticleList.updated_at.year == year, BasketArticleList.show_status == ShowStatusEnum.PUBLIC_POST.value)) / 10) # get tags tags = Tags.select() # get popular articles. popular_articles = (BasketArticleList.select().join( Authors, on=(Authors.author_id == BasketArticleList.author)).where( BasketArticleList.show_status == ShowStatusEnum.PUBLIC_POST.value). order_by(-BasketArticleList.updated_at).limit(5)) return { 'article_list': basket_article_list_serializer.dump(article_list, many=True).data, 'current_page': page, 'max_page': page_num, 'tags': tags, 'popular_post': popular_articles, 'year': year }