Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
)
def test_models_person_get_courses(self):
"""
It should be possible to retrieve the list of related courses on the person instance.
The number of queries should be minimal.
"""
person = PersonFactory(should_publish=True)
courses = CourseFactory.create_batch(
3, page_title="my title", fill_team=[person], should_publish=True
)
retrieved_courses = person.get_courses()
with self.assertNumQueries(2):
self.assertEqual(set(retrieved_courses), set(courses))
with self.assertNumQueries(0):
for course in retrieved_courses:
self.assertEqual(
course.extended_object.prefetched_titles[0].title, "my title"
)
def test_templates_organization_detail_related_persons(self):
"""
Persons related to an organization via a plugin should appear on the organization
detail page.
"""
user = UserFactory(is_staff=True, is_superuser=True)
self.client.login(username=user.username, password="password")
organization = OrganizationFactory()
person = PersonFactory(fill_organizations=[organization])
page = organization.extended_object
url = page.get_absolute_url()
response = self.client.get(url)
# The person should be present on the page
pattern = (
r'<a href="{url:s}">'
r'<h3 class="person-glimpse__title">'
r".*{name:s}.*</h3></a>"
).format(
url=person.extended_object.get_absolute_url(),
name=person.extended_object.get_title(),
)
self.assertIsNotNone(re.search(pattern, str(response.content)))
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)
def test_indexers_persons_get_es_documents(self, _mock_picture):
"""
Happy path: person data is fetched from the models properly formatted
"""
person1 = PersonFactory(
fill_portrait=True,
page_title={"en": "my first person", "fr": "ma première personne"},
should_publish=True,
)
person2 = PersonFactory(
page_title={"en": "my second person", "fr": "ma deuxième personne"},
should_publish=True,
)
# Add a description in several languages to the first person
placeholder = person1.public_extension.extended_object.placeholders.get(
slot="bio"
)
plugin_params = {"placeholder": placeholder, "plugin_type": "CKEditorPlugin"}
add_plugin(body="english bio line 1.", language="en", **plugin_params)
add_plugin(body="english bio line 2.", language="en", **plugin_params)
def test_models_organization_get_persons_several_languages(self):
"""
The persons should not be duplicated if they exist in several languages.
"""
organization = OrganizationFactory(should_publish=True)
PersonFactory(
page_title={"en": "my title", "fr": "mon titre"},
fill_organizations=[organization],
should_publish=True,
)
self.assertEqual(Person.objects.count(), 2)
self.assertEqual(organization.get_persons().count(), 1)
def test_models_person_get_courses_several_languages(self):
"""
The courses should not be duplicated if they exist in several languages.
"""
person = PersonFactory(should_publish=True)
CourseFactory(
page_title={"en": "my title", "fr": "mon titre"},
fill_team=[person],
should_publish=True,
)
self.assertEqual(Course.objects.count(), 2)
self.assertEqual(person.get_courses().count(), 1)
def test_models_organization_get_persons(self):
"""
It should be possible to retrieve the list of related persons on the organization instance.
The number of queries should be minimal.
"""
organization = OrganizationFactory(should_publish=True)
persons = PersonFactory.create_batch(
2,
page_title="my title",
should_publish=True,
fill_organizations=[organization],
)
retrieved_persons = organization.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_cms_plugins_person_render_on_public_page(self):
"""
The person plugin should render as expected on a public page.
"""
# Create a Person
person = PersonFactory(
page_title={"en": "person title", "fr": "titre personne"},
fill_portrait={
"original_filename": "portrait.jpg",
"default_alt_text": "my portrait",
},
)
person_page = person.extended_object
# Add bio to related placeholder
bio_placeholder = person_page.placeholders.get(slot="bio")
bio_en = add_plugin(
bio_placeholder, PlainTextPlugin, "en", **{"body": "public bio"}
)
add_plugin(bio_placeholder, PlainTextPlugin, "fr", **{"body": "résumé public"})
# Create a page to add the plugin to
fill_logo=pick_image("logo"),
should_publish=True,
with_permissions=True,
)
)
# Create persons under the `persons` page
persons = []
persons_for_organization = defaultdict(list)
for _ in range(defaults.NB_OBJECTS["persons"]):
# Randomly assign each person to a set of organizations
person_organizations = random.sample(
organizations,
random.randint(1, defaults.NB_OBJECTS["person_organizations"]), # nosec
)
person = factories.PersonFactory(
page_in_navigation=True,
page_languages=["en", "fr"],
page_parent=pages_created["persons"],
fill_categories=random.sample(
subjects,
random.randint(1, defaults.NB_OBJECTS["person_subjects"]), # nosec
),
fill_organizations=person_organizations,
fill_portrait=pick_image("portrait"),
fill_bio=True,
fill_maincontent=True,
should_publish=True,
)
persons.append(person)
for organization in person_organizations:
persons_for_organization[organization.id].append(person)