Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def create_filter_pages():
"""
Create pages for each filter based on an indexable. We must create them in the same order
as they are instantiated in order to match the node paths we expect:
- subjects page path: 0001
- levels page path: 0002
- organizations page path: 0003
"""
CategoryFactory(page_reverse_id="subjects", should_publish=True)
CategoryFactory(page_reverse_id="levels", should_publish=True)
OrganizationFactory(page_reverse_id="organizations", should_publish=True)
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]
def test_templates_course_detail_cms_published_content(self):
"""
Validate that the important elements are displayed on a published course page
"""
categories = CategoryFactory.create_batch(4)
icons = CategoryFactory.create_batch(4, fill_icon=True)
organizations = OrganizationFactory.create_batch(4)
course = CourseFactory(
page_title="Very interesting course",
fill_organizations=organizations,
fill_categories=categories,
fill_icons=icons,
)
page = course.extended_object
# Create 2 ongoing open course runs
now = timezone.now()
course_run1, _course_run2 = CourseRunFactory.create_batch(
2,
page_parent=course.extended_object,
start=now - timedelta(hours=1),
def test_models_course_get_root_to_leaf_category_pages_duplicate(self):
"""
If the course is linked to several categories, the ancestor categories should not get
duplicated.
"""
# Create nested categories
create_page("Categories", "richie/single_column.html", "en")
meta_category = CategoryFactory(should_publish=True)
parent_category = CategoryFactory(
page_parent=meta_category.extended_object, should_publish=True
)
leaf_category1 = CategoryFactory(
page_parent=parent_category.extended_object, should_publish=True
)
leaf_category2 = CategoryFactory(
page_parent=parent_category.extended_object, should_publish=True
)
course = CourseFactory(
fill_categories=[leaf_category1, leaf_category2], should_publish=True
)
expected_pages = [
parent_category.public_extension.extended_object,
leaf_category1.public_extension.extended_object,
leaf_category2.public_extension.extended_object,
]
self.assertEqual(expected_pages, list(course.get_root_to_leaf_category_pages()))
def _extension_cms_draft_content(self, factory_model, control_string):
"""
Not a test. Sharing code for related page extension tests.
Validate how a draft category is displayed with its related page extensions.
"""
user = UserFactory(is_staff=True, is_superuser=True)
self.client.login(username=user.username, password="password")
category = CategoryFactory(page_title="Maths")
page = category.extended_object
# Organizations
published_extension = factory_model(
fill_categories=[category], should_publish=True
)
not_published_extension = factory_model(fill_categories=[category])
# Modify the draft version of the published page extension
title_obj = published_extension.extended_object.title_set.get(language="en")
title_obj.title = "modified extension"
title_obj.save()
# The page should be visible as draft to the staff user
url = page.get_absolute_url()
response = self.client.get(url)
def test_templates_category_detail_cms_published_content_max_courses_on_meta(
self, _mock_page_url
):
"""
Meta categories are a special case: technically, any page linked to one of their children
is linked to the meta-category too. This means in a lot of cases we just want a link to
*all* courses not just their related courses.
The Search view is also not equipped to filter on a meta-category. In this case, we just
link to it with a different link text.
"""
# Create our dummy category and the 3 courses we'll attach to it
meta = CategoryFactory(
page_parent=create_i18n_page(
{"en": "Categories", "fr": "Catégories"}, published=True
),
page_reverse_id="subjects",
page_title={"en": "Subjects", "fr": "Sujets"},
should_publish=True,
)
category = CategoryFactory(
page_parent=meta.extended_object, should_publish=True
)
courses = CourseFactory.create_batch(
3, fill_categories=[category], should_publish=True
)
# Link the 3 courses with our category through the relevant placeholder
for course in courses:
add_plugin(
def test_cms_plugins_organizations_by_category_form_page_choices(self):
"""
The form to create an organizations by category plugin should only list category pages
in the select box. There shouldn't be any duplicate because of published status.
"""
class OrganizationsByCategoryPluginModelForm(forms.ModelForm):
"""A form for testing the choices in the select box"""
class Meta:
model = OrganizationsByCategoryPluginModel
fields = ["page"]
category = CategoryFactory(should_publish=True)
PageFactory(title__title="other page", should_publish=True)
plugin_form = OrganizationsByCategoryPluginModelForm()
rendered_form = plugin_form.as_table()
self.assertEqual(rendered_form.count(category.extended_object.get_title()), 1)
self.assertNotIn("other", plugin_form.as_table())
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_models_category_get_organizations_descendants(self):
"""
Related organizations should include the organizations 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)
organizations = OrganizationFactory.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
)
organizations_child = OrganizationFactory.create_batch(
2, fill_categories=[child_category], should_publish=True
def test_models_category_get_courses_several_languages(self):
"""
The courses should not be duplicated if they exist in several languages.
"""
category = CategoryFactory(should_publish=True)
CourseFactory(
page_title={"en": "my title", "fr": "mon titre"},
fill_categories=[category],
should_publish=True,
)
self.assertEqual(Course.objects.count(), 2)
self.assertEqual(category.get_courses().count(), 1)