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

Ajout d'une fonction utilitaire get_default_site() #250

Merged
merged 1 commit into from
Feb 18, 2025
Merged
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
2 changes: 0 additions & 2 deletions blog/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 0 additions & 2 deletions content_manager/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
4 changes: 2 additions & 2 deletions content_manager/management/commands/create_demo_pages.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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

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"]
Expand All @@ -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
Expand Down
9 changes: 5 additions & 4 deletions content_manager/management/commands/create_starter_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -199,7 +200,7 @@ def create_contact_page(self, slug: str = "contact") -> None:

thank_you_text = RichText("<p>Merci pour votre message ! Nous reviendrons vers vous rapidement.</p>")

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)
Expand Down
4 changes: 2 additions & 2 deletions content_manager/management/commands/migrate_pages.py
Original file line number Diff line number Diff line change
@@ -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/"

Expand Down Expand Up @@ -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")
Expand Down
5 changes: 3 additions & 2 deletions content_manager/management/commands/set_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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()
Expand Down
35 changes: 24 additions & 11 deletions content_manager/services/accessors.py
Original file line number Diff line number Diff line change
@@ -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()

Expand All @@ -30,20 +31,26 @@ 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:
"""
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
Expand Down Expand Up @@ -80,20 +87,26 @@ 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:
"""
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
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down
5 changes: 3 additions & 2 deletions content_manager/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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()
Expand Down
12 changes: 12 additions & 0 deletions content_manager/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.
Expand Down
Loading