Beispiel #1
0
def dump_categories_data(apps, schema_editor, **context) -> dict:
    for category in OldCategory.get_root_nodes():
        if _is_wrong_blog_related_to_categories_tree(category):
            raise Exception(
                f'One or several of descendants of this category are linked to articles from different blogs: {category.safe_translation_getter("name", language_code=settings.LANGUAGE_CODE, any_language=True)}'
            )

    categories_data_list = prepare_category_data_list(
        OldCategory.get_root_nodes())
    upload_categories_data(categories_data_list)
    assign_blog_configs()
Beispiel #2
0
 def test_get_children_count(self):
     a = Category.add_root(name="A")
     a.add_child(name="B")
     self.assertEquals(a.get_children_count(), 1)
     a.add_child(name="C")
     a = self.reload(a)
     self.assertEquals(a.get_children_count(), 2)
    def test_category_multiple_choice_field(self):
        root = Category.add_root(name="root")
        root.save()
        child1 = root.add_child(name="child1")
        child2 = root.add_child(name="child2")
        grandchild1 = child1.add_child(name="grandchild1")
        root = self.reload(root)
        child1 = self.reload(child1)
        field = CategoryMultipleChoiceField(None)
        self.assertEqual(
            field.label_from_instance(child2),
            "  child2",
        )
        self.assertEqual(
            field.label_from_instance(grandchild1),
            "    grandchild1",
        )

        # Tests that the field correctly throws an ImproperlyConfigured
        # exception if the given object is not a Category (or something that
        # acts like one)
        with self.assertRaises(ImproperlyConfigured):
            field.label_from_instance(object)

        # Check that using an untranslated language does not raise exceptions
        with switch_language(child1, 'it'):
            try:
                field.label_from_instance(child1)
            except ImproperlyConfigured:
                self.fail("Translating to an unavailable language should not "
                          "result in an exception.")
 def test_create_in_orm_category(self):
     name = "Root Node"
     root = Category.add_root(name=name)
     root.set_current_language("en")
     root.save()
     root = self.reload(root)
     self.assertEquals(root.name, name)
Beispiel #5
0
    def setup_categories(self):
        """
        Sets-up i18n categories (self.category_root, self.category1 and
        self.category2) for use in tests
        """
        self.language = settings.LANGUAGES[0][0]

        categories = []
        # Set the default language, create the objects
        with override(self.language):
            code = "{0}-".format(self.language)
            self.category_root = Category.add_root(
                name=self.rand_str(prefix=code, length=8))
            categories.append(self.category_root)
            self.category1 = self.category_root.add_child(
                name=self.rand_str(prefix=code, length=8))
            categories.append(self.category1)
            self.category2 = self.category_root.add_child(
                name=self.rand_str(prefix=code, length=8))
            categories.append(self.category2)

        # We should reload category_root, since we modified its children.
        self.category_root = self.reload(self.category_root)

        # Setup the other language(s) translations for the categories
        for language, _ in settings.LANGUAGES[1:]:
            for category in categories:
                with switch_language(category, language):
                    code = "{0}-".format(language)
                    category.name = self.rand_str(prefix=code, length=8)
                    category.save()
 def test_get_children(self):
     a = Category.add_root(name="A")
     b = a.add_child(name="B")
     self.assertIn(b, a.get_children())
     c = a.add_child(name="C")
     a = self.reload(a)
     self.assertIn(c, a.get_children())
Beispiel #7
0
 def test_get_children(self):
     a = Category.add_root(name="A")
     b = a.add_child(name="B")
     self.assertIn(b, a.get_children())
     c = a.add_child(name="C")
     a = self.reload(a)
     self.assertIn(c, a.get_children())
Beispiel #8
0
    def setup_categories(self):
        """
        Sets-up i18n categories (self.category_root, self.category1 and
        self.category2) for use in tests
        """
        self.language = settings.LANGUAGES[0][0]

        categories = []
        # Set the default language, create the objects
        with override(self.language):
            code = "{0}-".format(self.language)
            self.category_root = Category.add_root(
                name=self.rand_str(prefix=code, length=8))
            categories.append(self.category_root)
            self.category1 = self.category_root.add_child(
                name=self.rand_str(prefix=code, length=8))
            categories.append(self.category1)
            self.category2 = self.category_root.add_child(
                name=self.rand_str(prefix=code, length=8))
            categories.append(self.category2)

        # We should reload category_root, since we modified its children.
        self.category_root = self.reload(self.category_root)

        # Setup the other language(s) translations for the categories
        for language, _ in settings.LANGUAGES[1:]:
            for category in categories:
                with switch_language(category, language):
                    code = "{0}-".format(language)
                    category.name = self.rand_str(prefix=code, length=8)
                    category.save()
