diff --git a/openedx/core/djangoapps/django_comment_common/comment_client/comment.py b/openedx/core/djangoapps/django_comment_common/comment_client/comment.py index e9ee12eb786..1d276cd4a62 100644 --- a/openedx/core/djangoapps/django_comment_common/comment_client/comment.py +++ b/openedx/core/djangoapps/django_comment_common/comment_client/comment.py @@ -70,11 +70,12 @@ def flagAbuse(self, user, voteable): url = _url_for_flag_abuse_comment(voteable.id) else: raise CommentClientRequestError("Can only flag/unflag threads or comments") - if is_forum_v2_enabled(get_course_key(self.attributes.get("course_id"))): + course_key = get_course_key(self.attributes.get("course_id")) + if is_forum_v2_enabled(course_key): if voteable.type == 'thread': - response = forum_api.update_thread_flag(voteable.id, "flag", user.id) + response = forum_api.update_thread_flag(voteable.id, "flag", user.id, str(course_key)) else: - response = forum_api.update_comment_flag(voteable.id, "flag", user.id) + response = forum_api.update_comment_flag(voteable.id, "flag", user.id, str(course_key)) else: params = {'user_id': user.id} response = perform_request( @@ -93,11 +94,16 @@ def unFlagAbuse(self, user, voteable, removeAll): url = _url_for_unflag_abuse_comment(voteable.id) else: raise CommentClientRequestError("Can flag/unflag for threads or comments") - if is_forum_v2_enabled(get_course_key(self.attributes.get("course_id"))): - if voteable.type == 'thread': - response = forum_api.update_thread_flag(voteable.id, "unflag", user.id, bool(removeAll)) + course_key = get_course_key(self.attributes.get("course_id")) + if is_forum_v2_enabled(course_key): + if voteable.type == "thread": + response = forum_api.update_thread_flag( + voteable.id, "unflag", user.id, bool(removeAll), str(course_key) + ) else: - response = forum_api.update_comment_flag(voteable.id, "unflag", user.id, bool(removeAll)) + response = forum_api.update_comment_flag( + voteable.id, "unflag", user.id, bool(removeAll), str(course_key) + ) else: params = {'user_id': user.id} diff --git a/openedx/core/djangoapps/django_comment_common/comment_client/models.py b/openedx/core/djangoapps/django_comment_common/comment_client/models.py index ef40f39c15f..ecb13024d3c 100644 --- a/openedx/core/djangoapps/django_comment_common/comment_client/models.py +++ b/openedx/core/djangoapps/django_comment_common/comment_client/models.py @@ -78,7 +78,7 @@ def _retrieve(self, *args, **kwargs): response = None if is_forum_v2_enabled(course_key): if self.type == "comment": - response = forum_api.get_parent_comment(self.attributes["id"]) + response = forum_api.get_parent_comment(self.attributes["id"], str(course_key)) if response is None: raise CommentClientRequestError("Forum v2 API call is missing") else: @@ -173,12 +173,13 @@ def save(self, params=None): self.after_save(self) def delete(self): - if is_forum_v2_enabled(get_course_key(self.attributes.get("course_id"))): + course_key = get_course_key(self.attributes.get("course_id")) + if is_forum_v2_enabled(course_key): response = None if self.type == "comment": - response = forum_api.delete_comment(self.attributes["id"]) + response = forum_api.delete_comment(self.attributes["id"], str(course_key)) elif self.type == "thread": - response = forum_api.delete_thread(self.attributes["id"]) + response = forum_api.delete_thread(self.attributes["id"], str(course_key)) if response is None: raise CommentClientRequestError("Forum v2 API call is missing") else: @@ -220,21 +221,22 @@ def handle_update(self, params=None): if params: request_params.update(params) course_id = self.attributes.get("course_id") or request_params.get("course_id") - if is_forum_v2_enabled(get_course_key(course_id)): + course_key = get_course_key(course_id) + if is_forum_v2_enabled(course_key): response = None if self.type == "comment": - response = self.handle_update_comment(request_params) + response = self.handle_update_comment(request_params, str(course_key)) elif self.type == "thread": - response = self.handle_update_thread(request_params) + response = self.handle_update_thread(request_params, str(course_key)) elif self.type == "user": - response = self.handle_update_user(request_params) + response = self.handle_update_user(request_params, str(course_key)) if response is None: raise CommentClientRequestError("Forum v2 API call is missing") else: response = self.perform_http_put_request(request_params) return response - def handle_update_user(self, request_params): + def handle_update_user(self, request_params, course_id): try: username = request_params["username"] external_id = str(request_params["external_id"]) @@ -243,10 +245,11 @@ def handle_update_user(self, request_params): response = forum_api.update_user( external_id, username, + course_id, ) return response - def handle_update_comment(self, request_params): + def handle_update_comment(self, request_params, course_id): try: body = request_params["body"] course_id = str(request_params["course_id"]) @@ -265,10 +268,11 @@ def handle_update_comment(self, request_params): request_params.get("editing_user_id"), request_params.get("edit_reason_code"), request_params.get("endorsement_user_id"), + course_id, ) return response - def handle_update_thread(self, request_params): + def handle_update_thread(self, request_params, course_id): response = forum_api.update_thread( self.attributes["id"], request_params.get("title"), @@ -286,6 +290,7 @@ def handle_update_thread(self, request_params): request_params.get("close_reason_code"), request_params.get("closing_user_id"), request_params.get("endorsed"), + course_id, ) return response @@ -313,24 +318,25 @@ def perform_http_post_request(self): def handle_create(self, params=None): course_id = self.attributes.get("course_id") or params.get("course_id") - if is_forum_v2_enabled(get_course_key(course_id)): + course_key = get_course_key(course_id) + if is_forum_v2_enabled(course_key): response = None if self.type == "comment": - response = self.handle_create_comment() + response = self.handle_create_comment(str(course_key)) elif self.type == "thread": - response = self.handle_create_thread() + response = self.handle_create_thread(str(course_key)) if response is None: raise CommentClientRequestError("Forum v2 API call is missing") else: response = self.perform_http_post_request() return response - def handle_create_comment(self): + def handle_create_comment(self, course_id): request_data = self.initializable_attributes() try: body = request_data["body"] user_id = request_data["user_id"] - course_id = str(request_data["course_id"]) + course_id = course_id or str(request_data["course_id"]) except KeyError as e: raise e if parent_id := self.attributes.get("parent_id"): @@ -353,13 +359,13 @@ def handle_create_comment(self): ) return response - def handle_create_thread(self): + def handle_create_thread(self, course_id): request_data = self.initializable_attributes() try: title = request_data["title"] body = request_data["body"] user_id = str(request_data["user_id"]) - course_id = str(request_data["course_id"]) + course_id = course_id or str(request_data["course_id"]) except KeyError as e: raise e response = forum_api.create_thread( diff --git a/openedx/core/djangoapps/django_comment_common/comment_client/subscriptions.py b/openedx/core/djangoapps/django_comment_common/comment_client/subscriptions.py index 84c830ba613..8f9bc06340c 100644 --- a/openedx/core/djangoapps/django_comment_common/comment_client/subscriptions.py +++ b/openedx/core/djangoapps/django_comment_common/comment_client/subscriptions.py @@ -35,9 +35,11 @@ def fetch(cls, thread_id, course_id, query_params): params.update( utils.strip_blank(utils.strip_none(query_params)) ) - - if is_forum_v2_enabled(utils.get_course_key(course_id)): - response = forum_api.get_thread_subscriptions(thread_id, params['page'], params['per_page']) + course_key = utils.get_course_key(course_id) + if is_forum_v2_enabled(course_key): + response = forum_api.get_thread_subscriptions( + thread_id, params["page"], params["per_page"], str(course_key) + ) else: response = utils.perform_request( 'get', diff --git a/openedx/core/djangoapps/django_comment_common/comment_client/thread.py b/openedx/core/djangoapps/django_comment_common/comment_client/thread.py index f5525fcc8c9..69d70e1f932 100644 --- a/openedx/core/djangoapps/django_comment_common/comment_client/thread.py +++ b/openedx/core/djangoapps/django_comment_common/comment_client/thread.py @@ -79,6 +79,8 @@ def search(cls, query_params): search_params.pop('commentable_id', None) response = forum_api.search_threads(**search_params) else: + if not params.get("course_id"): + params = query_params['course_id'] response = forum_api.get_user_threads(**params) else: response = utils.perform_request( @@ -178,7 +180,7 @@ def _retrieve(self, *args, **kwargs): if is_forum_v2_enabled(course_key): if user_id := request_params.get('user_id'): request_params['user_id'] = str(user_id) - response = forum_api.get_thread(self.id, request_params) + response = forum_api.get_thread(self.id, request_params, str(course_key)) else: response = utils.perform_request( 'get', @@ -194,8 +196,9 @@ def flagAbuse(self, user, voteable): url = _url_for_flag_abuse_thread(voteable.id) else: raise utils.CommentClientRequestError("Can only flag/unflag threads or comments") - if is_forum_v2_enabled(utils.get_course_key(self.attributes.get("course_id"))): - response = forum_api.update_thread_flag(voteable.id, "flag", user.id) + course_key = utils.get_course_key(self.attributes.get("course_id")) + if is_forum_v2_enabled(course_key): + response = forum_api.update_thread_flag(voteable.id, "flag", user.id, str(course_key)) else: params = {'user_id': user.id} response = utils.perform_request( @@ -212,8 +215,9 @@ def unFlagAbuse(self, user, voteable, removeAll): url = _url_for_unflag_abuse_thread(voteable.id) else: raise utils.CommentClientRequestError("Can only flag/unflag for threads or comments") - if is_forum_v2_enabled(utils.get_course_key(self.attributes.get("course_id"))): - response = forum_api.update_thread_flag(voteable.id, "unflag", user.id, bool(removeAll)) + course_key = utils.get_course_key(self.attributes.get("course_id")) + if is_forum_v2_enabled(course_key): + response = forum_api.update_thread_flag(voteable.id, "unflag", user.id, bool(removeAll), str(course_key)) else: params = {'user_id': user.id} #if you're an admin, when you unflag, remove ALL flags @@ -230,8 +234,9 @@ def unFlagAbuse(self, user, voteable, removeAll): voteable._update_from_response(response) def pin(self, user, thread_id): - if is_forum_v2_enabled(utils.get_course_key(self.attributes.get("course_id"))): - response = forum_api.pin_thread(user.id, thread_id) + course_key = utils.get_course_key(self.attributes.get("course_id")) + if is_forum_v2_enabled(course_key): + response = forum_api.pin_thread(user.id, thread_id, str(course_key)) else: url = _url_for_pin_thread(thread_id) params = {'user_id': user.id} @@ -245,8 +250,9 @@ def pin(self, user, thread_id): self._update_from_response(response) def un_pin(self, user, thread_id): - if is_forum_v2_enabled(utils.get_course_key(self.attributes.get("course_id"))): - response = forum_api.unpin_thread(user.id, thread_id) + course_key = utils.get_course_key(self.attributes.get("course_id")) + if is_forum_v2_enabled(course_key): + response = forum_api.unpin_thread(user.id, thread_id, str(course_key)) else: url = _url_for_un_pin_thread(thread_id) params = {'user_id': user.id} diff --git a/openedx/core/djangoapps/django_comment_common/comment_client/user.py b/openedx/core/djangoapps/django_comment_common/comment_client/user.py index 206a08a91f9..ee9274c6206 100644 --- a/openedx/core/djangoapps/django_comment_common/comment_client/user.py +++ b/openedx/core/djangoapps/django_comment_common/comment_client/user.py @@ -55,7 +55,7 @@ def read(self, source): def follow(self, source): course_key = utils.get_course_key(self.attributes.get("course_id")) if is_forum_v2_enabled(course_key): - forum_api.create_subscription(self.id, source.id) + forum_api.create_subscription(self.id, source.id, str(course_key)) else: params = {'source_type': source.type, 'source_id': source.id} utils.perform_request( @@ -69,7 +69,7 @@ def follow(self, source): def unfollow(self, source): course_key = utils.get_course_key(self.attributes.get("course_id")) if is_forum_v2_enabled(course_key): - forum_api.delete_subscription(self.id, source.id) + forum_api.delete_subscription(self.id, source.id, str(course_key)) else: params = {'source_type': source.type, 'source_id': source.id} utils.perform_request( @@ -90,9 +90,9 @@ def vote(self, voteable, value): course_key = utils.get_course_key(self.attributes.get("course_id")) if is_forum_v2_enabled(course_key): if voteable.type == 'thread': - response = forum_api.update_thread_votes(voteable.id, self.id, value) + response = forum_api.update_thread_votes(voteable.id, self.id, value, str(course_key)) else: - response = forum_api.update_comment_votes(voteable.id, self.id, value) + response = forum_api.update_comment_votes(voteable.id, self.id, value, str(course_key)) else: params = {'user_id': self.id, 'value': value} response = utils.perform_request( @@ -114,9 +114,9 @@ def unvote(self, voteable): course_key = utils.get_course_key(self.attributes.get("course_id")) if is_forum_v2_enabled(course_key): if voteable.type == 'thread': - response = forum_api.delete_thread_vote(voteable.id, self.id) + response = forum_api.delete_thread_vote(voteable.id, self.id, str(course_key)) else: - response = forum_api.delete_comment_vote(voteable.id, self.id) + response = forum_api.delete_comment_vote(voteable.id, self.id, str(course_key)) else: params = {'user_id': self.id} response = utils.perform_request( @@ -146,6 +146,8 @@ def active_threads(self, query_params=None): params["per_page"] = int(per_page) if count_flagged := params.get("count_flagged", False): params["count_flagged"] = str_to_bool(count_flagged) + if not params.get("course_id"): + params["course_id"] = str(course_key) response = forum_api.get_user_active_threads(**params) else: response = utils.perform_request( @@ -178,6 +180,8 @@ def subscribed_threads(self, query_params=None): params["per_page"] = int(per_page) if count_flagged := params.get("count_flagged", False): params["count_flagged"] = str_to_bool(count_flagged) + if not params.get("course_id"): + params["course_id"] = str(course_key) response = forum_api.get_user_threads(**params) else: response = utils.perform_request( @@ -206,6 +210,8 @@ def _retrieve(self, *args, **kwargs): retrieve_params['group_id'] = self.attributes["group_id"] course_key = utils.get_course_key(course_id) if is_forum_v2_enabled(course_key): + if not retrieve_params.get("course_id"): + retrieve_params["course_id"] = str(course_key) try: response = forum_api.get_user(self.attributes["id"], retrieve_params) except ForumV2RequestError as e: @@ -239,7 +245,7 @@ def _retrieve(self, *args, **kwargs): def retire(self, retired_username): course_key = utils.get_course_key(self.attributes.get("course_id")) if is_forum_v2_enabled(course_key): - forum_api.retire_user(self.id, retired_username) + forum_api.retire_user(self.id, retired_username, str(course_key)) else: url = _url_for_retire(self.id) params = {'retired_username': retired_username} @@ -255,7 +261,7 @@ def retire(self, retired_username): def replace_username(self, new_username): course_key = utils.get_course_key(self.attributes.get("course_id")) if is_forum_v2_enabled(course_key): - forum_api.update_username(self.id, new_username) + forum_api.update_username(self.id, new_username, str(course_key)) else: url = _url_for_username_replacement(self.id) params = {"new_username": new_username}