Skip to content

Commit

Permalink
feat: migrate user active_thread api
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammad Faraz Maqsood committed Oct 2, 2024
1 parent 8cdc9fc commit 9440a91
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import logging

from opaque_keys.edx.keys import CourseKey

from .utils import CommentClientRequestError, extract, perform_request
from forum import api as forum_api
from lms.djangoapps.discussion.toggles import is_forum_v2_enabled
Expand Down Expand Up @@ -72,7 +74,9 @@ def retrieve(self, *args, **kwargs):

def _retrieve(self, *args, **kwargs):
response = None
if is_forum_v2_enabled(self.attributes.get("course_id")):
if course_id := self.attributes.get("course_id"):
course_id = CourseKey.from_string(course_id)
if is_forum_v2_enabled(course_id):
if self.type == "comment":
response = forum_api.get_parent_comment(self.attributes["id"])
if response is None:
Expand Down Expand Up @@ -168,7 +172,9 @@ def save(self, params=None):

def delete(self):
response = None
if is_forum_v2_enabled(self.attributes.get("course_id")):
if course_id := self.attributes.get("course_id"):
course_id = CourseKey.from_string(course_id)
if is_forum_v2_enabled(course_id):
if self.type == "comment":
response = forum_api.delete_comment(self.attributes["id"])
if response is None:
Expand Down Expand Up @@ -210,7 +216,9 @@ def handle_update(self, params=None):
if params:
request_params.update(params)
response = None
if is_forum_v2_enabled(request_params.get("course_id")):
if course_id := self.attributes.get("course_id"):
course_id = CourseKey.from_string(course_id)
if is_forum_v2_enabled(course_id):
if self.type == "comment":
response = self.handle_update_comment(request_params)
if response is None:
Expand Down Expand Up @@ -263,7 +271,9 @@ def perform_http_post_request(self):

def handle_create(self):
response = None
if is_forum_v2_enabled(self.attributes.get("course_id")):
if course_id := self.attributes.get("course_id"):
course_id = CourseKey.from_string(course_id)
if is_forum_v2_enabled(course_id):
if self.type == "comment":
response = self.handle_create_comment()
if response is None:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# pylint: disable=missing-docstring,protected-access
""" User model wrapper for comment service"""

from opaque_keys.edx.keys import CourseKey

from . import models, settings, utils
from forum import api as forum_api
from forum.utils import str_to_bool
from lms.djangoapps.discussion.toggles import is_forum_v2_enabled


Expand Down Expand Up @@ -107,14 +109,28 @@ def active_threads(self, query_params=None):
url = _url_for_user_active_threads(self.id)
params = {'course_id': str(self.course_id)}
params.update(query_params)
response = utils.perform_request(
'get',
url,
params,
metric_action='user.active_threads',
metric_tags=self._metric_tags,
paged_results=True,
)
if user_id := params.get("user_id"):
params["user_id"] = str(user_id)
if page := params.get("page"):
params["page"] = int(page)
if per_page := params.get("per_page"):
params["per_page"] = int(per_page)
if count_flagged := params.get("count_flagged", False):
params["count_flagged"] = str_to_bool(count_flagged)

if course_id := self.attributes.get("course_id"):
course_id = CourseKey.from_string(course_id)
if is_forum_v2_enabled(course_id):
response = forum_api.get_user_active_threads(**params)
else:
response = utils.perform_request(
'get',
url,
params,
metric_action='user.active_threads',
metric_tags=self._metric_tags,
paged_results=True,
)
return response.get('collection', []), response.get('page', 1), response.get('num_pages', 1)

def subscribed_threads(self, query_params=None):
Expand Down

0 comments on commit 9440a91

Please sign in to comment.