From beec68367f76c90eab25dacc4534c0c37d8e1d32 Mon Sep 17 00:00:00 2001 From: Muhammad Arslan Date: Wed, 20 Nov 2024 20:36:32 +0500 Subject: [PATCH] section creation moved from model save to wagtail hook test case added for new hook "after_create_page" --- cms/models.py | 18 ---------------- cms/models_test.py | 50 ++++++++++++++++++++++++++++++++++++++++++++ cms/utils.py | 5 +++-- cms/wagtail_hooks.py | 19 +++++++++++++++++ 4 files changed, 72 insertions(+), 20 deletions(-) diff --git a/cms/models.py b/cms/models.py index 4550690b1..ce99a1e85 100644 --- a/cms/models.py +++ b/cms/models.py @@ -78,7 +78,6 @@ CatalogSorting, ) from cms.forms import CertificatePageForm, CoursewareForm -from cms.utils import create_b2b_section, create_how_you_will_learn_section from courses.constants import DEFAULT_COURSE_IMG_PATH, PROGRAM_RUN_ID_PATTERN from courses.models import ( Course, @@ -1488,23 +1487,6 @@ class ExternalCoursePage(CourseProductPage): template = "product_page.html" - def save(self, *args, **kwargs): - if not self.id: - super().save(*args, **kwargs) - return - - icongrid_page = self.get_child_page_of_type_including_draft( - LearningTechniquesPage - ) - if not icongrid_page: - icongrid_page = create_how_you_will_learn_section() - self.add_child(instance=icongrid_page) - - b2b_page = self.get_child_page_of_type_including_draft(ForTeamsPage) - if not b2b_page: - b2b_page = create_b2b_section() - self.add_child(instance=b2b_page) - class ExternalProgramPage(ProgramProductPage): """ diff --git a/cms/models_test.py b/cms/models_test.py index 119b7374d..fac995f72 100644 --- a/cms/models_test.py +++ b/cms/models_test.py @@ -14,9 +14,11 @@ from wagtail.test.utils.form_data import querydict_from_html from cms.constants import ( + B2B_SECTION, FORMAT_HYBRID, FORMAT_ONLINE, FORMAT_OTHER, + HOW_YOU_WILL_LEARN_SECTION, ON_DEMAND_WEBINAR, ON_DEMAND_WEBINAR_BUTTON_TITLE, UPCOMING_WEBINAR, @@ -57,7 +59,9 @@ ) from cms.models import ( CertificatePage, + CourseIndexPage, CoursesInProgramPage, + ExternalCoursePage, ForTeamsPage, FrequentlyAskedQuestionPage, LearningJourneySection, @@ -2089,3 +2093,49 @@ def test_certificatepage_saved_no_signatories_external_courseware( resp = superuser_client.post(path, data_to_post) assert resp.status_code == 302 + + +def test_external_course_page_auto_created_sections(superuser_client): + """ + Creating ExternalCoursePage should also create sub pages: + 1. LearningTechniquesPage + 2. ForTeamsPage + """ + external_course_page_slug = "icon-grid-6064" + course = CourseFactory.create() + post_data = { + "course": course.id, + "title": "Icon Grid #6064", + "subhead": "testing #6064", + "format": "Online", + "content-count": 0, + "slug": external_course_page_slug, + "action-publish": "action-publish", + } + response = superuser_client.post( + reverse( + "wagtailadmin_pages:add", + args=("cms", "externalcoursepage", CourseIndexPage.objects.first().id), + ), + post_data, + ) + + assert response.status_code == 302 + external_course_page = ExternalCoursePage.objects.get( + slug=external_course_page_slug + ) + assert external_course_page.course.id == course.id + assert len(external_course_page.child_pages) >= 2 + learning_technical_page = ( + external_course_page.get_child_page_of_type_including_draft( + LearningTechniquesPage + ) + ) + for_teams_page = external_course_page.get_child_page_of_type_including_draft( + ForTeamsPage + ) + assert learning_technical_page + assert for_teams_page + assert learning_technical_page.title == HOW_YOU_WILL_LEARN_SECTION["title"] + assert len(learning_technical_page.technique_items) == 6 + assert for_teams_page.title == B2B_SECTION["title"] diff --git a/cms/utils.py b/cms/utils.py index 822ffecf6..9bf669dc2 100644 --- a/cms/utils.py +++ b/cms/utils.py @@ -1,3 +1,4 @@ +from copy import deepcopy from pathlib import Path from django.core.files import File @@ -9,7 +10,7 @@ def create_how_you_will_learn_section(): from cms.models import LearningTechniquesPage - section_content = HOW_YOU_WILL_LEARN_SECTION.copy() + section_content = deepcopy(HOW_YOU_WILL_LEARN_SECTION) for technique in section_content["technique_items"]: image_title = technique["value"]["heading"] with Path(technique["value"]["image"]).open("rb") as img: @@ -25,7 +26,7 @@ def create_how_you_will_learn_section(): def create_b2b_section(): from cms.models import ForTeamsPage - section_content = B2B_SECTION.copy() + section_content = deepcopy(B2B_SECTION) with Path(section_content["image"]).open("rb") as img: img_file = File(img) image, _ = Image.objects.get_or_create( diff --git a/cms/wagtail_hooks.py b/cms/wagtail_hooks.py index 809a9c5a6..cb4b5aa3e 100644 --- a/cms/wagtail_hooks.py +++ b/cms/wagtail_hooks.py @@ -4,6 +4,8 @@ from wagtail import hooks from wagtail.admin.api.views import PagesAdminAPIViewSet +from cms.models import ExternalCoursePage, ForTeamsPage, LearningTechniquesPage +from cms.utils import create_b2b_section, create_how_you_will_learn_section from courses.models import CourseRun, Program from ecommerce.models import Product, ProductVersion @@ -73,3 +75,20 @@ def create_product_and_versions_for_courseware_pages(request, page): ProductVersion.objects.create( product=product, price=price, description=page.program.text_id ) + + +@hooks.register("after_create_page") +def create_how_you_will_learn_and_b2b_sections(request, page): # noqa: ARG001 + if not isinstance(page, ExternalCoursePage): + # We need to create sections only for External Course Pages + return + + icongrid_page = page.get_child_page_of_type_including_draft(LearningTechniquesPage) + if not icongrid_page: + icongrid_page = create_how_you_will_learn_section() + page.add_child(instance=icongrid_page) + + b2b_page = page.get_child_page_of_type_including_draft(ForTeamsPage) + if not b2b_page: + b2b_page = create_b2b_section() + page.add_child(instance=b2b_page)