Exemplo n.º 1
0
    def create_lesson(self, topic, unit_plan, number, age_groups=None):
        """Create lesson object.

        Args:
            topic: The related Topic object (Topic).
            unit_plan: The related UnitPlan object (UnitPlan).
            number: Identifier of the topic (int).

        Returns:
            Lesson object.
        """
        if age_groups and not isinstance(age_groups, list):
            age_groups = [age_groups]
        lesson = Lesson(
            topic=topic,
            unit_plan=unit_plan,
            slug="lesson-{}".format(number),
            name="Lesson {} ({} to {})".format(
                number, age_groups[0].ages[0] if age_groups else "none",
                age_groups[-1].ages[1] if age_groups else "none"),
            duration=number,
            content="<p>Content for lesson {}.</p>".format(number),
            languages=["en"],
        )
        lesson.save()
        if age_groups:
            for age_group in age_groups:
                LessonNumber(
                    age_group=age_group,
                    lesson=lesson,
                    number=number,
                ).save()
        return lesson
Exemplo n.º 2
0
 def test_lesson_view_ages_context_multiple_group(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)
     age_group_2 = self.test_data.create_age_group(8, 10)
     lesson = self.test_data.create_lesson(topic, unit_plan, 1, age_group_1)
     LessonNumber(
         age_group=age_group_2,
         lesson=lesson,
         number=1,
     ).save()
     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.assertEqual(response.context["lesson_ages"], [
         {
             "lower": 5,
             "upper": 7,
             "number": 1
         },
         {
             "lower": 8,
             "upper": 10,
             "number": 1
         },
     ])
    def create_lesson(self, topic, unit_plan, number, age_group=None):
        """Create lesson object.

        Args:
            topic: The related Topic object (Topic).
            unit_plan: The related UnitPlan object (UnitPlan).
            number: Identifier of the topic (int).

        Returns:
            Lesson object.
        """
        lesson = Lesson(
            topic=topic,
            unit_plan=unit_plan,
            slug="lesson-{}".format(number),
            name="Lesson {} ({} to {})".format(
                number, age_group.ages[0] if age_group else "none",
                age_group.ages[1] if age_group else "none"),
            duration=number,
            content="<p>Content for lesson {}.</p>".format(number),
        )
        lesson.save()
        if age_group:
            LessonNumber(
                age_group=age_group,
                lesson=lesson,
                number=number,
            ).save()
        return lesson
Exemplo n.º 4
0
    def load(self):
        """Load the content for unit plans.

        Raise:
            KeyNotFoundError: when no object can be found with the matching attribute.
            MissingRequiredFieldError: when a value for a required model field cannot
                be found in the config file.
        """
        unit_plan_structure = self.load_yaml_file(self.structure_file_path)

        unit_plan_translations = self.get_blank_translation_dictionary()

        content_filename = "{}.md".format(self.unit_plan_slug)
        content_translations = self.get_markdown_translations(content_filename)
        for language, content in content_translations.items():
            unit_plan_translations[language]["content"] = content.html_string
            unit_plan_translations[language]["name"] = content.title
            if content.heading_tree:
                heading_tree = convert_heading_tree_to_dict(
                    content.heading_tree)
                unit_plan_translations[language]["heading_tree"] = heading_tree

        if "computational-thinking-links" in unit_plan_structure:
            ct_links_filename = unit_plan_structure[
                "computational-thinking-links"]
            ct_links_translations = self.get_markdown_translations(
                ct_links_filename,
                heading_required=False,
                remove_title=False,
            )
            for language, content in ct_links_translations.items():
                unit_plan_translations[language][
                    "computational_thinking_links"] = content.html_string

        unit_plan = self.topic.unit_plans.create(
            slug=self.unit_plan_slug,
            languages=list(content_translations.keys()),
        )

        self.populate_translations(unit_plan, unit_plan_translations)
        self.mark_translation_availability(unit_plan,
                                           required_fields=["name", "content"])

        unit_plan.save()

        self.log("Added unit plan: {}".format(unit_plan.name), 1)

        # Load the lessons for the unit plan
        # Get path to lesson yaml
        lessons_yaml = unit_plan_structure.get("lessons", None)
        if lessons_yaml is None:
            raise MissingRequiredFieldError(self.structure_file_path,
                                            ["lessons", "age-groups"],
                                            "Unit Plan")

        lesson_path, lesson_structure_file = os.path.split(lessons_yaml)

        # Call the loader to save the lessons into the db
        self.factory.create_lessons_loader(
            self.topic,
            unit_plan,
            content_path=os.path.join(self.content_path, lesson_path),
            structure_filename=lesson_structure_file,
            base_path=self.base_path,
        ).load()

        # Create AgeGroup and assign to lessons
        age_groups = unit_plan_structure.get("age-groups", None)
        if age_groups is None:
            raise MissingRequiredFieldError(self.structure_file_path,
                                            ["lessons", "age-groups"],
                                            "Unit Plan")

        for (age_group_slug, age_group_data) in age_groups.items():

            try:
                age_group = AgeGroup.objects.get(slug=age_group_slug)
            except ObjectDoesNotExist:
                raise KeyNotFoundError(self.structure_file_path,
                                       age_group_slug, "Age Range")

            if age_group_data is None:
                raise MissingRequiredFieldError(self.structure_file_path,
                                                ["lesson keys"], "Unit Plan")

            for (lesson_slug, lesson_data) in age_group_data.items():
                try:
                    lesson = Lesson.objects.get(slug=lesson_slug)
                except ObjectDoesNotExist:
                    raise KeyNotFoundError(self.structure_file_path,
                                           lesson_slug, "Lesson")

                if lesson_data is None or lesson_data.get("number",
                                                          None) is None:
                    raise MissingRequiredFieldError(self.structure_file_path,
                                                    ["number"], "Unit Plan")
                else:
                    lesson_number = lesson_data.get("number", None)

                relationship = LessonNumber(
                    age_group=age_group,
                    lesson=lesson,
                    number=lesson_number,
                )
                relationship.save()