Beispiel #9
0
    def test_add_translations(self):
        values = [
            # language code, name, slug
            ('en', "Cheese Omelette", "cheese-omelette"),
            ('de', "Käseomelett", "kaseomelett"),
            ('fr', "Omelette au Fromage", "omelette-au-fromage"),
        ]

        node = None

        # Create the translations
        for lang, name, slug in values:
            if node:
                with switch_language(node, lang):
                    node.name = name
                    node.save()
            else:
                with translation.override(lang):
                    node = Category.add_root(name=name)
                    node.save()

        # Now test that they exist (and didn't obliterate one another)
        for lang, name, slug in values:
            with switch_language(node, lang):
                self.assertEqual(node.name, name)
                self.assertEqual(node.slug, slug)

        # Now test that we gracefully handle languages where there is no
        # translation.
        with switch_language(node, 'it'):
            try:
                node.name
            except Exception:
                self.fail("Translating to an unavailable language should not "
                          "result in an exception.")
Beispiel #10
0
 def test_create_in_orm_category(self):
     name = "Root Node"
     root = Category.add_root(name=name)
     root.set_current_language("en")
     root.save()
     root = self.reload(root)
     self.assertEquals(root.name, name)
    def test_add_translations(self):
        values = [
            # language code, name, slug
            ('en', "Cheese Omelette", "cheese-omelette"),
            ('de', "Käseomelett", "kaseomelett"),
            ('fr', "Omelette au Fromage", "omelette-au-fromage"),
        ]

        node = None

        # Create the translations
        for lang, name, slug in values:
            if node:
                with switch_language(node, lang):
                    node.name = name
                    node.save()
            else:
                with translation.override(lang):
                    node = Category.add_root(name=name)
                    node.save()

        # Now test that they exist (and didn't obliterate one another)
        for lang, name, slug in values:
            with switch_language(node, lang):
                self.assertEqual(node.name, name)
                self.assertEqual(node.slug, slug)

        # Now test that we gracefully handle languages where there is no
        # translation.
        with switch_language(node, 'it'):
            try:
                node.name
            except:
                self.fail("Translating to an unavailable language should not "
                          "result in an exception.")
 def test_get_children_count(self):
     a = Category.add_root(name="A")
     a.add_child(name="B")
     self.assertEquals(a.get_children_count(), 1)
     a.add_child(name="C")
     a = self.reload(a)
     self.assertEquals(a.get_children_count(), 2)
Beispiel #13
0
def assign_blog_configs():
    for root_category in OldCategory.get_root_nodes():
        tree_related_app_configs = list([
            article.app_config for article in root_category.article_set.all()
        ])
        for category in root_category.get_descendants():
            tree_related_app_configs.extend(
                [article.app_config for article in category.article_set.all()])

        if tree_related_app_configs:
            newsblog_config = list(set(tree_related_app_configs))[0]
        else:
            newsblog_config = None

        new_category = NewCategory.objects.get(
            translations__slug=root_category.slug)
        if newsblog_config:
            new_category.newsblog_config = newsblog_config
        new_category.save()
        for category in root_category.get_descendants():
            new_category = NewCategory.objects.get(
                translations__slug=category.slug)
            if newsblog_config:
                new_category.newsblog_config = newsblog_config
            new_category.save()
 def test_get_ancestors(self):
     a = Category.add_root(name="A")
     b = a.add_child(name="B")
     c = b.add_child(name="C")
     self.assertIn(a, b.get_ancestors())
     self.assertIn(a, c.get_ancestors())
     d = b.add_child(name='D')
     self.assertIn(a, d.get_ancestors())
 def test_get_descendants(self):
     a = Category.add_root(name="A")
     b = a.add_child(name="B")
     c = b.add_child(name="C")
     self.assertIn(c, a.get_descendants())
     d = b.add_child(name='D')
     b = self.reload(b)
     self.assertIn(d, b.get_descendants())
