Repository: oppia/oppia

Issue:
fetching multiple rights object using get multi
Implement functions to fetch multiple exploration rights object.


Discussion:
NB: this is in core.domain.summary_services.get_exploration_metadata_dicts() and get_displayable_exp_summary_dicts_matching_ids() (and possibly elsewhere).

Resolving patch:
diff --git a/core/domain/rights_manager.py b/core/domain/rights_manager.py
--- a/core/domain/rights_manager.py
+++ b/core/domain/rights_manager.py
@@ -384,6 +384,31 @@ def get_exploration_rights(exploration_id, strict=True):
         model, constants.ACTIVITY_TYPE_EXPLORATION)
 
 
+def get_multiple_exploration_rights_by_ids(exp_ids):
+    """Returns a list of ActivityRights objects for given exploration ids.
+
+    Args:
+        exp_ids: list(str). List of exploration ids.
+
+    Returns:
+        list(ActivityRights or None). List of rights object containing
+            ActivityRights object for existing exploration or None.
+    """
+    exp_rights_models = exp_models.ExplorationRightsModel.get_multi(
+        exp_ids)
+    exp_models_list = []
+
+    for model in exp_rights_models:
+        if model is None:
+            exp_models_list.append(None)
+        else:
+            exp_models_list.append(
+                _get_activity_rights_from_model(
+                    model, constants.ACTIVITY_TYPE_EXPLORATION))
+
+    return exp_models_list
+
+
 def is_exploration_private(exploration_id):
     """Returns whether exploration is private.
 
diff --git a/core/domain/summary_services.py b/core/domain/summary_services.py
--- a/core/domain/summary_services.py
+++ b/core/domain/summary_services.py
@@ -245,13 +245,15 @@ def get_exploration_metadata_dicts(exploration_ids, user):
     """
     exploration_summaries = (
         exp_services.get_exploration_summaries_matching_ids(exploration_ids))
+    exploration_rights_objects = (
+        rights_manager.get_multiple_exploration_rights_by_ids(exploration_ids))
 
     filtered_exploration_summaries = []
-    for exploration_summary in exploration_summaries:
+    for (exploration_summary, exploration_rights) in (
+            zip(exploration_summaries, exploration_rights_objects)):
         if exploration_summary is None:
             continue
-        exploration_rights = rights_manager.get_exploration_rights(
-            exploration_summary.id, strict=False)
+
         if exploration_rights is None:
             continue
 
@@ -309,13 +311,15 @@ def get_displayable_exp_summary_dicts_matching_ids(exploration_ids, user=None):
     """
     exploration_summaries = (
         exp_services.get_exploration_summaries_matching_ids(exploration_ids))
+    exploration_rights_objects = (
+        rights_manager.get_multiple_exploration_rights_by_ids(exploration_ids))
 
     filtered_exploration_summaries = []
-    for exploration_summary in exploration_summaries:
+    for (exploration_summary, exploration_rights) in (
+            zip(exploration_summaries, exploration_rights_objects)):
         if exploration_summary is None:
             continue
-        exploration_rights = rights_manager.get_exploration_rights(
-            exploration_summary.id)
+
         if exploration_rights is None:
             continue
         if exploration_summary.status == (


Test patch:
diff --git a/core/domain/rights_manager_test.py b/core/domain/rights_manager_test.py
--- a/core/domain/rights_manager_test.py
+++ b/core/domain/rights_manager_test.py
@@ -356,6 +356,21 @@ def test_check_exploration_rights(self):
         self.assertFalse(exp_rights.is_owner(self.user_id_b))
         self.assertFalse(exp_rights.is_editor(self.user_id_b))
 
+    def test_get_multiple_exploration_rights(self):
+        exp_ids = ['exp1', 'exp2', 'exp3', 'exp4']
+
+        # saving only first 3 explorations to check that None is returned for
+        # non-existing exploration
+        for exp_id in exp_ids[:3]:
+            self.save_new_valid_exploration(exp_id, self.user_id_admin)
+        exp_rights = rights_manager.get_multiple_exploration_rights_by_ids(
+            exp_ids)
+
+        self.assertEqual(len(exp_rights), 4)
+        for rights_object in exp_rights[:3]:
+            self.assertIsNotNone(rights_object)
+        self.assertIsNone(exp_rights[3])
+
 
 class CollectionRightsTests(test_utils.GenericTestBase):
     """Test that rights for actions on collections work as expected."""
@@ -667,6 +682,7 @@ def test_can_only_delete_unpublished_collections(self):
         self.assertTrue(rights_manager.check_can_delete_activity(
             self.user_a, collection_rights))
 
+
 class CheckCanReleaseOwnershipTest(test_utils.GenericTestBase):
     """Tests for check_can_release_ownership function."""
     published_exp_id = 'exp_id_1'