Skip to content

Commit

Permalink
feat: pass course_id to native APIs
Browse files Browse the repository at this point in the history
course_id is needed to be used in second coursewaffle flag
  • Loading branch information
Muhammad Faraz Maqsood committed Oct 10, 2024
1 parent 5ef8196 commit 5aa42da
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"])
Expand All @@ -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"])
Expand All @@ -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"),
Expand All @@ -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

Expand Down Expand Up @@ -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"):
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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',
Expand All @@ -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(
Expand All @@ -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
Expand All @@ -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}
Expand All @@ -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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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}
Expand All @@ -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}
Expand Down

0 comments on commit 5aa42da

Please sign in to comment.