Exemplo n.º 5
0
    def load(self):
        """Load the content for unit plans.

        Raise:
            KeyNotFoundError: when no object can be found with the matching attribute.
            MissingRequiredFieldError: when a value for a required model field cannot
                be found in the config file.
        """
        unit_plan_structure = self.load_yaml_file(self.structure_file_path)

        # Convert the content to HTML
        unit_plan_content = self.convert_md_file(
            os.path.join(self.BASE_PATH, "{}.md".format(self.unit_plan_slug)),
            self.structure_file_path)

        heading_tree = None
        if unit_plan_content.heading_tree:
            heading_tree = convert_heading_tree_to_dict(
                unit_plan_content.heading_tree)

        if "computational-thinking-links" in unit_plan_structure:
            file_name = unit_plan_structure["computational-thinking-links"]
            file_path = os.path.join(self.BASE_PATH, file_name)
            ct_links_content = self.convert_md_file(
                file_path,
                self.structure_file_path,
                heading_required=False,
                remove_title=False,
            )
            ct_links = ct_links_content.html_string
        else:
            ct_links = None

        unit_plan = self.topic.unit_plans.create(
            slug=self.unit_plan_slug,
            name=unit_plan_content.title,
            content=unit_plan_content.html_string,
            heading_tree=heading_tree,
            computational_thinking_links=ct_links,
        )
        unit_plan.save()

        self.log("Added unit plan: {}".format(unit_plan.name), 1)

        # Load the lessons for the unit plan
        # Get path to lesson yaml
        lessons_yaml = unit_plan_structure.get("lessons", None)
        if lessons_yaml is None:
            raise MissingRequiredFieldError(self.structure_file_path,
                                            ["lessons", "age-groups"],
                                            "Unit Plan")
        lessons_structure_file_path = os.path.join(self.BASE_PATH,
                                                   lessons_yaml)

        # Call the loader to save the lessons into the db
        self.factory.create_lessons_loader(lessons_structure_file_path,
                                           self.topic, unit_plan,
                                           self.BASE_PATH).load()

        # Create AgeGroup and assign to lessons
        age_groups = unit_plan_structure.get("age-groups", None)
        if age_groups is None:
            raise MissingRequiredFieldError(self.structure_file_path,
                                            ["lessons", "age-groups"],
                                            "Unit Plan")

        for (age_group_slug, age_group_data) in age_groups.items():

            try:
                age_group = AgeGroup.objects.get(slug=age_group_slug)
            except:
                raise KeyNotFoundError(self.structure_file_path,
                                       age_group_slug, "Age Range")

            if age_group_data is None:
                raise MissingRequiredFieldError(self.structure_file_path,
                                                ["lesson keys"], "Unit Plan")

            for (lesson_slug, lesson_data) in age_group_data.items():
                try:
                    lesson = Lesson.objects.get(slug=lesson_slug)
                except:
                    raise KeyNotFoundError(self.structure_file_path,
                                           lesson_slug, "Lesson")

                if lesson_data is None or lesson_data.get("number",
                                                          None) is None:
                    raise MissingRequiredFieldError(self.structure_file_path,
                                                    ["number"], "Unit Plan")
                else:
                    lesson_number = lesson_data.get("number", None)

                relationship = LessonNumber(
                    age_group=age_group,
                    lesson=lesson,
                    number=lesson_number,
                )
                relationship.save()