diff --git a/blog/tests/test_views.py b/blog/tests/test_views.py index 4be9d8d9..bf626b5b 100644 --- a/blog/tests/test_views.py +++ b/blog/tests/test_views.py @@ -107,6 +107,4 @@ def test_deep_blog_works(self): self.assertPageIsRenderable(new_blog_post) response = self.client.get(deep_blog_index_page.url + "rss/") - print(deep_blog_index_page.url + "rss/") - print(response) self.assertEqual(response.status_code, 200) diff --git a/content_manager/blocks.py b/content_manager/blocks.py index 9c5f7159..6038de1e 100644 --- a/content_manager/blocks.py +++ b/content_manager/blocks.py @@ -757,8 +757,6 @@ def enlarge_link(self): if len(call_to_action): enlarge = False elif len(tags): - print(tags) - print(tags.raw_data) tags_list = tags.raw_data for tag in tags_list: if ( diff --git a/content_manager/management/commands/create_demo_pages.py b/content_manager/management/commands/create_demo_pages.py index d8dd3e96..150e0b96 100644 --- a/content_manager/management/commands/create_demo_pages.py +++ b/content_manager/management/commands/create_demo_pages.py @@ -1,7 +1,6 @@ from django.core.management.base import BaseCommand from faker import Faker from taggit.models import slugify -from wagtail.models import Site from wagtail.rich_text import RichText from wagtailmenus.models.menuitems import FlatMenuItem, MainMenuItem from wagtailmenus.models.menus import FlatMenu, MainMenu @@ -9,6 +8,7 @@ from blog.models import BlogIndexPage from content_manager.models import ContentPage, MegaMenu, MegaMenuCategory from content_manager.services.accessors import get_or_create_catalog_index_page, get_or_create_content_page +from content_manager.utils import get_default_site from forms.models import FormField, FormPage ALL_ALLOWED_SLUGS = ["blog_index", "publications", "menu_page", "form"] @@ -32,7 +32,7 @@ def handle(self, *args, **kwargs): if not slugs: slugs = ALL_ALLOWED_SLUGS - site = Site.objects.filter(is_default_site=True).first() + site = get_default_site() # First, add the home page to the main menu if not already done home_page = site.root_page diff --git a/content_manager/management/commands/create_starter_pages.py b/content_manager/management/commands/create_starter_pages.py index f09b3fed..19defdbe 100644 --- a/content_manager/management/commands/create_starter_pages.py +++ b/content_manager/management/commands/create_starter_pages.py @@ -3,12 +3,13 @@ from django.core.management.base import BaseCommand from django.urls import reverse from wagtail.images.models import Image -from wagtail.models import Page, Site +from wagtail.models import Page from wagtail.rich_text import RichText from wagtailmenus.models.menuitems import FlatMenuItem, MainMenuItem from content_manager.models import ContentPage from content_manager.services.accessors import get_or_create_footer_menu, get_or_create_main_menu +from content_manager.utils import get_default_site from forms.models import FormField, FormPage ALL_ALLOWED_SLUGS = ["home", "mentions-legales", "accessibilite", "contact"] @@ -134,7 +135,7 @@ def create_homepage(self) -> None: home_page = root.add_child(instance=ContentPage(title=title, body=body, show_in_menus=True)) # Define it as default for the default site - site = Site.objects.filter(is_default_site=True).first() + site = get_default_site() site.root_page_id = home_page.id site.save() @@ -159,7 +160,7 @@ def create_page(self, slug: str, title: str, body: list, footer_label: str = "") self.stdout.write(f"The {slug} page seem to already exist with id {already_exists.id}") return - home_page = Site.objects.filter(is_default_site=True).first().root_page + home_page = get_default_site().root_page new_page = home_page.add_child(instance=ContentPage(title=title, body=body, slug=slug, show_in_menus=True)) footer_menu = get_or_create_footer_menu() @@ -199,7 +200,7 @@ def create_contact_page(self, slug: str = "contact") -> None: thank_you_text = RichText("
Merci pour votre message ! Nous reviendrons vers vous rapidement.
") - default_site = Site.objects.filter(is_default_site=True).first() + default_site = get_default_site() home_page = default_site.root_page contact_page = home_page.add_child( instance=FormPage(title=title, slug=slug, intro=intro, thank_you_text=thank_you_text, show_in_menus=True) diff --git a/content_manager/management/commands/migrate_pages.py b/content_manager/management/commands/migrate_pages.py index 65c9a86f..fb0fea1e 100644 --- a/content_manager/management/commands/migrate_pages.py +++ b/content_manager/management/commands/migrate_pages.py @@ -1,9 +1,9 @@ from pathlib import Path from django.core.management.base import BaseCommand -from wagtail.models import Site from content_manager.services.import_export import ExportPage, ImportExportImages, ImportPages +from content_manager.utils import get_default_site SOURCE_URL = "https://sites-faciles.beta.numerique.gouv.fr/" @@ -37,7 +37,7 @@ def handle(self, *args, **kwargs): parent_page_slug = kwargs.get("parent_page_slug") if not parent_page_slug: - site = Site.objects.filter(is_default_site=True).first() + site = get_default_site() parent_page_slug = site.root_page.slug image_folder = Path("/tmp/sf_img") diff --git a/content_manager/management/commands/set_config.py b/content_manager/management/commands/set_config.py index d06a5309..84e28ae8 100644 --- a/content_manager/management/commands/set_config.py +++ b/content_manager/management/commands/set_config.py @@ -3,9 +3,10 @@ from django.conf import settings from django.core.management.base import BaseCommand -from wagtail.models import Group, Site +from wagtail.models import Group from content_manager.models import CmsDsfrConfig +from content_manager.utils import get_default_site class Command(BaseCommand): @@ -22,7 +23,7 @@ def handle(self, *args, **kwargs): without the port or http/https protocol.""" ) - site = Site.objects.filter(is_default_site=True).first() + site = get_default_site() site.hostname = settings.HOST_URL site.site_name = settings.WAGTAIL_SITE_NAME site.save() diff --git a/content_manager/services/accessors.py b/content_manager/services/accessors.py index 286bec67..a1318135 100644 --- a/content_manager/services/accessors.py +++ b/content_manager/services/accessors.py @@ -1,12 +1,13 @@ import sys from django.core.management.color import color_style -from wagtail.models import Collection, PageViewRestriction, Site +from wagtail.models import Collection, Page, PageViewRestriction from wagtailmenus.models.menuitems import MainMenuItem from wagtailmenus.models.menus import FlatMenu, MainMenu from content_manager.constants import HEADER_FIELDS from content_manager.models import CatalogIndexPage, ContentPage +from content_manager.utils import get_default_site style = color_style() @@ -30,7 +31,7 @@ def get_or_create_catalog_index_page( slug: str, title: str, body: list, - parent_page: ContentPage | None = None, + parent_page: Page | ContentPage | None = None, restriction_type: str | None = None, page_fields: dict | None = None, ) -> CatalogIndexPage: @@ -38,12 +39,18 @@ def get_or_create_catalog_index_page( Get a CatalogIndexPage if it exists, or creates it instead. """ - site = Site.objects.filter(is_default_site=True).first() + site = get_default_site() root_page = site.root_page locale = root_page.locale - # If parent_page is not passed as parameter, use the Home page of the default site. - if not parent_page: + if parent_page: + if not isinstance(parent_page, (Page, ContentPage)): + # Default "Page" type is allowed to allow the default root page" + raise TypeError("The parent page should be a content page.") + else: + # If parent_page is not passed as parameter, use the Home page of the default site. + if not isinstance(root_page, (Page, ContentPage)): + raise TypeError("The parent page should be a content page.") parent_page = root_page # Don't replace or duplicate an already existing page @@ -80,7 +87,7 @@ def get_or_create_content_page( slug: str, title: str, body: list, - parent_page: ContentPage | CatalogIndexPage | None = None, + parent_page: Page | ContentPage | CatalogIndexPage | None = None, restriction_type: str | None = None, page_fields: dict | None = None, ) -> ContentPage: @@ -88,12 +95,18 @@ def get_or_create_content_page( Get a ContentPage if it exists, or creates it instead. """ - site = Site.objects.filter(is_default_site=True).first() + site = get_default_site() root_page = site.root_page locale = root_page.locale - # If parent_page is not passed as parameter, use the Home page of the default site. - if not parent_page: + if parent_page: + if not isinstance(parent_page, (Page, ContentPage, CatalogIndexPage)): + # Default "Page" type is allowed to allow the default root page" + raise TypeError("The parent page should be a content page or a catalog index page.") + else: + # If parent_page is not passed as parameter, use the Home page of the default site. + if not isinstance(root_page, (Page, ContentPage, CatalogIndexPage)): + raise TypeError("The parent page should be a content page or a catalog index page.") parent_page = root_page # Don't replace or duplicate an already existing page @@ -133,7 +146,7 @@ def get_or_create_footer_menu() -> FlatMenu: In any case, return it. """ - default_site = Site.objects.filter(is_default_site=True).first() + default_site = get_default_site() footer_menu = FlatMenu.objects.filter(handle="footer", site=default_site).first() if not footer_menu: @@ -149,7 +162,7 @@ def get_or_create_main_menu() -> MainMenu: In any case, return it. """ - default_site = Site.objects.filter(is_default_site=True).first() + default_site = get_default_site() main_menu = MainMenu.objects.filter(site=default_site).first() if not main_menu: diff --git a/content_manager/tests/test_views.py b/content_manager/tests/test_views.py index 91a53223..0e1aa99c 100644 --- a/content_manager/tests/test_views.py +++ b/content_manager/tests/test_views.py @@ -1,12 +1,13 @@ from django.contrib.auth.models import User from django.core.management import call_command -from wagtail.models import Page, Site +from wagtail.models import Page from wagtail.rich_text import RichText from wagtail.test.utils import WagtailPageTestCase from wagtailmenus.models.menuitems import FlatMenuItem, MainMenuItem from wagtailmenus.models.menus import FlatMenu, MainMenu from content_manager.models import CatalogIndexPage, CmsDsfrConfig, ContentPage, MegaMenu, MegaMenuCategory +from content_manager.utils import get_default_site class ContentPageTestCase(WagtailPageTestCase): @@ -163,7 +164,7 @@ def setUpTestData(cls) -> None: call_command("create_starter_pages") def setUp(self) -> None: - self.site = Site.objects.filter(is_default_site=True).first() + self.site = get_default_site() self.home_page = self.site.root_page self.main_menu = MainMenu.objects.first() diff --git a/content_manager/utils.py b/content_manager/utils.py index 8e6d69ab..a482e19b 100644 --- a/content_manager/utils.py +++ b/content_manager/utils.py @@ -5,6 +5,7 @@ from bs4 import BeautifulSoup from django.core.files.images import ImageFile from wagtail.images.models import Image +from wagtail.models import Site def import_image(full_path: str, title: str) -> Image: @@ -20,6 +21,17 @@ def import_image(full_path: str, title: str) -> Image: return image +def get_default_site() -> Site: + """ + Returns the default site, or the first one if none is set. + """ + site = Site.objects.filter(is_default_site=True).first() + if not site: + site = Site.objects.filter().first() + + return site + + def get_streamblock_raw_text(block) -> str: """ Get the raw text of a streamblock.