From c0631c320e246a76ed4d64bef2118f31678a23bf Mon Sep 17 00:00:00 2001 From: SaadYousaf Date: Mon, 14 Nov 2022 13:49:16 +0500 Subject: [PATCH] fix: legacy coverage and add missing properties for reported events --- .../django_comment_client/base/tests.py | 7 ++++--- .../django_comment_client/base/views.py | 17 +++++++++++++++-- .../discussion/rest_api/tests/test_api.py | 8 ++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/lms/djangoapps/discussion/django_comment_client/base/tests.py b/lms/djangoapps/discussion/django_comment_client/base/tests.py index a566126403..8e3df879b7 100644 --- a/lms/djangoapps/discussion/django_comment_client/base/tests.py +++ b/lms/djangoapps/discussion/django_comment_client/base/tests.py @@ -138,7 +138,8 @@ class ThreadActionGroupIdTestCase( "group_id": self.student_cohort.id, "closed": False, "type": "thread", - "commentable_id": "non_team_dummy_id" + "commentable_id": "non_team_dummy_id", + "body": "test body" } ) request = RequestFactory().post("dummy_url", post_params or {}) @@ -1663,7 +1664,7 @@ class TeamsPermissionsTestCase(ForumsEnableMixin, UrlResetMixin, SharedModuleSto commentable_id = getattr(self, commentable_id) self._setup_mock( user, mock_request, - {"closed": False, "commentable_id": commentable_id, "thread_id": "dummy_thread"}, + {"closed": False, "commentable_id": commentable_id, "thread_id": "dummy_thread", "body": 'dummy body'}, ) for action in ["upvote_comment", "downvote_comment", "un_flag_abuse_for_comment", "flag_abuse_for_comment"]: response = self.client.post( @@ -1684,7 +1685,7 @@ class TeamsPermissionsTestCase(ForumsEnableMixin, UrlResetMixin, SharedModuleSto commentable_id = getattr(self, commentable_id) self._setup_mock( user, mock_request, - {"closed": False, "commentable_id": commentable_id}, + {"closed": False, "commentable_id": commentable_id, "body": "dummy body"}, ) for action in ["upvote_thread", "downvote_thread", "un_flag_abuse_for_thread", "flag_abuse_for_thread", "follow_thread", "unfollow_thread"]: diff --git a/lms/djangoapps/discussion/django_comment_client/base/views.py b/lms/djangoapps/discussion/django_comment_client/base/views.py index 6072ebd902..29bf054cd5 100644 --- a/lms/djangoapps/discussion/django_comment_client/base/views.py +++ b/lms/djangoapps/discussion/django_comment_client/base/views.py @@ -270,8 +270,11 @@ def track_thread_reported_event(request, course, thread): event_name = _EVENT_NAME_TEMPLATE.format(obj_type='thread', action_name='reported') event_data = { 'body': thread.body[:TRACKING_MAX_FORUM_BODY], + 'truncated': len(thread.body) > TRACKING_MAX_FORUM_BODY, 'content_type': 'Post', 'commentable_id': thread.get('commentable_id', ''), + 'thread_type': thread.get('thread_type', ''), + 'group_id': thread.get('group_id', ''), } if hasattr(thread, 'username'): event_data['target_username'] = thread.get('username', '') @@ -287,6 +290,7 @@ def track_comment_reported_event(request, course, comment): event_name = _EVENT_NAME_TEMPLATE.format(obj_type=obj_type, action_name='reported') event_data = { 'body': comment.body[:TRACKING_MAX_FORUM_BODY], + 'truncated': len(comment.body) > TRACKING_MAX_FORUM_BODY, 'commentable_id': comment.get('commentable_id', ''), 'content_type': obj_type.capitalize(), } @@ -302,9 +306,13 @@ def track_thread_unreported_event(request, course, thread): event_name = _EVENT_NAME_TEMPLATE.format(obj_type='thread', action_name='unreported') event_data = { 'body': thread.body[:TRACKING_MAX_FORUM_BODY], + 'truncated': len(thread.body) > TRACKING_MAX_FORUM_BODY, 'content_type': 'Post', 'commentable_id': thread.get('commentable_id', ''), 'reported_status_cleared': not bool(thread.get('abuse_flaggers', [])), + 'thread_type': thread.get('thread_type', ''), + 'group_id': thread.get('group_id', ''), + } if hasattr(thread, 'username'): event_data['target_username'] = thread.get('username', '') @@ -320,6 +328,7 @@ def track_comment_unreported_event(request, course, comment): event_name = _EVENT_NAME_TEMPLATE.format(obj_type=obj_type, action_name='unreported') event_data = { 'body': comment.body[:TRACKING_MAX_FORUM_BODY], + 'truncated': len(comment.body) > TRACKING_MAX_FORUM_BODY, 'commentable_id': comment.get('commentable_id', ''), 'content_type': obj_type.capitalize(), 'reported_status_cleared': not bool(comment.get('abuse_flaggers', [])), @@ -767,9 +776,10 @@ def flag_abuse_for_thread(request, course_id, thread_id): """ course_key = CourseKey.from_string(course_id) user = cc.User.from_django_user(request.user) + course = get_course_by_id(course_key) thread = cc.Thread.find(thread_id) thread.flagAbuse(user, thread) - + track_discussion_reported_event(request, course, thread) return JsonResponse(prepare_content(thread.to_dict(), course_key)) @@ -790,7 +800,7 @@ def un_flag_abuse_for_thread(request, course_id, thread_id): has_access(request.user, 'staff', course) ) thread.unFlagAbuse(user, thread, remove_all) - + track_discussion_unreported_event(request, course, thread) return JsonResponse(prepare_content(thread.to_dict(), course_key)) @@ -804,8 +814,10 @@ def flag_abuse_for_comment(request, course_id, comment_id): """ course_key = CourseKey.from_string(course_id) user = cc.User.from_django_user(request.user) + course = get_course_by_id(course_key) comment = cc.Comment.find(comment_id) comment.flagAbuse(user, comment) + track_discussion_unreported_event(request, course, comment) return JsonResponse(prepare_content(comment.to_dict(), course_key)) @@ -826,6 +838,7 @@ def un_flag_abuse_for_comment(request, course_id, comment_id): ) comment = cc.Comment.find(comment_id) comment.unFlagAbuse(user, comment, remove_all) + track_discussion_unreported_event(request, course, comment) return JsonResponse(prepare_content(comment.to_dict(), course_key)) diff --git a/lms/djangoapps/discussion/rest_api/tests/test_api.py b/lms/djangoapps/discussion/rest_api/tests/test_api.py index 15e6fb8cfb..aab2be06c2 100644 --- a/lms/djangoapps/discussion/rest_api/tests/test_api.py +++ b/lms/djangoapps/discussion/rest_api/tests/test_api.py @@ -2871,6 +2871,9 @@ class UpdateThreadTest( 'target_username': self.user.username, 'title_truncated': False, 'title': 'Original Title', + 'thread_type': 'discussion', + 'group_id': None, + 'truncated': False, } if not new_flagged: expected_event_data['reported_status_cleared'] = False @@ -2919,6 +2922,9 @@ class UpdateThreadTest( 'title_truncated': False, 'title': 'Original Title', 'reported_status_cleared': False, + 'thread_type': 'discussion', + 'group_id': None, + 'truncated': False, } actual_event_name, actual_event_data = mock_emit.call_args[0] @@ -3433,6 +3439,7 @@ class UpdateCommentTest( 'content_type': 'Response', 'commentable_id': 'dummy', 'url': '', + 'truncated': False, 'user_course_roles': [], 'user_forums_roles': [FORUM_ROLE_STUDENT], 'target_username': self.user.username, @@ -3477,6 +3484,7 @@ class UpdateCommentTest( 'id': 'test_comment', 'content_type': 'Response', 'commentable_id': 'dummy', + 'truncated': False, 'url': '', 'user_course_roles': [], 'user_forums_roles': [FORUM_ROLE_STUDENT, FORUM_ROLE_ADMINISTRATOR],