-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Migration des pages flatpage vers wagtail (#1496)
- Loading branch information
Showing
25 changed files
with
229 additions
and
765 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import json | ||
|
||
from django.core.management.base import BaseCommand | ||
from wagtail.models import Site | ||
|
||
from content_manager.models import ContentPage | ||
|
||
|
||
class Command(BaseCommand): | ||
help = """ | ||
Creates a series of content pages. | ||
""" | ||
|
||
def handle(self, *args, **kwargs): | ||
try: | ||
with open("lemarche/fixtures/cms_content_pages.json") as f: | ||
pages_data = json.load(f) | ||
except FileNotFoundError: | ||
self.stdout.write( | ||
self.style.ERROR("Le fichier content_manager/fixtures/cms_content_pages.json n'existe pas.") | ||
) | ||
return | ||
except json.JSONDecodeError: | ||
self.stdout.write( | ||
self.style.ERROR( | ||
"Le fichier content_manager/fixtures/cms_content_pages.json n'est pas un fichier JSON valide." | ||
) | ||
) | ||
return | ||
|
||
home_page = Site.objects.filter(is_default_site=True).first().root_page | ||
|
||
for page_data in pages_data: | ||
slug = page_data["slug"] | ||
title = page_data["title"] | ||
body = page_data.get("body", []) | ||
|
||
self.create_content_page(slug, title, body, home_page) | ||
|
||
def create_content_page(self, slug: str, title: str, body: list, parent_page: ContentPage) -> ContentPage: | ||
""" | ||
Creates a page for the site. | ||
""" | ||
|
||
# Don't replace or duplicate an already existing page | ||
already_exists = ContentPage.objects.filter(slug=slug).first() | ||
if already_exists: | ||
self.stdout.write(f"The {slug} page seem to already exist with id {already_exists.id}") | ||
return already_exists | ||
|
||
new_page = parent_page.add_child(instance=ContentPage(title=title, body=body, slug=slug, show_in_menus=True)) | ||
|
||
self.stdout.write(self.style.SUCCESS(f"Page {slug} created with id {new_page.id}")) | ||
|
||
return new_page |
25 changes: 25 additions & 0 deletions
25
lemarche/cms/migrations/0014_homepage_sub_header_custom_message.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Generated by Django 4.2.15 on 2024-10-23 10:31 | ||
|
||
import wagtail.fields | ||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("cms", "0013_alter_articlepage_intro_alter_faqpage_intro"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="homepage", | ||
name="sub_header_custom_message", | ||
field=wagtail.fields.StreamField( | ||
[("message", 0)], | ||
blank=True, | ||
block_lookup={0: ("wagtail.blocks.RichTextBlock", (), {"label": "Message personnalisé du bandeau"})}, | ||
help_text="Contenu affiché dans le bandeau sous l'en-tête.", | ||
null=True, | ||
verbose_name="Message personnalisé du bandeau", | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import json | ||
|
||
from django.core.management import call_command | ||
from django.test import Client, TestCase | ||
from wagtail.models import Site | ||
|
||
from content_manager.models import ContentPage | ||
|
||
|
||
class CreateContentPagesCommandTests(TestCase): | ||
def test_create_content_pages_with_json(self): | ||
client = Client() | ||
with open("lemarche/fixtures/cms_content_pages.json") as f: | ||
pages_data = json.load(f) | ||
|
||
call_command("create_content_pages") | ||
|
||
# Check that the pages were created | ||
for page_data in pages_data: | ||
slug = page_data["slug"] | ||
self.assertTrue( | ||
ContentPage.objects.filter(slug=slug).exists(), msg=f"La page avec le slug '{slug}' n'a pas été créée." | ||
) | ||
|
||
response = client.get(f"/{slug}/") | ||
self.assertEqual( | ||
response.status_code, | ||
200, | ||
msg=f"La page avec le slug '{slug}' n'est pas accessible (status: {response.status_code}).", | ||
) | ||
|
||
def test_prevent_duplicate_page_creation(self): | ||
"""Ensure the command does not create duplicate pages""" | ||
home_page = Site.objects.get(is_default_site=True).root_page | ||
home_page.add_child(instance=ContentPage(slug="mentions-legales", title="Mentions légales")) | ||
|
||
call_command("create_content_pages") | ||
|
||
self.assertEqual( | ||
ContentPage.objects.filter(slug="mentions-legales").count(), | ||
1, | ||
msg="Une page en double a été créée pour le slug 'mentions-legales'.", | ||
) |
Oops, something went wrong.