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)
def test_models_course_get_persons_other_placeholders(self): """ The `get_persons` method should return all persons linked to a course via a plugin on whichever placeholder. """ person1, person2 = PersonFactory.create_batch(2) course = CourseFactory(should_publish=True) placeholder1 = course.extended_object.placeholders.get( slot="course_description") placeholder2 = course.extended_object.placeholders.get( slot="course_format") add_plugin( language="en", placeholder=placeholder1, plugin_type="PersonPlugin", page=person1.extended_object, ) add_plugin( language="en", placeholder=placeholder2, plugin_type="PersonPlugin", page=person2.extended_object, ) self.assertEqual(list(course.get_persons()), [person1, person2])
def test_models_course_get_persons(self): """ The `get_persons` method should return all persons linked to a course and should respect publication status. """ # The 2 first persons are grouped in one variable name and will be linked to the # course in the following, the third person will not be linked so we can check that # only the persons linked to the course are retrieved (its name starts with `_` # because it is not used and only here for unpacking purposes) *draft_persons, _other_draft = PersonFactory.create_batch(3) *published_persons, _other_public = PersonFactory.create_batch( 3, should_publish=True) course = CourseFactory(fill_team=draft_persons + published_persons, should_publish=True) self.assertEqual(list(course.get_persons()), draft_persons + published_persons) self.assertEqual(list(course.public_extension.get_persons()), published_persons)
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])
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" )
def test_models_organization_get_persons_language_fallback_published(self): """ Validate that the reverse persons lookup works as expected with language fallback on a published page. """ organization1, organization2, organization3 = OrganizationFactory.create_batch( 3, should_publish=True) public_organization1 = organization1.public_extension public_organization2 = organization2.public_extension public_organization3 = organization3.public_extension person, person_unpublished = PersonFactory.create_batch( 2, page_languages=["en", "fr", "de"], should_publish=True) public_person = person.public_extension public_person_unpublished = person_unpublished.public_extension person_unpublished.extended_object.unpublish("en") person_unpublished.extended_object.unpublish("fr") person_unpublished.extended_object.unpublish("de") placeholder = public_person.extended_object.placeholders.get( slot="organizations") placeholder_unpublished = ( public_person_unpublished.extended_object.placeholders.get( slot="organizations")) # Reverse plugin lookups should fallback up to the second priority language add_plugin( language="de", placeholder=placeholder, plugin_type="OrganizationPlugin", **{"page": organization1.extended_object}, ) add_plugin( language="de", placeholder=placeholder_unpublished, plugin_type="OrganizationPlugin", **{"page": organization1.extended_object}, ) with translation.override("en"): self.assertEqual(list(public_organization1.get_persons()), [public_person]) self.assertEqual(list(public_organization2.get_persons()), []) self.assertEqual(list(public_organization3.get_persons()), []) with translation.override("fr"): self.assertEqual(list(public_organization1.get_persons()), [public_person]) self.assertEqual(list(public_organization2.get_persons()), []) self.assertEqual(list(public_organization3.get_persons()), []) with translation.override("de"): self.assertEqual(list(public_organization1.get_persons()), [public_person]) self.assertEqual(list(public_organization2.get_persons()), []) self.assertEqual(list(public_organization3.get_persons()), []) # Reverse plugin lookups should fallback to the first priority language if available # and ignore the second priority language unless it is the current language add_plugin( language="fr", placeholder=placeholder, plugin_type="OrganizationPlugin", **{"page": organization2.extended_object}, ) add_plugin( language="fr", placeholder=placeholder_unpublished, plugin_type="OrganizationPlugin", **{"page": organization2.extended_object}, ) with translation.override("en"): self.assertEqual(list(public_organization1.get_persons()), []) self.assertEqual(list(public_organization2.get_persons()), [public_person]) self.assertEqual(list(public_organization3.get_persons()), []) with translation.override("fr"): self.assertEqual(list(public_organization1.get_persons()), []) self.assertEqual(list(public_organization2.get_persons()), [public_person]) self.assertEqual(list(public_organization3.get_persons()), []) with translation.override("de"): self.assertEqual(list(public_organization1.get_persons()), [public_person]) self.assertEqual(list(public_organization2.get_persons()), []) self.assertEqual(list(public_organization3.get_persons()), []) # Reverse plugin lookups should stick to the current language if available and # ignore plugins on fallback languages add_plugin( language="en", placeholder=placeholder, plugin_type="OrganizationPlugin", **{"page": organization3.extended_object}, ) add_plugin( language="en", placeholder=placeholder_unpublished, plugin_type="OrganizationPlugin", **{"page": organization3.extended_object}, ) with translation.override("en"): self.assertEqual(list(public_organization1.get_persons()), []) self.assertEqual(list(public_organization2.get_persons()), []) self.assertEqual(list(public_organization3.get_persons()), [public_person]) with translation.override("fr"): self.assertEqual(list(public_organization1.get_persons()), []) self.assertEqual(list(public_organization2.get_persons()), [public_person]) self.assertEqual(list(public_organization3.get_persons()), []) with translation.override("de"): self.assertEqual(list(public_organization1.get_persons()), [public_person]) self.assertEqual(list(public_organization2.get_persons()), []) self.assertEqual(list(public_organization3.get_persons()), [])
def test_models_person_get_courses_language_fallback_draft(self): """ Validate that the reverse courses lookup works as expected with language fallback on a draft page. """ person1, person2, person3 = PersonFactory.create_batch( 3, should_publish=True) course = CourseFactory(should_publish=True) placeholder = course.extended_object.placeholders.get( slot="course_team") cms_languages = { "default": { "public": True, "hide_untranslated": False, "redirect_on_fallback": False, "fallbacks": ["en", "fr", "de"], } } # Reverse plugin lookups should fallback up to the second priority language add_plugin( language="de", placeholder=placeholder, plugin_type="PersonPlugin", **{"page": person1.extended_object}, ) with override_settings(CMS_LANGUAGES=cms_languages): with translation.override("en"): self.assertEqual(list(person1.get_courses()), [course]) self.assertEqual(list(person2.get_courses()), []) self.assertEqual(list(person3.get_courses()), []) with translation.override("fr"): self.assertEqual(list(person1.get_courses()), [course]) self.assertEqual(list(person2.get_courses()), []) self.assertEqual(list(person3.get_courses()), []) with translation.override("de"): self.assertEqual(list(person1.get_courses()), [course]) self.assertEqual(list(person2.get_courses()), []) self.assertEqual(list(person3.get_courses()), []) # Reverse plugin lookups should fallback to the first priority language if available # and ignore the second priority language unless it is the current language add_plugin( language="fr", placeholder=placeholder, plugin_type="PersonPlugin", **{"page": person2.extended_object}, ) with override_settings(CMS_LANGUAGES=cms_languages): with translation.override("en"): self.assertEqual(list(person1.get_courses()), []) self.assertEqual(list(person2.get_courses()), [course]) self.assertEqual(list(person3.get_courses()), []) with translation.override("fr"): self.assertEqual(list(person1.get_courses()), []) self.assertEqual(list(person2.get_courses()), [course]) self.assertEqual(list(person3.get_courses()), []) with translation.override("de"): self.assertEqual(list(person1.get_courses()), [course]) self.assertEqual(list(person2.get_courses()), []) self.assertEqual(list(person3.get_courses()), []) # Reverse plugin lookups should stick to the current language if available and # ignore plugins on fallback languages add_plugin( language="en", placeholder=placeholder, plugin_type="PersonPlugin", **{"page": person3.extended_object}, ) with override_settings(CMS_LANGUAGES=cms_languages): with translation.override("en"): self.assertEqual(list(person1.get_courses()), []) self.assertEqual(list(person2.get_courses()), []) self.assertEqual(list(person3.get_courses()), [course]) with translation.override("fr"): self.assertEqual(list(person1.get_courses()), []) self.assertEqual(list(person2.get_courses()), [course]) self.assertEqual(list(person3.get_courses()), []) with translation.override("de"): self.assertEqual(list(person1.get_courses()), [course]) self.assertEqual(list(person2.get_courses()), []) self.assertEqual(list(person3.get_courses()), [])