Exemple #1
0
    def convert_activity_step_file(self, yaml_file_path):
        """Return HTML for a given YAML file.

        Args:
            yaml_file_path: Location of YAML file to convert (str).

        Returns:
            String of HTML

        Raises:
            CouldNotFindYAMLFileError: When a given YAML file cannot be found.
        """
        activity_steps_data = self.load_yaml_file(yaml_file_path)

        # Check all images exist
        activity_image_filenames = set()
        for step_data in activity_steps_data:
            activity_image_filenames.add(step_data.get('image'))
        find_image_files(activity_image_filenames, yaml_file_path)

        # Render as HTML
        html = render_to_string(
            'at_home/activity_steps.html',
            {'activity_steps': activity_steps_data}
        )
        return html
    def load(self):
        """Load the activity challenges.

        Raise:
            MissingRequiredFieldError: when no object can be found with the matching
                attribute.
        """
        challenge_translations = self.get_yaml_translations(
            CHALLENGES_FILENAME, required_fields=["question", "answer"])

        for (challenge_order_number,
             challenge_data) in challenge_translations.items():
            translations = self.get_blank_translation_dictionary()
            translations.update(
                challenge_translations.get(challenge_order_number, dict()))
            # Store all answers in lower case
            # TODO: Check if this ruins answers, especially in translations
            for language, translation in translations.items():
                if 'answer' in translation:
                    translation['answer'] = translation['answer'].lower()

            if "image" in challenge_data:
                challenge_image = challenge_data["image"]
                # Check if challenge image is available
                find_image_files([challenge_image], self.structure_file_path)

            # Create or update challenge and save to databbase
            challenge, created = Challenge.objects.update_or_create(
                order_number=challenge_order_number,
                activity=self.activity,
            )
            self.populate_translations(challenge, translations)
            self.mark_translation_availability(
                challenge, required_fields=["question", "answer"])
            challenge.save()

            if created:
                self.log("Added challenge: {}".format(challenge.__str__()), 1)
            else:
                self.log("Updated challenge: {}".format(challenge.__str__()),
                         1)
    def load(self):
        """Load the content for a topic.

        Raise:
            MissingRequiredFieldError: when no object can be found with the matching
                attribute.
        """
        topic_structure = self.load_yaml_file(self.structure_file_path)

        unit_plans = topic_structure.get("unit-plans", None)
        if unit_plans is None:
            raise MissingRequiredFieldError(self.structure_file_path,
                                            ["unit-plans"], "Topic")

        topic_translations = self.get_blank_translation_dictionary()

        content_filename = "{}.md".format(self.topic_slug)
        content_translations = self.get_markdown_translations(content_filename)
        for language, content in content_translations.items():
            topic_translations[language]["content"] = content.html_string
            topic_translations[language]["name"] = content.title

        if "other-resources" in topic_structure and topic_structure[
                "other-resources"] is not None:
            other_resources_filename = topic_structure["other-resources"]
            other_resources_translations = self.get_markdown_translations(
                other_resources_filename)
            for language, content in other_resources_translations.items():
                topic_translations[language][
                    "other_resources"] = content.html_string

        # Check if icon is given
        if "icon" in topic_structure:
            topic_icon = topic_structure["icon"]
            if topic_icon is not None:
                find_image_files([topic_icon], self.structure_file_path)
            else:
                topic_icon = None
        else:
            topic_icon = None

        # Create topic objects and save to the db
        topic = Topic(
            slug=self.topic_slug,
            icon=topic_icon,
        )

        self.populate_translations(topic, topic_translations)
        self.mark_translation_availability(topic,
                                           required_fields=["name", "content"])
        topic.save()

        self.log("Added Topic: {}".format(topic.name))

        # Load programming challenges
        if "programming-challenges" in topic_structure:
            programming_challenges_structure_file_path = topic_structure[
                "programming-challenges"]
            if programming_challenges_structure_file_path is not None:
                programming_challenges_path, structure_filename = os.path.split(
                    programming_challenges_structure_file_path)
                self.factory.create_programming_challenges_loader(
                    topic,
                    base_path=self.base_path,
                    content_path=os.path.join(self.content_path,
                                              programming_challenges_path),
                    structure_filename=structure_filename).load()

        # Load unit plans
        for unit_plan_file_path in unit_plans:
            content_path, structure_filename = os.path.split(
                unit_plan_file_path)
            self.factory.create_unit_plan_loader(
                topic,
                base_path=self.base_path,
                content_path=os.path.join(self.content_path, content_path),
                structure_filename=structure_filename).load()

        if "curriculum-integrations" in topic_structure:
            curriculum_integrations_structure_file_path = topic_structure[
                "curriculum-integrations"]
            if curriculum_integrations_structure_file_path is not None:
                curriculum_integrations_path, structure_filename = os.path.split(
                    curriculum_integrations_structure_file_path)
                self.factory.create_curriculum_integrations_loader(
                    topic,
                    base_path=self.base_path,
                    content_path=os.path.join(self.content_path,
                                              curriculum_integrations_path),
                    structure_filename=structure_filename).load()

        self.log("")