Beispiel #16
0
 def test_get_descendants(self):
     a = Category.add_root(name="A")
     b = a.add_child(name="B")
     c = b.add_child(name="C")
     self.assertIn(c, a.get_descendants())
     d = b.add_child(name='D')
     b = self.reload(b)
     self.assertIn(d, b.get_descendants())
Beispiel #17
0
 def test_get_ancestors(self):
     a = Category.add_root(name="A")
     b = a.add_child(name="B")
     c = b.add_child(name="C")
     self.assertIn(a, b.get_ancestors())
     self.assertIn(a, c.get_ancestors())
     d = b.add_child(name='D')
     self.assertIn(a, d.get_ancestors())
 def test_slug_collision(self):
     root = Category.add_root(name="test")
     root.save()
     root = self.reload(root)
     self.assertEquals(root.slug, "test")
     child1 = root.add_child(name="test")
     self.assertEquals(child1.slug, "test-1")
     child2 = root.add_child(name="test")
     self.assertEquals(child2.slug, "test-2")
Beispiel #19
0
def _is_wrong_blog_related_to_categories_tree(
        root_category: OldCategory) -> bool:
    tree_related_app_configs = list(
        [article.app_config for article in root_category.article_set.all()])
    for category in root_category.get_descendants():
        tree_related_app_configs.extend(
            [article.app_config for article in category.article_set.all()])
    count_of_related_blogs = len(set(tree_related_app_configs))
    return count_of_related_blogs > 1
Beispiel #20
0
 def test_slug_collision(self):
     root = Category.add_root(name="test")
     root.save()
     root = self.reload(root)
     self.assertEquals(root.slug, "test")
     child1 = root.add_child(name="test")
     self.assertEquals(child1.slug, "test-1")
     child2 = root.add_child(name="test")
     self.assertEquals(child2.slug, "test-2")
 def test_move_category(self):
     a = Category.add_root(name="A")
     b = a.add_child(name="B")
     c = a.add_child(name="C")
     a = self.reload(a)
     b = self.reload(b)
     self.assertEqual(a, c.get_parent())
     self.assertNotEqual(b, c.get_parent())
     c.move(b, "first-child")
     b = self.reload(b)
     c = self.reload(c)
     self.assertEqual(b, c.get_parent())
Beispiel #22
0
 def test_delete(self):
     root = Category.add_root(name="test")
     root.save()
     child1 = root.add_child(name="Child 1")
     self.assertIn(child1, root.get_children())
     try:
         root.delete()
     except TypeError:
         self.fail('Deleting a node throws a TypeError.')
     except Exception:
         self.fail('Deleting a node throws an exception.')
     self.assertNotIn(child1, Category.objects.all())
Beispiel #23
0
 def test_move_category(self):
     a = Category.add_root(name="A")
     b = a.add_child(name="B")
     c = a.add_child(name="C")
     a = self.reload(a)
     b = self.reload(b)
     self.assertEqual(a, c.get_parent())
     self.assertNotEqual(b, c.get_parent())
     c.move(b, "first-child")
     b = self.reload(b)
     c = self.reload(c)
     self.assertEqual(b, c.get_parent())
 def test_delete(self):
     root = Category.add_root(name="test")
     root.save()
     child1 = root.add_child(name="Child 1")
     self.assertIn(child1, root.get_children())
     try:
         root.delete()
     except TypeError:
         self.fail('Deleting a node throws a TypeError.')
     except:
         self.fail('Deleting a node throws an exception.')
     self.assertNotIn(child1, Category.objects.all())
