Skip to content

Commit e1a117b

Browse files
Taimoor  AhmedTaimoor  Ahmed
Taimoor Ahmed
authored and
Taimoor Ahmed
committed
fix: discussion xblock not compatible with forum v2
fix all endpoints that were currently breaking with the discussion xblock.
1 parent 1d0c72a commit e1a117b

File tree

7 files changed

+37
-33
lines changed

7 files changed

+37
-33
lines changed

lms/djangoapps/discussion/django_comment_client/base/tests.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ def test_update_comment_basic(self, mock_is_forum_v2_enabled, mock_request):
812812
headers=ANY,
813813
params=ANY,
814814
timeout=ANY,
815-
data={"body": updated_body}
815+
data={"body": updated_body, "course_id": str(self.course_id)}
816816
)
817817

818818
def test_flag_thread_open(self, mock_is_forum_v2_enabled, mock_request):

lms/djangoapps/discussion/django_comment_client/base/views.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ def _create_comment(request, course_key, thread_id=None, parent_id=None):
673673
parent_id=parent_id,
674674
body=sanitize_body(post["body"]),
675675
)
676-
comment.save()
676+
comment.save(params={"course_id": str(course_key)})
677677

678678
comment_created.send(sender=None, user=user, post=comment)
679679

@@ -736,7 +736,7 @@ def update_comment(request, course_id, comment_id):
736736
if 'body' not in request.POST or not request.POST['body'].strip():
737737
return JsonError(_("Body can't be empty"))
738738
comment.body = sanitize_body(request.POST["body"])
739-
comment.save()
739+
comment.save(params={"course_id": course_id})
740740

741741
comment_edited.send(sender=None, user=request.user, post=comment)
742742

@@ -762,7 +762,7 @@ def endorse_comment(request, course_id, comment_id):
762762
endorsed = request.POST.get('endorsed', 'false').lower() == 'true'
763763
comment.endorsed = endorsed
764764
comment.endorsement_user_id = user.id
765-
comment.save()
765+
comment.save(params={"course_id": course_id})
766766
comment_endorsed.send(sender=None, user=user, post=comment)
767767
track_forum_response_mark_event(request, course, comment, endorsed)
768768
return JsonResponse(prepare_content(comment.to_dict(), course_key))
@@ -814,7 +814,7 @@ def delete_comment(request, course_id, comment_id):
814814
course_key = CourseKey.from_string(course_id)
815815
course = get_course_with_access(request.user, 'load', course_key)
816816
comment = cc.Comment.find(comment_id)
817-
comment.delete()
817+
comment.delete(course_id=course_id)
818818
comment_deleted.send(sender=None, user=request.user, post=comment)
819819
track_comment_deleted_event(request, course, comment)
820820
return JsonResponse(prepare_content(comment.to_dict(), course_key))
@@ -828,12 +828,12 @@ def _vote_or_unvote(request, course_id, obj, value='up', undo_vote=False):
828828
course = get_course_with_access(request.user, 'load', course_key)
829829
user = cc.User.from_django_user(request.user)
830830
if undo_vote:
831-
user.unvote(obj)
831+
user.unvote(obj, course_id)
832832
# TODO(smarnach): Determine the value of the vote that is undone. Currently, you can
833833
# only cast upvotes in the user interface, so it is assumed that the vote value is 'up'.
834834
# (People could theoretically downvote by handcrafting AJAX requests.)
835835
else:
836-
user.vote(obj, value)
836+
user.vote(obj, value, course_id)
837837
thread_voted.send(sender=None, user=request.user, post=obj)
838838
track_voted_event(request, course, obj, value, undo_vote)
839839
return JsonResponse(prepare_content(obj.to_dict(), course_key))
@@ -899,7 +899,7 @@ def flag_abuse_for_thread(request, course_id, thread_id):
899899
user = cc.User.from_django_user(request.user)
900900
course = get_course_by_id(course_key)
901901
thread = cc.Thread.find(thread_id)
902-
thread.flagAbuse(user, thread)
902+
thread.flagAbuse(user, thread, course_id)
903903
track_discussion_reported_event(request, course, thread)
904904
thread_flagged.send(sender='flag_abuse_for_thread', user=request.user, post=thread)
905905
return JsonResponse(prepare_content(thread.to_dict(), course_key))
@@ -921,7 +921,7 @@ def un_flag_abuse_for_thread(request, course_id, thread_id):
921921
has_permission(request.user, 'openclose_thread', course_key) or
922922
has_access(request.user, 'staff', course)
923923
)
924-
thread.unFlagAbuse(user, thread, remove_all)
924+
thread.unFlagAbuse(user, thread, remove_all, course_id)
925925
track_discussion_unreported_event(request, course, thread)
926926
return JsonResponse(prepare_content(thread.to_dict(), course_key))
927927

