From 7481b11e25481d7ca16e066422b282f3a7849e87 Mon Sep 17 00:00:00 2001 From: "adeel.tajamul" Date: Wed, 15 Jun 2022 17:37:13 +0500 Subject: [PATCH] fix: updated api response --- lms/djangoapps/discussion/rest_api/api.py | 46 +++++++++++++++++++ .../discussion/rest_api/tests/test_views.py | 12 +---- lms/djangoapps/discussion/rest_api/views.py | 31 ++----------- 3 files changed, 51 insertions(+), 38 deletions(-) diff --git a/lms/djangoapps/discussion/rest_api/api.py b/lms/djangoapps/discussion/rest_api/api.py index dd1f2ac97c..c512366b70 100644 --- a/lms/djangoapps/discussion/rest_api/api.py +++ b/lms/djangoapps/discussion/rest_api/api.py @@ -18,6 +18,7 @@ from edx_django_utils.monitoring import function_trace from opaque_keys import InvalidKeyError from opaque_keys.edx.locator import CourseKey from rest_framework.exceptions import PermissionDenied +from rest_framework.response import Response from rest_framework.request import Request from xmodule.course_module import CourseBlock from xmodule.modulestore.django import modulestore @@ -30,6 +31,7 @@ from lms.djangoapps.discussion.toggles import ENABLE_LEARNERS_TAB_IN_DISCUSSIONS from lms.djangoapps.discussion.toggles_utils import reported_content_email_notification_enabled from openedx.core.djangoapps.discussions.models import DiscussionsConfiguration, DiscussionTopicLink, Provider from openedx.core.djangoapps.discussions.utils import get_accessible_discussion_xblocks +from openedx.core.djangoapps.django_comment_common import comment_client from openedx.core.djangoapps.django_comment_common.comment_client.comment import Comment from openedx.core.djangoapps.django_comment_common.comment_client.course import ( get_course_commentable_counts, @@ -851,6 +853,50 @@ def get_thread_list( }) +def get_learner_active_thread_list(request, course_key, query_params): + """ + Return the list of active threads of a particular user in query params + user_id must be given in query_params + + Parameters: + + request: The django request objects used for build_absolute_uri + course_key: The key of the course + query_params: If true, fetch the count of flagged items in each thread + + Returns: + + A paginated result containing a list of threads. + """ + + course = _get_course(course_key, request.user) + context = get_context(course, request) + + group_id = query_params.get('group_id', None) + user_id = query_params.get('user_id', None) + if user_id is None: + return Response({'details': 'Invalid user id'}, status=400) + + if group_id is not None: + profiled_user = comment_client.User(id=user_id, course_id=course_key, group_id=group_id) + else: + profiled_user = comment_client.User(id=user_id, course_id=course_key) + + threads, page, num_pages = profiled_user.active_threads(query_params) + results = _serialize_discussion_entities( + request, context, threads, {'profile_image'}, DiscussionEntity.thread + ) + paginator = DiscussionAPIPagination( + request, + page, + num_pages, + len(threads) + ) + return paginator.get_paginated_response({ + "results": results, + }) + + def get_comment_list(request, thread_id, endorsed, page, page_size, flagged=False, requested_fields=None): """ Return the list of comments in the given thread. diff --git a/lms/djangoapps/discussion/rest_api/tests/test_views.py b/lms/djangoapps/discussion/rest_api/tests/test_views.py index c0194ddc7c..7cf1c18d47 100644 --- a/lms/djangoapps/discussion/rest_api/tests/test_views.py +++ b/lms/djangoapps/discussion/rest_api/tests/test_views.py @@ -1472,17 +1472,7 @@ class LearnerThreadViewAPITest(DiscussionAPIViewTestMixin, ModuleStoreTestCase): assert response.status_code == 200 response_data = json.loads(response.content.decode('utf-8')) expected_api_response = expected_response['collection'] - for thread in expected_api_response: - for key in ['group_id', 'abuse_flagged_count', 'closed_by', 'close_reason_code']: - thread.pop(key) - assert response_data["results"] == expected_api_response - assert response_data["pagination"] == { - "num_pages": 1, - "page": 1, - "count": 1, - "next": None, - "previous": None, - } + assert response_data['results'] == expected_api_response def test_no_user_id_given(self): response = self.client.get(self.url) diff --git a/lms/djangoapps/discussion/rest_api/views.py b/lms/djangoapps/discussion/rest_api/views.py index 9f44dbd9b7..fa9155dea3 100644 --- a/lms/djangoapps/discussion/rest_api/views.py +++ b/lms/djangoapps/discussion/rest_api/views.py @@ -24,13 +24,8 @@ from xmodule.modulestore.django import modulestore from common.djangoapps.util.file import store_uploaded_file from lms.djangoapps.course_goals.models import UserActivity -from lms.djangoapps.discussion.django_comment_client.permissions import has_permission from lms.djangoapps.discussion.django_comment_client import settings as cc_settings -from lms.djangoapps.discussion.django_comment_client.utils import ( - get_group_id_for_comments_service, - is_user_community_ta, - prepare_content, -) +from lms.djangoapps.discussion.django_comment_client.utils import get_group_id_for_comments_service from lms.djangoapps.instructor.access import update_forum_role from openedx.core.djangoapps.discussions.serializers import DiscussionSettingsSerializer from openedx.core.djangoapps.django_comment_common import comment_client @@ -54,6 +49,7 @@ from ..rest_api.api import ( get_response_comments, get_thread, get_thread_list, + get_learner_active_thread_list, get_user_comments, update_comment, update_thread, @@ -608,28 +604,9 @@ class LearnerThreadView(APIView): "per_page": threads_per_page, "course_id": str(course_key), "user_id": user_id, + "group_id": group_id } - - if group_id is not None: - query_params['group_id'] = group_id - profiled_user = comment_client.User(id=user_id, course_id=course_key, group_id=group_id) - else: - profiled_user = comment_client.User(id=user_id, course_id=course_key) - threads, page, num_pages = profiled_user.active_threads(query_params) - - is_staff = has_permission(request.user, 'openclose_thread', course_key) - is_community_ta = is_user_community_ta(request.user, course_key) - threads = [prepare_content(thread, course_key, is_staff, is_community_ta) for thread in threads] - return Response({ - "results": threads, - "pagination": { - "num_pages": num_pages, - "page": page, - "count": len(threads), - "next": page + 1 if page < num_pages else None, - "previous": None if page <= 1 else page - 1, - } - }) + return get_learner_active_thread_list(request, course_key, query_params) @view_auth_classes()