Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions forum/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ class ForumConfig(AppConfig):
}
},
}

def ready(self) -> None:
"""
Import Signals.
"""
import forum.signals # pylint: disable=import-outside-toplevel, unused-import
110 changes: 110 additions & 0 deletions forum/handlers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
"""
Handlers for the forum app.
"""

import logging
from typing import Any, Dict

from forum.search.es_helper import ElasticsearchHelper
from forum.utils import get_str_value_from_collection

log = logging.getLogger(__name__)


def handle_comment_thread_deletion(sender: Any, **kwargs: Dict[str, str]) -> None:
"""
Handle the deletion of a comment thread from the Elasticsearch index.

Args:
sender (Any): The model class that sends the signal.
**kwargs (Dict[str, Any]): Additional arguments, including 'comment_thread_id'.
"""
thread_id = get_str_value_from_collection(kwargs, "comment_thread_id")

es_helper = ElasticsearchHelper()
es_helper.delete_document(sender.index_name, thread_id)


def handle_comment_deletion(sender: Any, **kwargs: Dict[str, Any]) -> None:
"""
Handle the deletion of a comment from the Elasticsearch index.

Args:
sender (Any): The model class that sends the signal.
**kwargs (Dict[str, Any]): Additional arguments, including 'comment_id'.
"""
comment_id = get_str_value_from_collection(kwargs, "comment_id")
es_helper = ElasticsearchHelper()
es_helper.delete_document(sender.index_name, comment_id)


def handle_comment_thread_insertion(sender: Any, **kwargs: Dict[str, Any]) -> None:
"""
Handle the insertion of a comment thread into the Elasticsearch index.

Args:
sender (Any): The model class that sends the signal.
**kwargs (Dict[str, Any]): Additional arguments, including 'comment_thread_id'.
"""
thread_id = get_str_value_from_collection(kwargs, "comment_thread_id")
if thread_id:

thread = sender().get(_id=thread_id)
es_helper = ElasticsearchHelper()
doc = sender().doc_to_hash(thread)
es_helper.index_document(sender.index_name, thread_id, doc)
log.info(f"Thread {thread_id} added to Elasticsearch index")


def handle_comment_insertion(sender: Any, **kwargs: Dict[str, Any]) -> None:
"""
Handle the insertion of a comment into the Elasticsearch index.

Args:
sender (Any): The model class that sends the signal.
**kwargs (Dict[str, Any]): Additional arguments, including 'comment_id'.
"""
comment_id = get_str_value_from_collection(kwargs, "comment_id")
if comment_id:

comment = sender().get(_id=comment_id)
es_helper = ElasticsearchHelper()
doc = sender().doc_to_hash(comment)
es_helper.index_document(sender.index_name, comment_id, doc)
log.info(f"Comment {comment_id} added to Elasticsearch index")


def handle_comment_thread_updated(sender: Any, **kwargs: Dict[str, Any]) -> None:
"""
Handle the update of a comment thread in the Elasticsearch index.

Args:
sender (Any): The model class that sends the signal.
**kwargs (Dict[str, Any]): Additional arguments, including 'comment_thread_id'.
"""
thread_id = get_str_value_from_collection(kwargs, "comment_thread_id")
if thread_id:

thread = sender().get(_id=thread_id)
es_helper = ElasticsearchHelper()
doc = sender().doc_to_hash(thread)
es_helper.update_document(sender.index_name, thread_id, doc)
log.info(f"Thread {thread_id} added to Elasticsearch index")


def handle_comment_updated(sender: Any, **kwargs: Dict[str, Any]) -> None:
"""
Handle the update of a comment in the Elasticsearch index.

Args:
sender (Any): The model class that sends the signal.
**kwargs (Dict[str, Any]): Additional arguments, including 'comment_id'.
"""
comment_id = get_str_value_from_collection(kwargs, "comment_id")
if comment_id:

comment = sender().get(_id=comment_id)
es_helper = ElasticsearchHelper()
doc = sender().doc_to_hash(comment)
es_helper.update_document(sender.index_name, comment_id, doc)
log.info(f"Comment {comment_id} added to Elasticsearch index")
2 changes: 1 addition & 1 deletion forum/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

