Repository: uccser/cs-unplugged

Issue:
Learning outcomes are duplicated on lessons
It appears they duplicate for the number of curriculum areas they have.
Learning outcomes are duplicated on lessons
It appears they duplicate for the number of curriculum areas they have.


Discussion:

Resolving patch:
diff --git a/csunplugged/topics/views.py b/csunplugged/topics/views.py
--- a/csunplugged/topics/views.py
+++ b/csunplugged/topics/views.py
@@ -186,10 +186,10 @@ def get_context_data(self, **kwargs):
         # Add all the connected programming challenges
         context["programming_challenges"] = self.object.programming_challenges.exists()
         # Add all the connected learning outcomes
-        context["learning_outcomes"] = self.object.learning_outcomes(manager="translated_objects") \
-                                           .all().select_related()
-        context["classroom_resources"] = self.object.classroom_resources(manager="translated_objects") \
-                                             .all().select_related()
+        context["learning_outcomes"] = self.object.learning_outcomes(manager="translated_objects").order_by("text")
+        context["classroom_resources"] = self.object.classroom_resources(manager="translated_objects").order_by(
+            "description"
+        )
         # Add all the connected generated resources
         related_resources = self.object.generated_resources.order_by("name")
         generated_resources = []
@@ -268,7 +268,7 @@ def get_context_data(self, **kwargs):
             lesson.challenge_number = challenge_numbers.challenge_number
         context["topic"] = self.object.topic
         # Add all the connected learning outcomes
-        context["learning_outcomes"] = self.object.learning_outcomes(manager="translated_objects").all()
+        context["learning_outcomes"] = self.object.learning_outcomes(manager="translated_objects").order_by("text")
         context["implementations"] = self.object.ordered_implementations()
         return context
 


Test patch:
diff --git a/csunplugged/tests/topics/views/test_lesson_view.py b/csunplugged/tests/topics/views/test_lesson_view.py
--- a/csunplugged/tests/topics/views/test_lesson_view.py
+++ b/csunplugged/tests/topics/views/test_lesson_view.py
@@ -285,6 +285,34 @@ def test_lesson_view_ages_context_learning_outcome_multiple(self):
             ordered=False
         )
 
+    # Created to avoid https://github.com/uccser/cs-unplugged/issues/827
+    def test_lesson_view_ages_context_learning_outcome_not_duplicated(self):
+        topic = self.test_data.create_topic(1)
+        unit_plan = self.test_data.create_unit_plan(topic, 1)
+        age_group_1 = self.test_data.create_age_group(5, 7)
+        lesson = self.test_data.create_lesson(
+            topic,
+            unit_plan,
+            1,
+            age_group_1
+        )
+        learning_outcome = self.test_data.create_learning_outcome(1)
+        area_1 = self.test_data.create_curriculum_area(1)
+        area_2 = self.test_data.create_curriculum_area(2)
+        learning_outcome.curriculum_areas.add(area_1, area_2)
+        lesson.learning_outcomes.add(learning_outcome)
+        kwargs = {
+            "topic_slug": "topic-1",
+            "unit_plan_slug": "unit-plan-1",
+            "lesson_slug": "lesson-1",
+        }
+        url = reverse("topics:lesson", kwargs=kwargs)
+        response = self.client.get(url)
+        self.assertQuerysetEqual(
+            response.context["learning_outcomes"],
+            ["<LearningOutcome: Outcome 1>"]
+        )
+
     def test_lesson_view_ages_context_learning_outcome_none(self):
         topic = self.test_data.create_topic(1)
         unit_plan = self.test_data.create_unit_plan(topic, 1)
diff --git a/csunplugged/tests/topics/views/test_programming_challenge_view.py b/csunplugged/tests/topics/views/test_programming_challenge_view.py
--- a/csunplugged/tests/topics/views/test_programming_challenge_view.py
+++ b/csunplugged/tests/topics/views/test_programming_challenge_view.py
@@ -173,6 +173,27 @@ def test_programming_challenge_view_learning_outcomes_context_order(self):
             ]
         )
 
+    # Created to avoid https://github.com/uccser/cs-unplugged/issues/827
+    def test_programming_challenge_view_ages_context_learning_outcome_not_duplicated(self):
+        topic = self.test_data.create_topic(1)
+        difficulty = self.test_data.create_difficulty_level(1)
+        challenge = self.test_data.create_programming_challenge(topic, 1, difficulty)
+        learning_outcome = self.test_data.create_learning_outcome(1)
+        area_1 = self.test_data.create_curriculum_area(1)
+        area_2 = self.test_data.create_curriculum_area(2)
+        learning_outcome.curriculum_areas.add(area_1, area_2)
+        challenge.learning_outcomes.add(learning_outcome)
+        kwargs = {
+            "topic_slug": topic.slug,
+            "programming_challenge_slug": challenge.slug,
+        }
+        url = reverse("topics:programming_challenge", kwargs=kwargs)
+        response = self.client.get(url)
+        self.assertQuerysetEqual(
+            response.context["learning_outcomes"],
+            ["<LearningOutcome: Outcome 1>"]
+        )
+
     def test_programming_challenge_view_implementations_context(self):
         topic = self.test_data.create_topic(1)
         difficulty = self.test_data.create_difficulty_level(1)