Skip to content

Commit

Permalink
Comments Email Setup
Browse files Browse the repository at this point in the history
  • Loading branch information
prakass1 committed Jan 8, 2021
1 parent 3690f96 commit ce25cbc
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 4 deletions.
5 changes: 5 additions & 0 deletions blog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from dotenv import load_dotenv, find_dotenv
from instance.config import app_config
from flask_wtf.csrf import CSRFProtect
from flask_mail import Mail

load_dotenv(find_dotenv())

Expand All @@ -21,6 +22,7 @@


csrf_protect = CSRFProtect()
mail = Mail()

def create_app(config_name):
# More on DB init here...
Expand All @@ -37,6 +39,9 @@ def create_app(config_name):

# init cache to app
cache.init_app(app)

# Email
mail.init_app(app)

with app.app_context():
# Module imports
Expand Down
4 changes: 4 additions & 0 deletions blog_admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
from flask_login import LoginManager
from common import db, cache
from flask_wtf.csrf import CSRFProtect
from flask_mail import Mail

login_manager = LoginManager()
csrf_protect = CSRFProtect()
mail = Mail()

username = environ.get("ADMIN_USERNAME")
password = environ.get("PASSWORD")
Expand All @@ -31,6 +33,8 @@ def create_admin_app(config_name):
csrf_protect.init_app(app)
# init cache
cache.init_app(app)
# init email
mail.init_app(app)

# Set the login manager
login_manager.login_view = 'auth.admin'
Expand Down
20 changes: 18 additions & 2 deletions common/controller/comments_api_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def add_comment():
author_comment = request_body["author_comment"]
blog_title = request_body["blog_title"]
g_recaptcha = request_body["g_recaptcha"]

is_admin = False
post_data = PostService().get_post_by_title(blog_title)

if len(post_data) > 0:
Expand All @@ -32,9 +32,25 @@ def add_comment():
# Verify recaptcha else do not add the comment...
if verify_recaptcha(g_recaptcha)["success"]:
print("Recaptcha is verified, is human")
if comments_service_obj.add_comment(author_name, author_email, author_comment, post_data[0]):
if author_email == os.environ.get("EMAIL"):
is_admin=True

if comments_service_obj.add_comment(author_name, author_email, author_comment, post_data[0], is_admin=is_admin):
if is_admin:
c_status = comments_service.CommentService.send_email(author_comment, author_name, post_data[0])
if c_status:
#Admin and it is a reply
return "Your comment is posted and notification sent to the original commenter"

#Admin but not reply
return "Your comment is posted"

# Commented but not admin
return "Your comment is posted and is under moderation"

#Internal error
return "There has been an error while posting the comment, try after sometime..."
#recaptcha failed
return "Cannot comment this request"


Expand Down
24 changes: 22 additions & 2 deletions common/services/comments_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from common import db, cache
from common.models import posts_model, comments_model
from common.services.comment_state_enums import States
from common.services.utility import send_email, check_reply


class CommentService():
Expand Down Expand Up @@ -38,22 +39,41 @@ def serialize_comments(cls, comments_db_obj, is_admin=False):

return comments_list

def add_comment(self, author_name, author_email, author_comment, post_db_obj):
def add_comment(self, author_name, author_email, author_comment, post_db_obj, is_admin=False):
try:
if is_admin:
comment_state = States.APPROVED.value
else:
comment_state = States.UNDER_MODERATION.value

comment_db_obj = comments_model.Comments(author_name=author_name,
author_email=author_email,
author_comment=author_comment,
comment_uuid=str(
uuid.uuid4()).split("-")[0],
posted_date=datetime.datetime.now(),
comment_state=States.UNDER_MODERATION.value,
comment_state= comment_state,
posts=post_db_obj)
db.session.add(comment_db_obj)
db.session.commit()
return True
except exc.SQLAlchemyError:
traceback.print_exc()
return False

@classmethod
def send_email(cls, author_comment, author_name, post_db_obj):
c_name_tuple, c_status = check_reply(author_comment, post_db_obj)
if c_name_tuple and c_status:
e_status = send_email(author_comment, author_name, c_name_tuple, post_db_obj)
if e_status:
print("The reply email is sent to -- ", c_name_tuple[0])
return True
else:
print("error while sending the email reply")
return False
else:
return False

@classmethod
def get_comment_count(cls, is_admin=False):
Expand Down
32 changes: 32 additions & 0 deletions common/services/utility.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from os import environ
from common.models import comments_model
from flask_mail import Message
from blog_admin import mail

def check_reply(comment, post_obj):
refer_names = list()
words_list = comment.split(" ")
for word in words_list:
if len(word) >= 3 and "@" in word[0] and ":" in word[len(word) - 1]:
refer_names.append(word.replace("@", "").replace(":",""))
if len(refer_names) > 0:
for name in refer_names:
com_db_obj = comments_model.Comments.query.filter(comments_model.Comments.author_name.like("%" + name + "%"), comments_model.Comments.post_id==post_obj.p_id).first()
if com_db_obj.author_name:
return (com_db_obj.author_name, com_db_obj.author_email), True
return None, False


def send_email(author_comment, author_name, c_name_tuple, post_db_obj):
try:
msg = Message('New message from Blog',
sender = environ.get("MAIL_USERNAME"),
recipients = [c_name_tuple[1]])
msg_body = "<h4> Hello," + c_name_tuple[0] + "There has been a reply to your comment to " + post_db_obj.title + ".</h4><br/>"
msg_body += "<div style='width: 30%;display: flex;flex-direction: column;border: 1px red solid;'>"
msg_body += "<div>" + "<p>Comment: " + author_comment + "</p>" + "<p>Author: " + author_name + "</p></div>" + "</div>"
msg.html = msg_body
mail.send(msg)
return True
except (UnicodeError, TypeError, AttributeError):
return False

0 comments on commit ce25cbc

Please sign in to comment.