From b85929e2ee61ba0d9db3e747ffeee2ed97972fd0 Mon Sep 17 00:00:00 2001 From: Muhammad Adeel Tajamul <77053848+muhammadadeeltajamul@users.noreply.github.com> Date: Fri, 17 Feb 2023 06:51:07 +0500 Subject: [PATCH] feat: added stats at subsection in topics v3 api (#31763) Co-authored-by: adeel.tajamul --- .../discussion/rest_api/tests/test_views.py | 2 +- lms/djangoapps/discussion/rest_api/utils.py | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lms/djangoapps/discussion/rest_api/tests/test_views.py b/lms/djangoapps/discussion/rest_api/tests/test_views.py index 445def0b9e..559b4ff8f6 100644 --- a/lms/djangoapps/discussion/rest_api/tests/test_views.py +++ b/lms/djangoapps/discussion/rest_api/tests/test_views.py @@ -1003,7 +1003,7 @@ class CourseTopicsViewV3Test(DiscussionAPIViewTestMixin, CommentsServiceMockMixi assert courseware_topic_keys == expected_courseware_keys expected_courseware_keys.remove('courseware') sequential_keys = list(data[1]['children'][0].keys()) - assert sequential_keys == expected_courseware_keys + assert sequential_keys == (expected_courseware_keys + ['thread_counts']) expected_non_courseware_keys.remove('courseware') vertical_keys = list(data[1]['children'][0]['children'][0].keys()) assert vertical_keys == expected_non_courseware_keys diff --git a/lms/djangoapps/discussion/rest_api/utils.py b/lms/djangoapps/discussion/rest_api/utils.py index ac65a47010..06d726346b 100644 --- a/lms/djangoapps/discussion/rest_api/utils.py +++ b/lms/djangoapps/discussion/rest_api/utils.py @@ -226,6 +226,25 @@ def create_blocks_params(course_usage_key, user): } +def add_thread_stats_to_subsection(topics_list): + """ + Add topic stats at subsection by adding stats of all units in + the topic + """ + for section in topics_list: + for subsection in section.get('children', []): + discussions = 0 + questions = 0 + for unit in subsection.get('children', []): + thread_counts = unit.get('thread_counts', {}) + discussions += thread_counts.get('discussion', 0) + questions += thread_counts.get('question', 0) + subsection['thread_counts'] = { + 'discussion': discussions, + 'question': questions, + } + + def create_topics_v3_structure(blocks, topics): """ Create V3 topics structure from blocks and v2 topics @@ -253,6 +272,7 @@ def create_topics_v3_structure(blocks, topics): topics, ) + add_thread_stats_to_subsection(courseware_topics) structured_topics = non_courseware_topics + courseware_topics topic_ids = get_topic_ids_from_topics(topics)