Skip to content

Commit

Permalink
fix: quality checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammad Faraz Maqsood committed Sep 26, 2024
1 parent 3f9115b commit a93a8c1
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 23 deletions.
4 changes: 4 additions & 0 deletions forum/api/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
from forum.backends.mongodb.api import (
create_comment,
delete_comment_by_id,
get_thread_id_by_comment_id,
get_thread_by_id,
get_user_by_id,
mark_as_read,
validate_object,
update_comment_and_get_updated_comment,
update_stats_for_course,
)
from forum.backends.mongodb.comments import Comment
from forum.backends.mongodb.threads import CommentThread
from forum.serializers.comment import CommentSerializer
from forum.utils import ForumV2RequestError

Expand Down Expand Up @@ -120,6 +123,7 @@ def create_child_comment(
anonymous,
anonymous_to_peers,
1,
get_thread_id_by_comment_id(parent_comment_id),
parent_id=parent_comment_id,
)
if not comment:
Expand Down
7 changes: 2 additions & 5 deletions forum/api/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@
from typing import Any

from forum.backends.mongodb import Users
from forum.backends.mongodb.api import (
get_group_ids_from_params,
user_to_hash,
)
from forum.backends.mongodb.api import user_to_hash
from forum.serializers.users import UserSerializer
from forum.utils import ForumV2RequestError
from forum.utils import ForumV2RequestError, get_group_ids_from_params

log = logging.getLogger(__name__)

Expand Down
23 changes: 16 additions & 7 deletions forum/backends/mongodb/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
Subscriptions,
Users,
)
from forum.utils import make_aware
from forum.constants import RETIRED_BODY, RETIRED_TITLE
from forum.utils import get_group_ids_from_params, get_sort_criteria, make_aware

Expand Down Expand Up @@ -365,7 +364,7 @@ def get_abuse_flagged_count(thread_ids: list[str]) -> dict[str, int]:
pipeline: list[dict[str, Any]] = [
{
"$match": {
"comment_thread_id": {"$in": [tid for tid in thread_ids]},
"comment_thread_id": {"$in": thread_ids},
"abuse_flaggers": {"$ne": []},
}
},
Expand Down Expand Up @@ -430,7 +429,7 @@ def get_filtered_thread_ids(
set: A set of filtered thread IDs based on the context and group ID criteria.
"""
context_query = {
"_id": {"$in": [tid for tid in thread_ids]},
"_id": {"$in": thread_ids},
"context": context,
}
context_threads = CommentThread().find(context_query)
Expand All @@ -440,7 +439,7 @@ def get_filtered_thread_ids(
return context_thread_ids

group_query = {
"_id": {"$in": [tid for tid in thread_ids]},
"_id": {"$in": thread_ids},
"$or": [
{"group_id": {"$in": group_ids}},
{"group_id": {"$exists": False}},
Expand All @@ -464,7 +463,7 @@ def get_endorsed(thread_ids: list[str]) -> dict[str, bool]:
"""
endorsed_comments = Comment().find(
{
"comment_thread_id": {"$in": [tid for tid in thread_ids]},
"comment_thread_id": {"$in": thread_ids},
"endorsed": True,
}
)
Expand Down Expand Up @@ -1034,7 +1033,7 @@ def user_to_hash(
comment_thread_ids = filter_standalone_threads(list(comments))

group_query = {
"_id": {"$in": [tid for tid in comment_thread_ids]},
"_id": {"$in": comment_thread_ids},
"$and": [
{"group_id": {"$in": specified_groups_or_global}},
{"group_id": {"$exists": False}},
Expand Down Expand Up @@ -1310,7 +1309,7 @@ def create_comment(
anonymous: bool,
anonymous_to_peers: bool,
depth: int,
thread_id: Optional[str] = None,
thread_id: str,
parent_id: Optional[str] = None,
) -> Any:
"""
Expand Down Expand Up @@ -1406,3 +1405,13 @@ def update_comment_and_get_updated_comment(
def delete_comment_by_id(comment_id: str) -> None:
"""Delete a comment by it's Id."""
Comment().delete(comment_id)


def get_thread_id_by_comment_id(parent_comment_id: str) -> str:
"""
The thread Id from the parent comment.
"""
parent_comment = Comment().get(parent_comment_id)
if parent_comment:
return parent_comment["comment_thread_id"]
raise ValueError("Comment doesn't have the thread.")
2 changes: 1 addition & 1 deletion forum/backends/mongodb/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def insert(
"""
date = datetime.now()
comment_data = {
"_id":str(ObjectId()),
"_id": str(ObjectId()),
"votes": self.get_votes_dict(up=[], down=[]),
"visible": visible,
"abuse_flaggers": abuse_flaggers or [],
Expand Down
8 changes: 0 additions & 8 deletions forum/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ def get_group_ids_from_params(params: dict[str, Any]) -> list[int]:
Raises:
ValueError: If both `group_id` and `group_ids` are specified in the parameters.
"""

if "group_id" in params and "group_ids" in params:
raise ValueError("Cannot specify both group_id and group_ids")
group_ids: str | list[str] = []
Expand Down Expand Up @@ -221,10 +220,3 @@ def get_sort_criteria(sort_key: str) -> Sequence[tuple[str, int]]:

class ForumV2RequestError(Exception):
pass


def make_aware(dt: datetime) -> datetime:
"""Make datetime timezone-aware."""
if dt.tzinfo is None:
dt = dt.replace(tzinfo=timezone.utc)
return dt
2 changes: 1 addition & 1 deletion forum/views/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
get_parent_comment,
update_comment,
)
from forum.utils import ForumV2RequestError
from forum.utils import ForumV2RequestError, str_to_bool


class CommentsAPIView(APIView):
Expand Down
1 change: 0 additions & 1 deletion forum/views/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
)
from forum.serializers.thread import ThreadSerializer
from forum.serializers.users import UserSerializer
from forum.utils import get_group_ids_from_params
from forum.utils import ForumV2RequestError

log = logging.getLogger(__name__)
Expand Down

0 comments on commit a93a8c1

Please sign in to comment.