From 98bd42c790948089eca96c71021b4fbd4641a7e4 Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Mon, 5 Feb 2024 16:17:45 -0600 Subject: Add reply creation --- src/controllers/__init__.py | 1 + src/controllers/replies_controller.py | 18 ++++++++++++++++ src/services/__init__.py | 1 + src/services/callback.py | 26 ++++++++++++++++++++--- src/services/comment_creation_callbacks.py | 25 +++++----------------- src/services/create_reply_service.py | 33 ++++++++++++++++++++++++++++++ src/services/reply_creation_callback.py | 12 +++++++++++ 7 files changed, 93 insertions(+), 23 deletions(-) create mode 100644 src/controllers/replies_controller.py create mode 100644 src/services/create_reply_service.py create mode 100644 src/services/reply_creation_callback.py (limited to 'src') diff --git a/src/controllers/__init__.py b/src/controllers/__init__.py index 0fa79a7..56db251 100644 --- a/src/controllers/__init__.py +++ b/src/controllers/__init__.py @@ -1,2 +1,3 @@ import src.controllers.comments_controller +import src.controllers.replies_controller import src.controllers.dashboard_controller diff --git a/src/controllers/replies_controller.py b/src/controllers/replies_controller.py new file mode 100644 index 0000000..9228b5c --- /dev/null +++ b/src/controllers/replies_controller.py @@ -0,0 +1,18 @@ +from flask import Blueprint, request, abort, render_template +from src.services import CreateReplyService + + +replies_blueprint = Blueprint('replies_controller', + '__replies_controller__') + + +@replies_blueprint.post('/comments//replies') +def create(comment_id): + breakpoint() + service = CreateReplyService(request.get_json(), comment_id) + status_code = service.call() + + if status_code != 200: + abort(status_code) + + return render_template('comments/index.jinja') diff --git a/src/services/__init__.py b/src/services/__init__.py index bf71a33..aead8fb 100644 --- a/src/services/__init__.py +++ b/src/services/__init__.py @@ -1 +1,2 @@ from src.services.create_comment_service import CreateCommentService +from src.services.create_reply_service import CreateReplyService diff --git a/src/services/callback.py b/src/services/callback.py index 5dc3149..60383d6 100644 --- a/src/services/callback.py +++ b/src/services/callback.py @@ -1,6 +1,26 @@ -class Callback: +from config import user_config +import src.lib as lib + + +class Callbacks: + def __init__(self, object): + self._object = object + + def run_callbacks(self): + self._moderate() + self._deliver_notification() + + return self._object + def _moderate(self): - pass + if self._moderation_setting == 'strict': + self._object.approved = False + return + + if lib.contains_forbidden_term(self._object.content): + if user_config['Env']['blacklist_match_action'] == 'reject': + self._object.approved = False - def deliver_notification(self): + # TODO: Email notifications. + def _deliver_notification(self): pass diff --git a/src/services/comment_creation_callbacks.py b/src/services/comment_creation_callbacks.py index 5f5d024..93080ee 100644 --- a/src/services/comment_creation_callbacks.py +++ b/src/services/comment_creation_callbacks.py @@ -1,27 +1,12 @@ -import src.lib as lib from config import user_config -from src.services.callback import Callback +from src.services.callback import Callbacks -class CommentCreationCallbacks(Callback): +class CommentCreationCallbacks(Callbacks): """ Check config for blacklists, moderation behavior and notification delivery. """ - def __init__(self, comment): - self._comment = comment - - def run_callbacks(self): - self._moderate() - self._deliver_notification() - - return self._comment - - def _moderate(self): - if user_config['Env']['moderation'] == 'strict': - self._comment.approved = False - return - - if lib.contains_forbidden_term(self._comment.content): - if user_config['Env']['blacklist_match_action'] == 'reject': - self._comment.approved = False + def __init__(self, object): + super().__init__(object) + self._moderation_setting = user_config['Env']['moderation'] diff --git a/src/services/create_reply_service.py b/src/services/create_reply_service.py new file mode 100644 index 0000000..12600dd --- /dev/null +++ b/src/services/create_reply_service.py @@ -0,0 +1,33 @@ +from sqlalchemy import select +from src.database.models import Blog, Comment, Reply +from src.database import db +from src.services.reply_creation_callback import ReplyCreationCallbacks + +class CreateReplyService: + def __init__(self, reply_data, comment_id): + self._comment_id = comment_id + self._content = reply_data.get('content') + self._email = reply_data.get('email') + self._author = reply_data.get('author') + + def call(self): + if not self._comment_exists(): + return 404 + + reply = ReplyCreationCallbacks( + Reply(comment_id=self._comment_id, + content=self._content, email=self._email, + author=self._author) + ).run_callbacks() + db.session.add(reply) + db.session.commit() + + return 200 + + def _comment_exists(self): + search_comment_statement = select(Comment).where( + Comment.id == self._comment_id + ) + + return db.session.execute(search_comment_statement) \ + .scalars().first() is not None diff --git a/src/services/reply_creation_callback.py b/src/services/reply_creation_callback.py new file mode 100644 index 0000000..5d8f2ba --- /dev/null +++ b/src/services/reply_creation_callback.py @@ -0,0 +1,12 @@ +from config import user_config +from src.services.callback import Callbacks + + +class ReplyCreationCallbacks(Callbacks): + """ + Check config for blacklists, moderation behavior and + notification delivery. + """ + def __init__(self, object): + super().__init__(object) + self._moderation_setting = user_config['Env']['reply_moderation'] -- cgit v1.2.3