diff --git a/blog/posts/posts_controller.py b/blog/posts/posts_controller.py index de8dbc5..699a7c3 100644 --- a/blog/posts/posts_controller.py +++ b/blog/posts/posts_controller.py @@ -1,10 +1,10 @@ -from flask import Blueprint -from flask import render_template, request, redirect, url_for, jsonify +import os +from flask import Blueprint,render_template, request, redirect, url_for from common.services.posts_service import PostService +from common.services import tags_service from blog import resp from blog import cache from blog.posts.service import posts_blog_service -import os posts_bp = Blueprint( "posts", __name__) @@ -34,16 +34,43 @@ def blog(): "post_len": post_len} else: posts_data = False + post_len = 0 + return render_template("blog/index.html", posts_data=posts_data, resp=resp, prev_limit=prev_limit, post_len=post_len) @posts_bp.route("/post/") def get_post_title(blog_title): - post = PostService().get_post_by_title(blog_title) - if post: - post_data = post + post_data = PostService().get_post_by_title(blog_title) + if len(post_data) > 0: + data_resp = {"post_data": post_data[0], "tags": post_data[1]} + else: + data_resp = {"post_data": False, "tags": False} + + return render_template("blog/post.html", data_resp=data_resp, resp=resp) + +@posts_bp.route("/post/category/") +def get_post_tag(tag): + prev_limit = request.args.get("prev_limit") + posts = tags_service.get_post_tags(tag) + if not prev_limit and posts: + post_len = len(posts) + prev_limit = os.environ.get("post_init_limit") + posts_data = posts[:int(prev_limit)] + elif prev_limit and posts: + post_len = len(posts) + new_limit = int(prev_limit) + int(os.environ.get("post_init_limit")) + posts_data = posts[int(prev_limit):new_limit] + posts_serialized = [posts_blog_service.serialize( + post) for post in posts_data] + return {"posts_resp": posts_serialized, + "posts_html_reponse": posts_blog_service.get_posts_html_resp(posts_serialized, len(posts_data), new_limit), + "prev_limit": new_limit, + "load_more": True, + "post_len": post_len} else: - post_data = False + return redirect(url_for("posts.blog")) - return render_template("blog/post.html", post_data=post_data, resp=resp) + return render_template("blog/index.html", + posts_data=posts_data, resp=resp, prev_limit=prev_limit, post_len=post_len) diff --git a/blog/posts/service/posts_blog_service.py b/blog/posts/service/posts_blog_service.py index 034eee6..2610c8f 100644 --- a/blog/posts/service/posts_blog_service.py +++ b/blog/posts/service/posts_blog_service.py @@ -10,8 +10,7 @@ def serialize(obj): "author": obj.author } - -def get_posts_html_resp(serialized_obj, post_len, limit): +def get_posts_html_resp(serialized_obj, post_len, new_limit): str_concat = "" for post_obj in serialized_obj: if cache.get(str(post_len)): @@ -21,8 +20,7 @@ def get_posts_html_resp(serialized_obj, post_len, limit): "" + "
" cache.set(str(post_len), str_concat, timeout=50) - return str_concat - + return str_concat #if post_len > limit: # # Add load more content # str_concat += "
" + "" + \ diff --git a/blog/templates/blog/base.html b/blog/templates/blog/base.html index b5b2639..090394e 100644 --- a/blog/templates/blog/base.html +++ b/blog/templates/blog/base.html @@ -26,7 +26,7 @@