Example #1
0
    def load(self):
        """Load the glossary content into the database."""
        glossary_slugs = set()

        for filename in listdir(self.get_localised_dir(
                get_default_language())):
            if filename.endswith(self.FILE_EXTENSION):
                glossary_slug = filename[:-len(self.FILE_EXTENSION)]
                glossary_slugs.add(glossary_slug)

        for glossary_slug in glossary_slugs:
            term_translations = self.get_blank_translation_dictionary()

            content_filename = "{}.md".format(glossary_slug)
            content_translations = self.get_markdown_translations(
                content_filename)

            for language, content in content_translations.items():
                term_translations[language]["definition"] = content.html_string
                term_translations[language]["term"] = content.title

            glossary_term = GlossaryTerm(slug=glossary_slug, )
            self.populate_translations(glossary_term, term_translations)
            self.mark_translation_availability(
                glossary_term, required_fields=["term", "definition"])
            glossary_term.save()

            self.log("Added glossary term: {}".format(glossary_term.__str__()))

        self.log("All glossary terms loaded!\n")
Example #2
0
    def test_glossary_json_with_invalid_key(self):
        term = GlossaryTerm(slug="algorithm",
                            term="Algorithms",
                            definition="<p>Algorithms definition.</p>")
        term.save()

        url = reverse("topics:glossary_json")
        response = self.client.get(url, {"word": "pixel"})
        self.assertEqual(HTTPStatus.NOT_FOUND, response.status_code)
Example #3
0
    def test_glossary_with_one_definition(self):
        term = GlossaryTerm(slug="algorithm",
                            term="Algorithms",
                            definition="<p>Algorithms definition.</p>")
        term.save()

        url = reverse("topics:glossary")
        response = self.client.get(url)
        self.assertEqual(HTTPStatus.OK, response.status_code)
        self.assertEqual(len(response.context["glossary_terms"]), 1)
        self.assertQuerysetEqual(response.context["glossary_terms"],
                                 ["<GlossaryTerm: Algorithms>"])
Example #4
0
    def test_glossary_json_with_two_definitions(self):
        term1 = GlossaryTerm(
            slug="algorithm",
            term="Algorithms",
            definition="<p>Algorithms definition.</p>"
        )
        term1.save()
        term2 = GlossaryTerm(
            slug="pixel",
            term="Pixel",
            definition="<p>Pixel definition.</p>"
        )
        term2.save()

        url = reverse("topics:glossary_json")
        response = self.client.get(url, {"term": "pixel"})
        self.assertEqual(200, response.status_code)
        self.assertJSONEqual(
            str(response.content, encoding="utf8"),
            {
                "definition": "<p>Pixel definition.</p>",
                "slug": "pixel",
                "term": "Pixel"
            }
        )
Example #5
0
    def test_glossary_json_with_one_definition(self):
        term = GlossaryTerm(slug="algorithm",
                            term="Algorithms",
                            definition="<p>Algorithms definition.</p>")
        term.save()

        url = reverse("topics:glossary_json")
        response = self.client.get(url, {"term": "algorithm"})
        self.assertEqual(HTTPStatus.OK, response.status_code)
        self.assertJSONEqual(
            str(response.content, encoding="utf8"), {
                "definition": "<p>Algorithms definition.</p>",
                "slug": "algorithm",
                "term": "Algorithms"
            })
Example #6
0
    def create_glossary_term(self, number):
        """Create glossary term object.

        Args:
            number: Identifier of the glossary term (int).

        Returns:
            GlossaryTerm object.
        """
        term = GlossaryTerm(
            slug="term-{}".format(number),
            term="Term {}".format(number),
            definition="Defintion for term {}".format(number),
        )
        term.save()
        return term
    def test_glossary_with_two_definitions(self):
        term1 = GlossaryTerm(slug="algorithm",
                             term="Algorithms",
                             definition="<p>Algorithms definition.</p>",
                             languages=["en"])
        term1.save()
        term2 = GlossaryTerm(slug="pixel",
                             term="Pixel",
                             definition="<p>Pixel definition.</p>",
                             languages=["en"])
        term2.save()

        url = reverse("topics:glossary")
        response = self.client.get(url)
        self.assertEqual(HTTPStatus.OK, response.status_code)
        self.assertEqual(len(response.context["glossary_terms"]), 2)
        self.assertQuerysetEqual(
            response.context["glossary_terms"],
            ["<GlossaryTerm: Algorithms>", "<GlossaryTerm: Pixel>"])
Example #8
0
    def test_glossary_order(self):
        term_c = GlossaryTerm(slug="c", term="C", definition="")
        term_c.save()
        term_b = GlossaryTerm(slug="b", term="B", definition="")
        term_b.save()
        term_a = GlossaryTerm(slug="a", term="A", definition="")
        term_a.save()

        url = reverse("topics:glossary")
        response = self.client.get(url)
        self.assertEqual(HTTPStatus.OK, response.status_code)
        self.assertEqual(len(response.context["glossary_terms"]), 3)
        self.assertQuerysetEqual(
            response.context["glossary_terms"],
            ["<GlossaryTerm: A>", "<GlossaryTerm: B>", "<GlossaryTerm: C>"])