Skip to content

Commit

Permalink
context_processor implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
prakass1 committed Jan 5, 2021
1 parent 7ef0344 commit 3690f96
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 16 deletions.
13 changes: 9 additions & 4 deletions blog_admin/auth/auth_controller.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
from flask import Blueprint, render_template, request, redirect,url_for
from flask import Blueprint, render_template, request, redirect,url_for, current_app
from flask_login import login_required, current_user, logout_user
from common.services import comments_service

auth_bp = Blueprint("auth", __name__)

@current_app.context_processor
def inject_data():
count_comments = comments_service.CommentService.get_comment_count(is_admin=True)
return dict(user=current_user, no_comments=count_comments)

@auth_bp.route("/", methods=["GET"])
def admin():
if current_user.is_authenticated:
Expand All @@ -16,13 +22,12 @@ def intrim_login():
if current_user.changed_pass:
return redirect(url_for("posts.dash_posts"))

return render_template("auth/login_intrim.html", user = current_user)
return render_template("auth/login_intrim.html")

@auth_bp.route("/dashboard", methods=["GET"])
@login_required
def dashboard():
return render_template("dashboard/dashboard.html", user=current_user)

return render_template("dashboard/dashboard.html")


@auth_bp.route("/logout")
Expand Down
4 changes: 3 additions & 1 deletion blog_admin/posts/comments_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
from common.services import comments_service

comments_bp = Blueprint("comments", __name__)
#count_comments = comments_service.CommentService.get_comment_count(is_admin=True)


@comments_bp.route("/comments.html")
@login_required
def dash_comments():
#load all comments under_moderation
comments = comments_service.CommentService.get_comments(is_admin=True)
return render_template("dashboard/comments_moderation.html", user=current_user, comments=comments)
return render_template("dashboard/comments_moderation.html", comments=comments)
13 changes: 8 additions & 5 deletions blog_admin/posts/posts_controller.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
from flask import Blueprint, render_template
from flask_login import login_required, current_user
from common.services import posts_service
from common.services import posts_service, comments_service

posts_bp = Blueprint("posts", __name__)

# Count the comments
#count_comments = comments_service.CommentService.get_comment_count(is_admin=True)


@posts_bp.route("/overview.html")
@login_required
def dash_overview():
return render_template("dashboard/overview.html", user=current_user)
return render_template("dashboard/overview.html")


@posts_bp.route("/posts.html")
@login_required
def dash_posts():
#load all posts
posts = posts_service.PostService.get_all_posts(order_by=True, is_admin=True)
return render_template("dashboard/posts.html", user=current_user, posts=posts)
return render_template("dashboard/posts.html", posts=posts)


@posts_bp.route("/add_post.html")
@login_required
def add_post():
return render_template("dashboard/add_post.html", user=current_user)
return render_template("dashboard/add_post.html")

@posts_bp.route("/edit_post.html/<post_title>")
@login_required
Expand All @@ -33,4 +36,4 @@ def edit_post(post_title):
else:
data_resp = {"post_data": False, "tags": False}

return render_template("dashboard/edit_post.html", user=current_user, data_resp=data_resp)
return render_template("dashboard/edit_post.html", data_resp=data_resp)
9 changes: 9 additions & 0 deletions blog_admin/static/js/comments_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ function approve_comment(comment_status, comment_ref_id) {
$('<strong>' + response.message + '</strong>').prependTo(".commentlog");
$(".toast").toast("show");
$("#" + comment_ref_id).remove();
temp_val = $("#comment-badge").text();
new_val = Integer.parseInt(temp_val) - 1;
if (new_val > 0){
$("#comment-badge").text(new_val);
}
else{
$("a.notification").remove();
}

}
else{
$('<strong>' + response.message + '</strong>').prependTo(".commentlog");
Expand Down
49 changes: 43 additions & 6 deletions blog_admin/templates/dashboard/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,31 @@
.middle {
text-align: center;
}

/* Notification settings */
.notification {
background-color: #555;
color: white;
text-decoration: none;
padding: 8px 5px;
position: relative;
display: inline-block;
border-radius: 2px;
}

.notification:hover {
font-weight: bold;
background: orange;
}

.notification .badge {
position: absolute;
top: -10px;
right: -10px;
border-radius: 50%;
background-color: red;
color: white;
}
</style>
</head>

Expand All @@ -87,6 +112,18 @@
<input class="form-control form-control-dark" type="text" placeholder="Search" aria-label="Search">
</div> -->
<div class="dropdown">
{% if no_comments > 0 %}
<a href="{{url_for('comments.dash_comments')}}" class="notification">
<span>Comments
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-bell-fill"
viewBox="0 0 16 16">
<path
d="M8 16a2 2 0 0 0 2-2H6a2 2 0 0 0 2 2zm.995-14.901a1 1 0 1 0-1.99 0A5.002 5.002 0 0 0 3 6c0 1.098-.5 6-2 7h14c-1.5-1-2-5.902-2-7 0-2.42-1.72-4.44-4.005-4.901z" />
</svg>
</span>
<span id="comment-badge" class="badge">{{no_comments}}</span>
</a>
{% endif %}
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown"
aria-expanded="false">
Hello, {{user.user_name}}
Expand Down Expand Up @@ -129,12 +166,12 @@
</li>
<li class="nav-item">
<a class="nav-link" href="{{url_for('comments.dash_comments')}}">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none"
stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"
class="feather feather-monitor">
<rect x="2" y="3" width="20" height="14" rx="2" ry="2"></rect>
<line x1="8" y1="21" x2="16" y2="21"></line>
<line x1="12" y1="17" x2="12" y2="21"></line>
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor"
class="bi bi-chat-left-text" viewBox="0 0 16 16">
<path
d="M14 1a1 1 0 0 1 1 1v8a1 1 0 0 1-1 1H4.414A2 2 0 0 0 3 11.586l-2 2V2a1 1 0 0 1 1-1h12zM2 0a2 2 0 0 0-2 2v12.793a.5.5 0 0 0 .854.353l2.853-2.853A1 1 0 0 1 4.414 12H14a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2H2z" />
<path
d="M3 3.5a.5.5 0 0 1 .5-.5h9a.5.5 0 0 1 0 1h-9a.5.5 0 0 1-.5-.5zM3 6a.5.5 0 0 1 .5-.5h9a.5.5 0 0 1 0 1h-9A.5.5 0 0 1 3 6zm0 2.5a.5.5 0 0 1 .5-.5h5a.5.5 0 0 1 0 1h-5a.5.5 0 0 1-.5-.5z" />
</svg>
<span class="ml-2">Comment Moderation</span>
</a>
Expand Down
12 changes: 12 additions & 0 deletions common/services/comments_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ def add_comment(self, author_name, author_email, author_comment, post_db_obj):
except exc.SQLAlchemyError:
traceback.print_exc()
return False

@classmethod
def get_comment_count(cls, is_admin=False):
try:
if is_admin:
count = comments_model.Comments.query.filter_by(comment_state=States.UNDER_MODERATION.value).count()
else:
count = comments_model.Comments.query.filter_by(comment_state=States.APPROVED.value).count()
return count
except exc.SQLAlchemyError:
traceback.print_exc()
return 0

@classmethod
def get_comments(cls, post_db_obj=None, is_admin=False):
Expand Down

0 comments on commit 3690f96

Please sign in to comment.