From 4cc9bfb6cf55e7bcd32fe9f55f7c9eb86a4bb10c Mon Sep 17 00:00:00 2001 From: SaadYousaf Date: Fri, 17 Feb 2023 14:27:46 +0500 Subject: [PATCH] fix: update response for marked events for discussion content --- .../django_comment_client/base/views.py | 37 +++++-------------- .../rest_api/tests/test_serializers.py | 9 ++++- 2 files changed, 17 insertions(+), 29 deletions(-) diff --git a/lms/djangoapps/discussion/django_comment_client/base/views.py b/lms/djangoapps/discussion/django_comment_client/base/views.py index f7036041b3..f0a7f65017 100644 --- a/lms/djangoapps/discussion/django_comment_client/base/views.py +++ b/lms/djangoapps/discussion/django_comment_client/base/views.py @@ -19,6 +19,7 @@ from opaque_keys.edx.keys import CourseKey import lms.djangoapps.discussion.django_comment_client.settings as cc_settings import openedx.core.djangoapps.django_comment_common.comment_client as cc +from common.djangoapps.student.roles import GlobalStaff from common.djangoapps.util.file import store_uploaded_file from common.djangoapps.track import contexts from lms.djangoapps.courseware.access import has_access @@ -349,39 +350,24 @@ def track_comment_unreported_event(request, course, comment): track_forum_event(request, event_name, course, comment, event_data) -def track_response_mark_event(request, course, comment, commentable_id, thread_type): +def track_response_mark_event(request, course, comment, thread, action_name): """ Send analytics event for response that is marked as endorsed or answered. """ - event_name = _EVENT_NAME_TEMPLATE.format(obj_type='response', action_name='mark') - if thread_type == 'question': + event_name = _EVENT_NAME_TEMPLATE.format(obj_type='response', action_name=action_name) + if getattr(thread, 'thread_type', '') == 'question': mark_type = 'Answer' else: mark_type = 'Endorse' event_data = { 'discussion': {'id': comment.thread_id}, - 'commentable_id': commentable_id, + 'commentable_id': getattr(thread, 'commentable_id', None), + 'is_thread_author': getattr(thread, 'user_id', None) == str(request.user.id), + 'is_global_staff': GlobalStaff().has_user(request.user), 'mark_type': mark_type, 'target_username': comment.get('username') } - track_forum_event(request, event_name, course, comment, event_data) - -def track_response_unmark_event(request, course, comment, commentable_id, thread_type): - """ - Send analytics event for response that is marked as unendorsed or unanswered. - """ - event_name = _EVENT_NAME_TEMPLATE.format(obj_type='response', action_name='unmark') - if thread_type == 'question': - mark_type = 'Answer' - else: - mark_type = 'Endorse' - event_data = { - 'discussion': {'id': comment.thread_id}, - 'commentable_id': commentable_id, - 'mark_type': mark_type, - 'target_username': comment.get('username'), - } track_forum_event(request, event_name, course, comment, event_data) @@ -410,13 +396,8 @@ def track_forum_response_mark_event(request, course, cc_content, value): Helper method for discussions response mark event """ thread = cc.Thread.find(cc_content.thread_id) - commentable_id = thread.get('commentable_id') - thread_type = thread.get('thread_type') - - if value: - track_response_mark_event(request, course, cc_content, commentable_id, thread_type) - else: - track_response_unmark_event(request, course, cc_content, commentable_id, thread_type) + action_name = 'mark' if value else 'unmark' + track_response_mark_event(request, course, cc_content, thread, action_name) def permitted(func): diff --git a/lms/djangoapps/discussion/rest_api/tests/test_serializers.py b/lms/djangoapps/discussion/rest_api/tests/test_serializers.py index 0c417d6d13..6e7f02b553 100644 --- a/lms/djangoapps/discussion/rest_api/tests/test_serializers.py +++ b/lms/djangoapps/discussion/rest_api/tests/test_serializers.py @@ -670,7 +670,7 @@ class CommentSerializerDeserializationTest(ForumsEnableMixin, CommentsServiceMoc } self.existing_comment = Comment(**make_minimal_cs_comment({ "id": "existing_comment", - "thread_id": "existing_thread", + "thread_id": "dummy", "body": "Original body", "user_id": str(self.user.id), "username": self.user.username, @@ -870,7 +870,14 @@ class CommentSerializerDeserializationTest(ForumsEnableMixin, CommentsServiceMoc } self.register_put_comment_response(cs_response_data) data = {"raw_body": "Edited body", "endorsed": True} + self.register_get_thread_response( + make_minimal_cs_thread({ + "id": "dummy", + "course_id": str(self.course.id), + }) + ) saved = self.save_and_reserialize(data, instance=self.existing_comment) + assert parsed_body(httpretty.last_request()) == { 'body': ['Edited body'], 'course_id': [str(self.course.id)],