Skip to content

Commit

Permalink
section creation moved from model save to wagtail hook
Browse files Browse the repository at this point in the history
test case added for new hook "after_create_page"
  • Loading branch information
marslanabdulrauf committed Nov 20, 2024
1 parent f508324 commit beec683
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 20 deletions.
18 changes: 0 additions & 18 deletions cms/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
"""
Expand Down
50 changes: 50 additions & 0 deletions cms/models_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -57,7 +59,9 @@
)
from cms.models import (
CertificatePage,
CourseIndexPage,
CoursesInProgramPage,
ExternalCoursePage,
ForTeamsPage,
FrequentlyAskedQuestionPage,
LearningJourneySection,
Expand Down Expand Up @@ -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"]
5 changes: 3 additions & 2 deletions cms/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from copy import deepcopy
from pathlib import Path

from django.core.files import File
Expand All @@ -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:
Expand All @@ -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(
Expand Down
19 changes: 19 additions & 0 deletions cms/wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

0 comments on commit beec683

Please sign in to comment.