Exemple #4
0
    def load(self):
        """Load the content for an activity.

        Raise:
            MissingRequiredFieldError: when no object can be found with the matching attribute.
        """
        activity_translations = self.get_blank_translation_dictionary()

        try:
            order_number = self.activity_data['order-number']
            activity_icon = self.activity_data['icon']
        except KeyError:
            raise MissingRequiredFieldError(
                self.structure_file_path,
                [
                    "order-number",
                    "icon"
                ],
                "Activity"
            )

        # Check if icon is available
        find_image_files([activity_icon], self.structure_file_path)

        # Introduction content
        content_translations = self.get_markdown_translations(INTRODUCTION_FILENAME)
        for language, content in content_translations.items():
            activity_translations[language]['name'] = content.title
            activity_translations[language]['introduction'] = content.html_string

        activity_steps_translations = self.get_activity_step_translations()
        for language, content in activity_steps_translations.items():
            activity_translations[language]['activity_steps'] = content

        activity_extra_info_md_file_path = self.get_localised_file(
            language,
            ACTIVITY_EXTRA_INFORMATION_FILENMAE,
        )
        activity_extra_info_exists = os.path.exists(activity_extra_info_md_file_path)
        if activity_extra_info_exists:
            activity_extra_info_translations = self.get_markdown_translations(
                ACTIVITY_EXTRA_INFORMATION_FILENMAE,
                heading_required=False,
                remove_title=False,
            )
            for language, content in activity_extra_info_translations.items():
                activity_translations[language]['activity_extra_information'] = content.html_string

        inside_computer_translations = self.get_markdown_translations(
            INSIDE_THE_COMPUTER_FILENAME,
            heading_required=False,
            remove_title=False,
        )
        for language, content in inside_computer_translations.items():
            activity_translations[language]['inside_the_computer'] = content.html_string

        project_md_file_path = self.get_localised_file(
            language,
            PROJECT_FILENAME,
        )
        project_exists = os.path.exists(project_md_file_path)
        if project_exists:
            project_translations = self.get_markdown_translations(
                PROJECT_FILENAME,
                heading_required=False,
                remove_title=False,
            )
            for language, content in project_translations.items():
                activity_translations[language]['project'] = content.html_string

        more_information_translations = self.get_markdown_translations(
            MORE_INFORMATION_FILENAME,
            heading_required=False,
            remove_title=False,
        )
        for language, content in more_information_translations.items():
            activity_translations[language]['more_information'] = content.html_string

        # Create or update activity objects and save to the database
        activity, created = Activity.objects.update_or_create(
            slug=self.activity_slug,
            defaults={
                'order_number': order_number,
                'icon': activity_icon,
            }
        )

        self.populate_translations(activity, activity_translations)
        self.mark_translation_availability(activity, required_fields=['name', 'introduction'])
        activity.save()

        if created:
            self.log('Added Activity: {}'.format(activity.name))
        else:
            self.log('Updated Activity: {}'.format(activity.name))

        # structure_filename = os.path.join(unit_plan_file_path)
        self.factory.create_challenge_loader(
            activity,
            base_path=self.base_path,
            content_path=self.content_path,
            structure_filename=self.structure_filename,
            lite_loader=self.lite_loader,
        ).load()
Exemple #5
0
 def test_find_image_files_valid(self):
     images = {"img/logos/uc-computer-science-education-logo.png", "img/guide-favicon.png"}
     check_required_files.find_image_files(images, "md file path")
Exemple #6
0
 def test_find_image_files_valid(self):
     images = {"img/logo.png", "img/favicon.ico"}
     check_required_files.find_image_files(images, "md file path")
    def load(self):
        """Load the content for a topic.

        Raise:
            MissingRequiredFieldError: when no object can be found with the matching
                attribute.
        """
        topic_structure = self.load_yaml_file(self.structure_file_path)

        unit_plans = topic_structure.get("unit-plans", None)
        if unit_plans is None:
            raise MissingRequiredFieldError(self.structure_file_path,
                                            ["unit-plans"], "Topic")

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

        # If other resources are given, convert to HTML
        if "other-resources" in topic_structure:
            topic_other_resources_file = topic_structure["other-resources"]
            if topic_other_resources_file is not None:
                other_resources_content = self.convert_md_file(
                    os.path.join(self.BASE_PATH, topic_other_resources_file),
                    self.structure_file_path)
                topic_other_resources_html = other_resources_content.html_string
            else:
                topic_other_resources_html = None
        else:
            topic_other_resources_html = None

        # Check if icon is given
        if "icon" in topic_structure:
            topic_icon = topic_structure["icon"]
            if topic_icon is not None:
                find_image_files([topic_icon], self.structure_file_path)
            else:
                topic_icon = None
        else:
            topic_icon = None

        # Create topic objects and save to the db
        topic = Topic(slug=self.topic_slug,
                      name=topic_content.title,
                      content=topic_content.html_string,
                      other_resources=topic_other_resources_html,
                      icon=topic_icon)
        topic.save()

        self.log("Added Topic: {}".format(topic.name))

        # Load programming challenges
        if "programming-challenges" in topic_structure:
            programming_challenges_structure_file_path = topic_structure[
                "programming-challenges"]
            if programming_challenges_structure_file_path is not None:
                self.factory.create_programming_challenges_loader(
                    programming_challenges_structure_file_path, topic,
                    self.BASE_PATH).load()

        # Load unit plans
        for unit_plan_file_path in unit_plans:
            self.factory.create_unit_plan_loader(unit_plan_file_path, topic,
                                                 self.BASE_PATH).load()

        if "curriculum-integrations" in topic_structure:
            curriculum_integrations_structure_file_path = topic_structure[
                "curriculum-integrations"]
            if curriculum_integrations_structure_file_path is not None:
                self.factory.create_curriculum_integrations_loader(
                    curriculum_integrations_structure_file_path, topic,
                    self.BASE_PATH).load()

        self.log("")