Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrated Wagtail to support course/program detail pages #264

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions cms/migrations/0015_courseindexpage_programindexpage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Generated by Django 2.1.7 on 2019-05-10 17:20

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
("wagtailcore", "0041_group_collection_permissions_verbose_name_plural"),
("cms", "0014_resourcepage"),
]

operations = [
migrations.CreateModel(
name="CourseIndexPage",
fields=[
(
"page_ptr",
models.OneToOneField(
auto_created=True,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to="wagtailcore.Page",
),
)
],
options={"abstract": False},
bases=("wagtailcore.page",),
),
migrations.CreateModel(
name="ProgramIndexPage",
fields=[
(
"page_ptr",
models.OneToOneField(
auto_created=True,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to="wagtailcore.Page",
),
)
],
options={"abstract": False},
bases=("wagtailcore.page",),
),
]
96 changes: 96 additions & 0 deletions cms/migrations/0016_course_program_index_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
"""
Data migration to ensure the correct state for course/program index pages and
correct depth for course/program detail pages
"""
from django.db import migrations

from wagtail.core.models import Page


COURSE_INDEX_PAGE_PROPERTIES = dict(title="Courses")
PROGRAM_INDEX_PAGE_PROPERTIES = dict(title="Programs")


def delete_wagtail_pages(specific_page_cls, filter_dict=None):
"""
Completely deletes Wagtail CMS pages that match a filter. Wagtail overrides standard delete functionality,
making it difficult to actually delete Page objects and get information about what was deleted.
"""
page_ids_to_delete = specific_page_cls.objects.values_list("id", flat=True)
if filter_dict:
page_ids_to_delete = page_ids_to_delete.filter(**filter_dict)
num_pages = len(page_ids_to_delete)
base_pages_qset = Page.objects.filter(id__in=page_ids_to_delete)
if not base_pages_qset.exists():
return 0, {}
base_pages_qset.delete()
return (
num_pages,
{specific_page_cls._meta.label: num_pages}, # pylint: disable=protected-access
)


def get_top_level_wagtail_page():
"""
The Wagtail CMS (at least in our usage) has one root page at depth 1, and one page at depth 2. All pages that we
create in Wagtail are added as children to the page at depth 2.
"""
return Page.objects.get(depth=2)


def create_index_pages_and_nest_detail(apps, schema_editor):
from cms.models import CourseIndexPage, ProgramIndexPage

CoursePage = apps.get_model("cms", "CoursePage")
ProgramPage = apps.get_model("cms", "ProgramPage")

# Add the course/program index pages
top_level_page = get_top_level_wagtail_page()
course_index = CourseIndexPage.objects.first()
if not course_index:
page_obj = CourseIndexPage(**COURSE_INDEX_PAGE_PROPERTIES)
course_index = top_level_page.add_child(instance=page_obj)
program_index = ProgramIndexPage.objects.first()
if not program_index:
page_obj = ProgramIndexPage(**PROGRAM_INDEX_PAGE_PROPERTIES)
program_index = top_level_page.add_child(instance=page_obj)
# Move course/program detail pages to be children of the course/program index pages
for page_id in CoursePage.objects.values_list("id", flat=True):
page = Page.objects.get(id=page_id)
page.move(course_index, "last-child")
for page_id in ProgramPage.objects.values_list("id", flat=True):
page = Page.objects.get(id=page_id)
page.move(program_index, "last-child")


def unnest_detail_and_delete_index_pages(apps, schema_editor):
CourseIndexPage = apps.get_model("cms", "CourseIndexPage")
ProgramIndexPage = apps.get_model("cms", "ProgramIndexPage")
CoursePage = apps.get_model("cms", "CoursePage")
ProgramPage = apps.get_model("cms", "ProgramPage")

# Move course/program detail pages to be children of the top-level page
top_level_page = get_top_level_wagtail_page()
top_level_child_ids = [child.id for child in top_level_page.get_children()]
for page_id in CoursePage.objects.values_list("id", flat=True):
if page_id not in top_level_child_ids:
page = Page.objects.get(id=page_id)
page.move(top_level_page, "last-child")
for page_id in ProgramPage.objects.values_list("id", flat=True):
if page_id not in top_level_child_ids:
page = Page.objects.get(id=page_id)
page.move(top_level_page, "last-child")
# Remove the course/program index pages
delete_wagtail_pages(ProgramIndexPage)
delete_wagtail_pages(CourseIndexPage)


class Migration(migrations.Migration):

dependencies = [("cms", "0015_courseindexpage_programindexpage")]

operations = [
migrations.RunPython(
create_index_pages_and_nest_detail, unnest_detail_and_delete_index_pages
)
]
Loading