Skip to content

Commit

Permalink
feat: call python methods from forum v2
Browse files Browse the repository at this point in the history
- directly call python native APIs from forum v2 for pin, unpin thread,
commentables count_stats and get user's data by user_id
  • Loading branch information
Muhammad Faraz Maqsood authored and Muhammad Faraz Maqsood committed Sep 16, 2024
1 parent 6a63cfc commit b704528
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from edx_django_utils.monitoring import function_trace
from opaque_keys.edx.keys import CourseKey

from forum import api as forum_api
from openedx.core.djangoapps.django_comment_common.comment_client import settings
from openedx.core.djangoapps.django_comment_common.comment_client.utils import perform_request

Expand All @@ -29,17 +30,8 @@ def get_course_commentable_counts(course_key: CourseKey) -> Dict[str, Dict[str,
}
"""
url = f"{settings.PREFIX}/commentables/{course_key}/counts"
response = perform_request(
'get',
url,
metric_tags=[
f"course_key:{course_key}",
"function:get_course_commentable_counts",
],
metric_action='commentable_stats.retrieve',
)
return response
commentable_stats = forum_api.retrieve_commentables_stats(str(course_key))
return commentable_stats


@function_trace("get_course_user_stats")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from eventtracking import tracker

from . import models, settings, utils
from forum import api as forum_api

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -193,27 +194,11 @@ def unFlagAbuse(self, user, voteable, removeAll):
voteable._update_from_response(response)

def pin(self, user, thread_id):
url = _url_for_pin_thread(thread_id)
params = {'user_id': user.id}
response = utils.perform_request(
'put',
url,
params,
metric_tags=self._metric_tags,
metric_action='thread.pin'
)
self._update_from_response(response)
thread_data = forum_api.pin_thread(user.id, thread_id)
self._update_from_response(thread_data)

def un_pin(self, user, thread_id):
url = _url_for_un_pin_thread(thread_id)
params = {'user_id': user.id}
response = utils.perform_request(
'put',
url,
params,
metric_tags=self._metric_tags,
metric_action='thread.unpin'
)
response = forum_api.unpin_thread(user.id, thread_id)
self._update_from_response(response)


Expand All @@ -223,11 +208,3 @@ def _url_for_flag_abuse_thread(thread_id):

def _url_for_unflag_abuse_thread(thread_id):
return f"{settings.PREFIX}/threads/{thread_id}/abuse_unflag"


def _url_for_pin_thread(thread_id):
return f"{settings.PREFIX}/threads/{thread_id}/pin"


def _url_for_un_pin_thread(thread_id):
return f"{settings.PREFIX}/threads/{thread_id}/unpin"
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@


from . import models, settings, utils
from forum import api as forum_api
from forum.utils import ForumV2RequestError


class User(models.Model):
Expand Down Expand Up @@ -141,35 +143,13 @@ def subscribed_threads(self, query_params=None):
)

def _retrieve(self, *args, **kwargs):
url = self.url(action='get', params=self.attributes)
retrieve_params = self.default_retrieve_params.copy()
retrieve_params.update(kwargs)
if self.attributes.get('course_id'):
retrieve_params['course_id'] = str(self.course_id)
if self.attributes.get('group_id'):
retrieve_params['group_id'] = self.group_id
try:
response = utils.perform_request(
'get',
url,
retrieve_params,
metric_action='model.retrieve',
metric_tags=self._metric_tags,
)
except utils.CommentClientRequestError as e:
if e.status_code == 404:
# attempt to gracefully recover from a previous failure
# to sync this user to the comments service.
self.save()
response = utils.perform_request(
'get',
url,
retrieve_params,
metric_action='model.retrieve',
metric_tags=self._metric_tags,
)
else:
raise
response = forum_api.retrieve_user(self.attributes["id"], retrieve_params)
self._update_from_response(response)

def retire(self, retired_username):
Expand Down

0 comments on commit b704528

Please sign in to comment.