@@ -938,7 +938,7 @@ def flag_abuse_for_comment(request, course_id, comment_id):
938938
user = cc.User.from_django_user(request.user)
939939
course = get_course_by_id(course_key)
940940
comment = cc.Comment.find(comment_id)
941-
comment.flagAbuse(user, comment)
941+
comment.flagAbuse(user, comment, course_id)
942942
track_discussion_reported_event(request, course, comment)
943943
comment_flagged.send(sender='flag_abuse_for_comment', user=request.user, post=comment)
944944
return JsonResponse(prepare_content(comment.to_dict(), course_key))
@@ -960,7 +960,7 @@ def un_flag_abuse_for_comment(request, course_id, comment_id):
960960
has_access(request.user, 'staff', course)
961961
)
962962
comment = cc.Comment.find(comment_id)
963-
comment.unFlagAbuse(user, comment, remove_all)
963+
comment.unFlagAbuse(user, comment, remove_all, course_id)
964964
track_discussion_unreported_event(request, course, comment)
965965
return JsonResponse(prepare_content(comment.to_dict(), course_key))
966966

@@ -1005,7 +1005,7 @@ def follow_thread(request, course_id, thread_id): # lint-amnesty, pylint: disab
10051005
course_key = CourseKey.from_string(course_id)
10061006
course = get_course_by_id(course_key)
10071007
thread = cc.Thread.find(thread_id)
1008-
user.follow(thread)
1008+
user.follow(thread, course_id=course_id)
10091009
thread_followed.send(sender=None, user=request.user, post=thread)
10101010
track_thread_followed_event(request, course, thread, True)
10111011
return JsonResponse({})
@@ -1037,7 +1037,7 @@ def unfollow_thread(request, course_id, thread_id): # lint-amnesty, pylint: dis
10371037
course = get_course_by_id(course_key)
10381038
user = cc.User.from_django_user(request.user)
10391039
thread = cc.Thread.find(thread_id)
1040-
user.unfollow(thread)
1040+
user.unfollow(thread, course_id=course_id)
10411041
thread_unfollowed.send(sender=None, user=request.user, post=thread)
10421042
track_thread_followed_event(request, course, thread, False)
10431043
return JsonResponse({})

