Skip to content

Commit

Permalink
feat: add python native APIs from forum v2
Browse files Browse the repository at this point in the history
- migrate following native APIs i.e. mark_thread_as_read, create_subscription,
delete_subscription, update_thread_votes, update_comment_votes, delete_thread_vote,
delete_comment_vote, get_user_threads, retire_user, update_username,
get_user_course_stats, update_users_in_course, flag/unflag comment/threads
from forum v2 to edx-platform
- refactor some code
  • Loading branch information
Muhammad Faraz Maqsood committed Oct 2, 2024
1 parent 634af22 commit 78e6536
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
from openedx.core.djangoapps.django_comment_common.comment_client import models, settings

from .thread import Thread, _url_for_flag_abuse_thread, _url_for_unflag_abuse_thread
from .utils import CommentClientRequestError, perform_request
from .utils import CommentClientRequestError, get_course_key, perform_request
from forum import api as forum_api
from lms.djangoapps.discussion.toggles import is_forum_v2_enabled


class Comment(models.Model):
Expand Down Expand Up @@ -68,14 +70,20 @@ def flagAbuse(self, user, voteable):
url = _url_for_flag_abuse_comment(voteable.id)
else:
raise CommentClientRequestError("Can only flag/unflag threads or comments")
params = {'user_id': user.id}
response = perform_request(
'put',
url,
params,
metric_tags=self._metric_tags,
metric_action='comment.abuse.flagged'
)
if is_forum_v2_enabled(get_course_key(self.course_id)):
if voteable.type == 'thread':
response = forum_api.update_thread_flag(voteable.id, "flag", user.id)
else:
response = forum_api.update_comment_flag(voteable.id, "flag", user.id)
else:
params = {'user_id': user.id}
response = perform_request(
'put',
url,
params,
metric_tags=self._metric_tags,
metric_action='comment.abuse.flagged'
)
voteable._update_from_response(response)

def unFlagAbuse(self, user, voteable, removeAll):
Expand All @@ -85,18 +93,24 @@ def unFlagAbuse(self, user, voteable, removeAll):
url = _url_for_unflag_abuse_comment(voteable.id)
else:
raise CommentClientRequestError("Can flag/unflag for threads or comments")
params = {'user_id': user.id}

if removeAll:
params['all'] = True

response = perform_request(
'put',
url,
params,
metric_tags=self._metric_tags,
metric_action='comment.abuse.unflagged'
)
if is_forum_v2_enabled(get_course_key(self.course_id)):
if voteable.type == 'thread':
response = forum_api.update_thread_flag(voteable.id, "unflag", user.id, True if removeAll else False)
else:
response = forum_api.update_comment_flag(voteable.id, "unflag", user.id, True if removeAll else False)
else:
params = {'user_id': user.id}

if removeAll:
params['all'] = True

response = perform_request(
'put',
url,
params,
metric_tags=self._metric_tags,
metric_action='comment.abuse.unflagged'
)
voteable._update_from_response(response)

@property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,21 @@ def get_course_user_stats(course_key: CourseKey, params: Optional[Dict] = None)
"""
if params is None:
params = {}
url = f"{settings.PREFIX}/users/{course_key}/stats"
return perform_request(
'get',
url,
params,
metric_action='user.course_stats',
metric_tags=[
f"course_key:{course_key}",
"function:get_course_user_stats",
],
)
if is_forum_v2_enabled(course_key):
course_stats = forum_api.get_user_course_stats(str(course_key), **params)
else:
url = f"{settings.PREFIX}/users/{course_key}/stats"
course_stats = perform_request(
'get',
url,
params,
metric_action='user.course_stats',
metric_tags=[
f"course_key:{course_key}",
"function:get_course_user_stats",
],
)
return course_stats


@function_trace("update_course_users_stats")
Expand All @@ -105,13 +109,17 @@ def update_course_users_stats(course_key: CourseKey) -> Dict:
Returns:
dict: data returned by API. Contains count of users updated.
"""
url = f"{settings.PREFIX}/users/{course_key}/update_stats"
return perform_request(
'post',
url,
metric_action='user.update_course_stats',
metric_tags=[
f"course_key:{course_key}",
"function:update_course_users_stats",
],
)
if is_forum_v2_enabled(course_key):
course_stats = forum_api.update_users_in_course(str(course_key))
else:
url = f"{settings.PREFIX}/users/{course_key}/update_stats"
course_stats = perform_request(
'post',
url,
metric_action='user.update_course_stats',
metric_tags=[
f"course_key:{course_key}",
"function:update_course_users_stats",
],
)
return course_stats
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from opaque_keys.edx.keys import CourseKey

