Ejemplo n.º 1
0
    def test_models_category_get_persons_ordering(self):
        """The related persons should be sorted by their position in the pages tree."""
        category = CategoryFactory(should_publish=True)
        person1, person2, person3 = PersonFactory.create_batch(
            3, fill_categories=[category], should_publish=True)
        self.assertEqual(list(category.get_persons()),
                         [person1, person2, person3])

        # Move pages in the tree and check that they are returned in the new order
        person3.extended_object.move_page(person1.extended_object.node,
                                          position="left")
        self.assertEqual(list(category.get_persons()),
                         [person3, person1, person2])

        person1.extended_object.move_page(person3.extended_object.node,
                                          position="left")
        self.assertEqual(list(category.get_persons()),
                         [person1, person3, person2])
Ejemplo n.º 2
0
 def test_models_category_get_persons_several_languages(self):
     """
     The persons should not be duplicated if they exist in several languages.
     """
     category = CategoryFactory(should_publish=True)
     PersonFactory(
         page_title={"en": "my title", "fr": "mon titre"},
         fill_categories=[category],
         should_publish=True,
     )
     self.assertEqual(Person.objects.count(), 2)
     self.assertEqual(category.get_persons().count(), 1)
Ejemplo n.º 3
0
    def test_models_category_get_persons_descendants(self):
        """
        Related persons should include the persons linked to the category's descendants,
        unless specifically deactivated by the "include_descendants" argument.
        """
        category_page = create_page("Subjects",
                                    "courses/cms/category_detail.html",
                                    "en",
                                    published=True)
        category = CategoryFactory(extended_object=category_page,
                                   should_publish=True)
        persons = PersonFactory.create_batch(2,
                                             fill_categories=[category],
                                             should_publish=True)

        child_category_page = create_page(
            "Literature",
            "courses/cms/category_detail.html",
            "en",
            parent=category_page,
            published=True,
        )
        child_category = CategoryFactory(extended_object=child_category_page,
                                         should_publish=True)
        persons_child = PersonFactory.create_batch(
            2, fill_categories=[child_category], should_publish=True)

        grand_child_category_page = create_page(
            "Literature",
            "courses/cms/category_detail.html",
            "en",
            parent=child_category_page,
            published=True,
        )
        grand_child_category = CategoryFactory(
            extended_object=grand_child_category_page, should_publish=True)
        persons_grand_child = PersonFactory.create_batch(
            2, fill_categories=[grand_child_category], should_publish=True)

        # Check that each category gets persons from its descendants
        # ...unless we pass an argument to deactivate it
        self.assertEqual(list(category.get_persons()),
                         persons + persons_child + persons_grand_child)
        self.assertEqual(list(category.get_persons(include_descendants=False)),
                         persons)

        self.assertEqual(list(child_category.get_persons()),
                         persons_child + persons_grand_child)
        self.assertEqual(
            list(child_category.get_persons(include_descendants=False)),
            persons_child)

        self.assertEqual(
            list(grand_child_category.get_persons(include_descendants=False)),
            persons_grand_child,
        )
        self.assertEqual(list(grand_child_category.get_persons()),
                         persons_grand_child)
Ejemplo n.º 4
0
    def test_models_category_get_persons(self):
        """
        It should be possible to retrieve the list of related persons on the category
        instance. The number of queries should be minimal.
        """
        category = CategoryFactory(should_publish=True)
        persons = PersonFactory.create_batch(
            2, page_title="my title", fill_categories=[category], should_publish=True
        )
        retrieved_persons = category.get_persons()

        with self.assertNumQueries(2):
            self.assertEqual(set(retrieved_persons), set(persons))

        with self.assertNumQueries(0):
            for person in retrieved_persons:
                self.assertEqual(
                    person.extended_object.prefetched_titles[0].title, "my title"
                )
Ejemplo n.º 5
0
    def test_models_category_get_persons_public_category_page(self):
        """
        When a category is added on a draft person, the person should not be visible on
        the public category page until the person is published.
        """
        category = CategoryFactory(should_publish=True)
        category_page = category.extended_object
        person = PersonFactory(page_title="my title", should_publish=True)
        person_page = person.extended_object

        # Add a category to the person but don't publish the modification
        placeholder = person_page.placeholders.get(slot="categories")
        add_plugin(placeholder, CategoryPlugin, "en", page=category_page)

        self.assertEqual(list(category.get_persons()), [person])
        self.assertEqual(list(category.public_extension.get_persons()), [])

        # Now publish the modification and check that the person is displayed
        # on the public category page
        person.extended_object.publish("en")
        self.assertEqual(list(category.public_extension.get_persons()),
                         [person.public_extension])