from .comments import Comment
from .contents import Contents
from .threads import CommentThread
from .subscriptions import Subscriptions
from .threads import CommentThread
from .users import Users

__all__ = ["Comment", "Contents", "CommentThread", "Subscriptions", "Users"]
17 changes: 16 additions & 1 deletion forum/models/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

from bson import ObjectId

from forum.models.threads import CommentThread
from forum.models.contents import BaseContents
from forum.models.threads import CommentThread
from forum.models.users import Users
from forum.utils import get_handler_by_name


class Comment(BaseContents):
Expand Down Expand Up @@ -132,6 +133,10 @@ def insert(
self.update_child_count_in_parent_comment(parent_id, 1)
if comment_thread_id:
self.update_comment_count_in_comment_thread(comment_thread_id, 1)
if result:
get_handler_by_name("comment_inserted").send(
sender=self.__class__, comment_id=str(result.inserted_id)
)
return str(result.inserted_id)

def update(
Expand Down Expand Up @@ -229,6 +234,11 @@ def update(
{"_id": ObjectId(comment_id)},
{"$set": update_data},
)
if result:
get_handler_by_name("comment_updated").send(
sender=self.__class__, comment_id=comment_id
)

return result.modified_count

def delete(self, _id: str) -> int:
Expand Down Expand Up @@ -257,6 +267,11 @@ def delete(self, _id: str) -> int:
self.update_comment_count_in_comment_thread(
comment_thread_id, -(int(no_of_comments_delete))
)
if result:
get_handler_by_name("comment_deleted").send(
sender=self.__class__, comment_id=_id
)

return no_of_comments_delete

def get_author_username(self, author_id: str) -> str | None:
Expand Down
4 changes: 2 additions & 2 deletions forum/models/model_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,14 +400,14 @@ def get_endorsed(thread_ids: list[str]) -> dict[str, bool]:
Returns:
dict[str, bool]: A dictionary mapping thread IDs to their endorsed status (True if endorsed, False otherwise).
"""
endorsed_threads = Contents().find(
endorsed_comments = Comment().find(
{
"comment_thread_id": {"$in": [ObjectId(tid) for tid in thread_ids]},
"endorsed": True,
}
)

return {str(item["comment_thread_id"]): True for item in endorsed_threads}
return {str(item["comment_thread_id"]): True for item in endorsed_comments}


def get_user_read_state_by_course_id(
Expand Down
18 changes: 18 additions & 0 deletions forum/models/threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from bson import ObjectId

from forum.models.contents import BaseContents
from forum.utils import get_handler_by_name


class CommentThread(BaseContents):
Expand All @@ -16,6 +17,15 @@ class CommentThread(BaseContents):
index_name = "comment_threads"
content_type = "CommentThread"

def delete(self, _id: str) -> int:
"""Delete CommentThread"""
result = super().delete(_id)
if result:
get_handler_by_name("comment_thread_deleted").send(
sender=self.__class__, comment_thread_id=_id
)
return result

@classmethod
def mapping(cls) -> dict[str, Any]:
"""
Expand Down Expand Up @@ -150,6 +160,10 @@ def insert(
"historical_abuse_flaggers": historical_abuse_flaggers,
}
result = self._collection.insert_one(thread_data)
if result:
get_handler_by_name("comment_thread_inserted").send(
sender=self.__class__, comment_thread_id=str(result.inserted_id)
)
return str(result.inserted_id)

def update(
Expand Down Expand Up @@ -235,4 +249,8 @@ def update(
{"_id": ObjectId(thread_id)},
{"$set": update_data},
)
if result:
get_handler_by_name("comment_thread_updated").send(
sender=self.__class__, comment_thread_id=thread_id
)
return result.modified_count
1 change: 1 addition & 0 deletions forum/pagination.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Pagination class for forum api."""

from typing import Any

from rest_framework.exceptions import NotFound
from rest_framework.pagination import PageNumberPagination
from rest_framework.request import Request
Expand Down
1 change: 1 addition & 0 deletions forum/search/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""The module includes search functionality."""
Loading