from .utils import CommentClientRequestError, extract, perform_request
from .utils import CommentClientRequestError, extract, perform_request, get_course_key
from forum import api as forum_api
from lms.djangoapps.discussion.toggles import is_forum_v2_enabled

Expand Down Expand Up @@ -74,10 +74,7 @@ def retrieve(self, *args, **kwargs):

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

def delete(self):
response = None
if course_id := self.attributes.get("course_id"):
if not isinstance(course_id, CourseKey):
course_id = CourseKey.from_string(course_id)
if is_forum_v2_enabled(course_id):
if is_forum_v2_enabled(get_course_key(self.attributes.get("course_id"))):
if self.type == "comment":
response = forum_api.delete_comment(self.attributes["id"])
if response is None:
Expand Down Expand Up @@ -218,10 +212,7 @@ def handle_update(self, params=None):
if params:
request_params.update(params)
response = None
if course_id := self.attributes.get("course_id"):
if not isinstance(course_id, CourseKey):
course_id = CourseKey.from_string(course_id)
if is_forum_v2_enabled(course_id):
if is_forum_v2_enabled(get_course_key(self.attributes.get("course_id"))):
if self.type == "comment":
response = self.handle_update_comment(request_params)
if response is None:
Expand Down Expand Up @@ -274,10 +265,7 @@ def perform_http_post_request(self):

def handle_create(self):
response = None
if course_id := self.attributes.get("course_id"):
if not isinstance(course_id, CourseKey):
course_id = CourseKey.from_string(course_id)
if is_forum_v2_enabled(course_id):
if is_forum_v2_enabled(get_course_key(self.attributes.get("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
Expand Up @@ -3,8 +3,6 @@
"""
import logging

from opaque_keys.edx.keys import CourseKey

from . import models, settings, utils
from forum import api as forum_api
from lms.djangoapps.discussion.toggles import is_forum_v2_enabled
Expand Down Expand Up @@ -38,9 +36,7 @@ def fetch(cls, thread_id, course_id, query_params):
utils.strip_blank(utils.strip_none(query_params))
)

if course_id and not isinstance(course_id, CourseKey):
course_id = CourseKey.from_string(course_id)
if is_forum_v2_enabled(course_id):
if is_forum_v2_enabled(utils.get_course_key(course_id)):
response = forum_api.get_thread_subscriptions(thread_id, params['page'], params['per_page'])
else:
response = utils.perform_request(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,33 +165,39 @@ def flagAbuse(self, user, voteable):
url = _url_for_flag_abuse_thread(voteable.id)
else:
raise utils.CommentClientRequestError("Can only flag/unflag threads or comments")
params = {'user_id': user.id}
response = utils.perform_request(
'put',
url,
params,
metric_action='thread.abuse.flagged',
metric_tags=self._metric_tags
)
if is_forum_v2_enabled(utils.get_course_key(self.course_id)):
response = forum_api.update_thread_flag(voteable.id, "flag", user.id)
else:
params = {'user_id': user.id}
response = utils.perform_request(
'put',
url,
params,
metric_action='thread.abuse.flagged',
metric_tags=self._metric_tags
)
voteable._update_from_response(response)

def unFlagAbuse(self, user, voteable, removeAll):
if voteable.type == 'thread':
url = _url_for_unflag_abuse_thread(voteable.id)
else:
raise utils.CommentClientRequestError("Can only flag/unflag for threads or comments")
params = {'user_id': user.id}
#if you're an admin, when you unflag, remove ALL flags
if removeAll:
params['all'] = True
if is_forum_v2_enabled(utils.get_course_key(self.course_id)):
response = forum_api.update_thread_flag(voteable.id, "unflag", user.id, True if removeAll else False)
else:
params = {'user_id': user.id}
#if you're an admin, when you unflag, remove ALL flags
if removeAll:
params['all'] = True

response = utils.perform_request(
'put',
url,
params,
metric_tags=self._metric_tags,
metric_action='thread.abuse.unflagged'
)
response = utils.perform_request(
'put',
url,
params,
metric_tags=self._metric_tags,
metric_action='thread.abuse.unflagged'
)
voteable._update_from_response(response)

def pin(self, user, thread_id):
Expand Down
Loading

0 comments on commit 78e6536

Please sign in to comment.