Skip to content

Commit

Permalink
blog tagging service
Browse files Browse the repository at this point in the history
  • Loading branch information
prakass1 committed Dec 21, 2020
1 parent 70f0072 commit 77b093e
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 32 deletions.
43 changes: 35 additions & 8 deletions blog/posts/posts_controller.py
Original file line number Diff line number Diff line change
@@ -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__)
Expand Down Expand Up @@ -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/<blog_title>")
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/<tag>")
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)
6 changes: 2 additions & 4 deletions blog/posts/service/posts_blog_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)):
Expand All @@ -21,8 +20,7 @@ def get_posts_html_resp(serialized_obj, post_len, limit):
"</h2></a>" + "<p class='post-meta'>Posted by " + "<a href='#'>" + \
post_obj["author"] + "</a> on " + post_obj["posted_date"] + "</p></div><hr>"
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 += "<div class='clearfix'>" + "<button class='btn btn-primary float-right' id='load_more'>Older Posts &rarr;</button>" + \
Expand Down
6 changes: 3 additions & 3 deletions blog/templates/blog/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

<style>
.newhead {
margin-bottom: 5px;
margin-bottom: 10px;
background: no-repeat center center;
/*background-color: #868e96;*/
background-color: #DCDCDC;
Expand Down Expand Up @@ -66,7 +66,7 @@
<body>

<!-- Navigation -->
<nav class="navbar navbar-expand-lg bg-dark navbar-light fixed-top" id="mainNav">
<nav class="navbar navbar-expand-lg bg-info navbar-dark fixed-top" id="mainNav">
<div class="container">
<a class="navbar-brand" href="{{ url_for('posts.blog') }}">{{resp["blog_header"]}}</a>
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse"
Expand Down Expand Up @@ -97,7 +97,7 @@
<footer>
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<div class="col-lg-10 col-md-10 mx-auto">
<ul class="list-inline text-center">
<li class="list-inline-item">
<a href={{resp.social_linkedin}}>
Expand Down
4 changes: 2 additions & 2 deletions blog/templates/blog/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<header class="newhead">
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<div class="col-lg-10 col-md-10 mx-auto">
<div class="site-heading">
<h1>{{resp.blog_header}}</h1>
<span class="subheading">{{resp.blog_subheader}}</span>
Expand All @@ -17,7 +17,7 @@ <h1>{{resp.blog_header}}</h1>
<!-- Main Content -->
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<div class="col-lg-10 col-md-10 mx-auto">
<div class="display-posts">
{% if posts_data %}
{% for post in posts_data %}
Expand Down
26 changes: 17 additions & 9 deletions blog/templates/blog/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@
<header class="newhead">
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
{% if post_data %}
<div class="col-lg-12 col-md-12 mx-auto">
{% if data_resp.post_data %}
<div class="post-heading">
<h1>{{post_data.title}}</h1>
<!--<h2 class="subheading">Problems look mighty small from 150 miles up</h2>-->
<span class="meta">Posted by
<a href="#">{{post_data.author}}</a>
on {{post_data.posted_date.strftime('%B %d, %Y')}}</span>
<h1>{{data_resp.post_data.title}}</h1>
<div style="margin-top: 10px;">
{% for item in data_resp.tags %}
<a href="{{url_for('posts.get_post_tag', tag=item.tag)}}" style="display:inline-block" class="btn-info btn-sm" role="button">
{{item.tag}}
</a>
{% endfor %}
</div>
<div style="margin-top:10px;">
<span class="meta">Posted by
<a href="#">{{data_resp.post_data.author}}</a>
on {{data_resp.post_data.posted_date.strftime('%B %d, %Y')}}</span>
</div>
</div>
</div>
</div>
Expand All @@ -21,8 +29,8 @@ <h1>{{post_data.title}}</h1>
<article>
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
{{post_data.content|safe}}
<div class="col-lg-12 col-md-12 mx-auto">
{{data_resp.post_data.content|safe}}
</div>
</div>
</div>
Expand Down
3 changes: 2 additions & 1 deletion common/models/posts_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from common.models.images_model import Images
from common.models.tags_model import Tags


class Posts(db.Model):
__tablename__ = "posts"
p_id = db.Column(db.Integer, primary_key=True)
Expand All @@ -12,4 +13,4 @@ class Posts(db.Model):
user_id = db.Column(db.Integer, db.ForeignKey("users.id"))
modified_flag = db.Column(db.Boolean, default=False)
images = db.relationship("Images", backref="posts", lazy=True)
category = db.relationship("Tags", backref="posts", lazy=True)
category = db.relationship("Tags", backref="posts", lazy="dynamic")
13 changes: 8 additions & 5 deletions common/services/posts_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def add_post(self, blog_title, blog_author, blog_content, curr_user, image_list,
except exc.SQLAlchemyError:
traceback.print_exc()
return False


def delete_post(self, post):
try:
Expand Down Expand Up @@ -116,14 +115,18 @@ def get_all_posts(cls, order_by=False, is_admin=False):
def get_post_by_title(cls, post_title, is_admin=False):
if is_admin:
post = posts_model.Posts.query.filter_by(title = post_title).first()
tags = tags_model.Tags.query.filter_by(posts=post).all()
post_set = [post, tags]
elif cache.get(post_title):
print("Retreiving from cache")
post = cache.get(post_title)
print("Retreiving from cache")
post_set = cache.get(post_title)
else:
print("Not yet cached, caching the current post details")
post = posts_model.Posts.query.filter_by(title = post_title).first()
cache.set(post_title, post, timeout=50)
return post
tags = tags_model.Tags.query.filter_by(posts=post).all()
cache.set(post_title, [post, tags], timeout=50)
post_set = [post, tags]
return post_set

@classmethod
def get_tags_for_post(cls, post):
Expand Down
22 changes: 22 additions & 0 deletions common/services/tags_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from common import cache
from common.models import tags_model, posts_model


def get_post_tags(tag_name):
if cache.get(tag_name):
print("Retreiving the posts from cache for the tag", tag_name)
posts = cache.get(tag_name)
else:
print("Not yet cached retreiving from db for the tag", tag_name)
tags_db_list = tags_model.Tags.query.filter_by(tag=tag_name).all()
if len(tags_db_list) > 0:
posts = []
for db_tag in tags_db_list:
print(db_tag.posts.title)
print(db_tag.posts.posted_date)
posts.append(db_tag.posts)
cache.set(tag_name, posts, timeout=50)
else:
return False

return posts

0 comments on commit 77b093e

Please sign in to comment.