lms/djangoapps/discussion/views.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def get_threads(request, course, user_info, discussion_id=None, per_page=THREADS
147147
# If the user clicked a sort key, update their default sort key
148148
cc_user = cc.User.from_django_user(request.user)
149149
cc_user.default_sort_key = request.GET.get('sort_key')
150-
cc_user.save()
150+
cc_user.save(params={"course_id": course.id})
151151

152152
#there are 2 dimensions to consider when executing a search with respect to group id
153153
#is user a moderator

openedx/core/djangoapps/django_comment_common/comment_client/comment.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,23 @@ def url(cls, action, params=None):
6363
else:
6464
return super().url(action, params)
6565

66-
def flagAbuse(self, user, voteable):
66+
def flagAbuse(self, user, voteable, course_id=None):
6767
if voteable.type == 'thread':
6868
url = _url_for_flag_abuse_thread(voteable.id)
6969
elif voteable.type == 'comment':
7070
url = _url_for_flag_abuse_comment(voteable.id)
7171
else:
7272
raise CommentClientRequestError("Can only flag/unflag threads or comments")
73-
course_key = get_course_key(self.attributes.get("course_id"))
73+
course_key = get_course_key(self.attributes.get("course_id") or course_id)
7474
if is_forum_v2_enabled(course_key):
7575
if voteable.type == 'thread':
76-
response = forum_api.update_thread_flag(voteable.id, "flag", user.id, str(course_key))
76+
response = forum_api.update_thread_flag(
77+
voteable.id, "flag", user_id=user.id, course_id=str(course_key)
78+
)
7779
else:
78-
response = forum_api.update_comment_flag(voteable.id, "flag", user.id, str(course_key))
80+
response = forum_api.update_comment_flag(
81+
voteable.id, "flag", user_id=user.id, course_id=str(course_key)
82+
)
7983
else:
8084
params = {'user_id': user.id}
8185
response = perform_request(
@@ -87,14 +91,14 @@ def flagAbuse(self, user, voteable):
8791
)
8892
voteable._update_from_response(response)
8993

90-
def unFlagAbuse(self, user, voteable, removeAll):
94+
def unFlagAbuse(self, user, voteable, removeAll, course_id=None):
9195
if voteable.type == 'thread':
9296
url = _url_for_unflag_abuse_thread(voteable.id)
9397
elif voteable.type == 'comment':
9498
url = _url_for_unflag_abuse_comment(voteable.id)
9599
else:
96100
raise CommentClientRequestError("Can flag/unflag for threads or comments")
97-
course_key = get_course_key(self.attributes.get("course_id"))
101+
course_key = get_course_key(self.attributes.get("course_id") or course_id)
98102
if is_forum_v2_enabled(course_key):
99103
if voteable.type == "thread":
100104
response = forum_api.update_thread_flag(

openedx/core/djangoapps/django_comment_common/comment_client/models.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ def save(self, params=None):
175175
self._update_from_response(response)
176176
self.after_save(self)
177177

178-
def delete(self):
179-
course_key = get_course_key(self.attributes.get("course_id"))
178+
def delete(self, course_id=None):
179+
course_key = get_course_key(self.attributes.get("course_id") or course_id)
180180
if is_forum_v2_enabled(course_key):
181181
response = None
182182
if self.type == "comment":

openedx/core/djangoapps/django_comment_common/comment_client/thread.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,14 @@ def _retrieve(self, *args, **kwargs):
196196
)
197197
self._update_from_response(response)
198198

199-
def flagAbuse(self, user, voteable):
199+
def flagAbuse(self, user, voteable, course_id=None):
200200
if voteable.type == 'thread':
201201
url = _url_for_flag_abuse_thread(voteable.id)
202202
else:
203203
raise utils.CommentClientRequestError("Can only flag/unflag threads or comments")
204-
course_key = utils.get_course_key(self.attributes.get("course_id"))
204+
course_key = utils.get_course_key(self.attributes.get("course_id") or course_id)
205205
if is_forum_v2_enabled(course_key):
206-
response = forum_api.update_thread_flag(voteable.id, "flag", user.id, str(course_key))
206+
response = forum_api.update_thread_flag(voteable.id, "flag", user_id=user.id, course_id=str(course_key))
207207
else:
208208
params = {'user_id': user.id}
209209
response = utils.perform_request(
@@ -215,7 +215,7 @@ def flagAbuse(self, user, voteable):
215215
)
216216
voteable._update_from_response(response)
217217

218-
def unFlagAbuse(self, user, voteable, removeAll):
218+
def unFlagAbuse(self, user, voteable, removeAll, course_id=None):
219219
if voteable.type == 'thread':
220220
url = _url_for_unflag_abuse_thread(voteable.id)
221221
else:

openedx/core/djangoapps/django_comment_common/comment_client/user.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ def read(self, source):
5050
metric_tags=self._metric_tags + [f'target.type:{source.type}'],
5151
)
5252

53-
def follow(self, source):
54-
course_key = utils.get_course_key(self.attributes.get("course_id"))
53+
def follow(self, source, course_id=None):
54+
course_key = utils.get_course_key(self.attributes.get("course_id") or course_id)
5555
if is_forum_v2_enabled(course_key):
5656
forum_api.create_subscription(
5757
user_id=self.id,
@@ -68,8 +68,8 @@ def follow(self, source):
6868
metric_tags=self._metric_tags + [f'target.type:{source.type}'],
6969
)
7070

71-
def unfollow(self, source):
72-
course_key = utils.get_course_key(self.attributes.get("course_id"))
71+
def unfollow(self, source, course_id=None):
72+
course_key = utils.get_course_key(self.attributes.get("course_id") or course_id)
7373
if is_forum_v2_enabled(course_key):
7474
forum_api.delete_subscription(
7575
user_id=self.id,
@@ -86,14 +86,14 @@ def unfollow(self, source):
8686
metric_tags=self._metric_tags + [f'target.type:{source.type}'],
8787
)
8888

89-
def vote(self, voteable, value):
89+
def vote(self, voteable, value, course_id=None):
9090
if voteable.type == 'thread':
9191
url = _url_for_vote_thread(voteable.id)
9292
elif voteable.type == 'comment':
9393
url = _url_for_vote_comment(voteable.id)
9494
else:
9595
raise utils.CommentClientRequestError("Can only vote / unvote for threads or comments")
96-
course_key = utils.get_course_key(self.attributes.get("course_id"))
96+
course_key = utils.get_course_key(self.attributes.get("course_id") or course_id)
9797
if is_forum_v2_enabled(course_key):
9898
if voteable.type == 'thread':
9999
response = forum_api.update_thread_votes(
@@ -120,7 +120,7 @@ def vote(self, voteable, value):
120120
)
121121
voteable._update_from_response(response)
122122

123-
def unvote(self, voteable):
123+
def unvote(self, voteable, course_id=None):
124124
if voteable.type == 'thread':
125125
url = _url_for_vote_thread(voteable.id)
126126
elif voteable.type == 'comment':

0 commit comments

Comments
 (0)