Beispiel #25
0
 def test_non_ascii_slug_generation(self):
     """Test slug generation for common non-ASCII types of characters"""
     root = Category.add_root(name="Root Node")
     root.save()
     child1 = root.add_child(name="Germanic umlauts: ä ö ü ß Ä Ö Ü")
     self.assertEquals(child1.slug, "germanic-umlauts-a-o-u-ss-a-o-u")
     child2 = root.add_child(name="Slavic Cyrillic: смачні пляцки")
     self.assertEquals(child2.slug, "slavic-cyrillic-smachni-pliatski")
     child3 = root.add_child(name="Simplified Chinese: 美味蛋糕")
     self.assertEquals(child3.slug, "simplified-chinese-mei-wei-dan-gao")
     # non-ascii only slug
     child4 = root.add_child(name="ß ў 美")
     self.assertEquals(child4.slug, "ss-u-mei")
 def test_non_ascii_slug_generation(self):
     """Test slug generation for common non-ASCII types of characters"""
     root = Category.add_root(name="Root Node")
     root.save()
     child1 = root.add_child(name="Germanic umlauts: ä ö ü ß Ä Ö Ü")
     self.assertEquals(child1.slug, "germanic-umlauts-a-o-u-ss-a-o-u")
     child2 = root.add_child(name="Slavic Cyrillic: смачні пляцки")
     self.assertEquals(child2.slug, "slavic-cyrillic-smachni-pliatski")
     child3 = root.add_child(name="Simplified Chinese: 美味蛋糕")
     self.assertEquals(child3.slug, "simplified-chinese-mei-wei-dan-gao")
     # non-ascii only slug
     child4 = root.add_child(name="ß ў 美")
     self.assertEquals(child4.slug, "ss-u-mei")
Beispiel #27
0
    def test_category_multiple_choice_field(self):
        root = Category.add_root(name="root")
        root.save()
        child1 = root.add_child(name="child1")
        child2 = root.add_child(name="child2")
        grandchild1 = child1.add_child(name="grandchild1")
        bad_grandchild = child1.add_child(
            name='bad grandchild<script>alert("bad stuff");</script>')
        root = self.reload(root)
        child1 = self.reload(child1)
        field = CategoryMultipleChoiceField(None)
        self.assertEqual(
            field.label_from_instance(child2),
            "&nbsp;&nbsp;child2",
        )
        self.assertEqual(
            field.label_from_instance(grandchild1),
            "&nbsp;&nbsp;&nbsp;&nbsp;grandchild1",
        )
        self.assertEqual(
            field.label_from_instance(bad_grandchild),
            '&nbsp;&nbsp;&nbsp;&nbsp;bad grandchild&lt;script&gt;alert'
            '(&quot;bad stuff&quot;);&lt;/script&gt;',
        )

        # Tests that the field correctly throws an ImproperlyConfigured
        # exception if the given object is not a Category (or something that
        # acts like one)
        with self.assertRaises(ImproperlyConfigured):
            field.label_from_instance(object)

        # Check that using an untranslated language does not raise exceptions
        with switch_language(child1, 'it'):
            try:
                field.label_from_instance(child1)
            except ImproperlyConfigured:
                self.fail("Translating to an unavailable language should not "
                          "result in an exception.")
 def test_tree_depth(self):
     a = Category.add_root(name="A")
     b = a.add_child(name="B")
     c = b.add_child(name="C")
     self.assertEqual(c.depth, 3)
Beispiel #29
0
 def test_tree_depth(self):
     a = Category.add_root(name="A")
     b = a.add_child(name="B")
     c = b.add_child(name="C")
     self.assertEqual(c.depth, 3)
 def test_category_slug_creation(self):
     name = "Root Node"
     root = Category.add_root(name=name)
     root.set_current_language("en")
     root.save()
     self.assertEquals(root.slug, "root-node")
Beispiel #31
0
 def test_create_in_mem_category(self):
     name = "Root Node"
     root = Category.add_root(name=name)
     root.set_current_language("en")
     self.assertEquals(root.name, "Root Node")
Beispiel #32
0
 def test_str_malicious(self):
     malicious = "<script>alert('hi');</script>"
     escaped = "&lt;script&gt;alert(&#39;hi&#39;);&lt;/script&gt;"
     root = Category.add_root(name=malicious)
     root.save()
     self.assertEqual(six.u(str(root)), escaped)
Beispiel #33
0
 def test_str(self):
     root = Category.add_root(name="test")
     root.save()
     self.assertEqual(root.name, str(root))
Beispiel #34
0
 def test_category_slug_creation(self):
     name = "Root Node"
     root = Category.add_root(name=name)
     root.set_current_language("en")
     root.save()
     self.assertEquals(root.slug, "root-node")
 def test_create_in_mem_category(self):
     name = "Root Node"
     root = Category.add_root(name=name)
     root.set_current_language("en")
     self.assertEquals(root.name, "Root Node")
 def test_str(self):
     root = Category.add_root(name="test")
     root.save()
     self.assertEqual(root.name, str(root))