From e571693455021bd63c3c25f8e249cfcb667e4166 Mon Sep 17 00:00:00 2001
From: Agrendalath <piotr@surowiec.it>
Date: Thu, 30 Jul 2020 20:01:56 +0200
Subject: [PATCH] Support EXCLUDED blocks in Block Completion Transformer

For now only the discussion blocks were supported. If we had a custom XBlock that specified `completion_mode = XBlockCompletionMode.EXCLUDED`, then it could never be marked as completed on the course outline page, despite being marked as such inside the learning sequence.
---
 .../blocks/transformers/block_completion.py    | 18 ++++++++++++++----
 .../tests/views/test_course_home.py            |  2 +-
 2 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/lms/djangoapps/course_api/blocks/transformers/block_completion.py b/lms/djangoapps/course_api/blocks/transformers/block_completion.py
index 38108251148..65bd3738e59 100644
--- a/lms/djangoapps/course_api/blocks/transformers/block_completion.py
+++ b/lms/djangoapps/course_api/blocks/transformers/block_completion.py
@@ -45,6 +45,17 @@ class BlockCompletionTransformer(BlockStructureTransformer):
     def collect(cls, block_structure):
         block_structure.request_xblock_fields('completion_mode')
 
+    @staticmethod
+    def _is_block_excluded(block_structure, block_key):
+        """
+        Checks whether block's completion method is of `EXCLUDED` type.
+        """
+        completion_mode = block_structure.get_xblock_field(
+            block_key, 'completion_mode'
+        )
+
+        return completion_mode == CompletionMode.EXCLUDED
+
     def mark_complete(self, complete_course_blocks, latest_complete_block_key, block_key, block_structure):
         """
         Helper function to mark a block as 'complete' as dictated by
@@ -58,14 +69,13 @@ class BlockCompletionTransformer(BlockStructureTransformer):
         """
         if block_key in complete_course_blocks:
             block_structure.override_xblock_field(block_key, self.COMPLETE, True)
-            if block_key == latest_complete_block_key:
+            if str(block_key) == str(latest_complete_block_key):
                 block_structure.override_xblock_field(block_key, self.RESUME_BLOCK, True)
 
         children = block_structure.get_children(block_key)
-        non_discussion_children = (child_key for child_key in children
-                                   if block_structure.get_xblock_field(child_key, 'category') != 'discussion')
         all_children_complete = all(block_structure.get_xblock_field(child_key, self.COMPLETE)
-                                    for child_key in non_discussion_children)
+                                    for child_key in children
+                                    if not self._is_block_excluded(block_structure, child_key))
 
         if children and all_children_complete:
             block_structure.override_xblock_field(block_key, self.COMPLETE, True)
diff --git a/openedx/features/course_experience/tests/views/test_course_home.py b/openedx/features/course_experience/tests/views/test_course_home.py
index b5850b8cd26..3dd3247dae8 100644
--- a/openedx/features/course_experience/tests/views/test_course_home.py
+++ b/openedx/features/course_experience/tests/views/test_course_home.py
@@ -208,7 +208,7 @@ class TestCourseHomePage(CourseHomePageTestCase):  # lint-amnesty, pylint: disab
 
         # Fetch the view and verify the query counts
         # TODO: decrease query count as part of REVO-28
-        with self.assertNumQueries(78, table_blacklist=QUERY_COUNT_TABLE_BLACKLIST):
+        with self.assertNumQueries(79, table_blacklist=QUERY_COUNT_TABLE_BLACKLIST):
             with check_mongo_calls(4):
                 url = course_home_url(self.course)
                 self.client.get(url)
-- 
GitLab