Skip to content

Commit

Permalink
Update content creation scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash-Crow committed Jun 27, 2024
1 parent 272e4a9 commit 8876da3
Show file tree
Hide file tree
Showing 4 changed files with 289 additions and 10 deletions.
156 changes: 155 additions & 1 deletion content_manager/management/commands/create_demo_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

from blog.models import BlogIndexPage
from content_manager.models import ContentPage, MegaMenu, MegaMenuCategory
from forms.models import FormField, FormPage

ALL_ALLOWED_SLUGS = ["blog_index", "publications"]
ALL_ALLOWED_SLUGS = ["blog_index", "publications", "menu_page", "form"]

fake = Faker("fr_FR")

Expand Down Expand Up @@ -51,6 +52,22 @@ def handle(self, *args, **kwargs):
elif slug == "publications":
self.create_publication_pages(site, home_page, main_menu)

elif slug == "menu_page":
# A blank page that is just destined to have a list of its subpages.
body = [("subpageslist", None)]
menu_page = self.create_content_page(slug, title="Pages d’exemple", body=body, parent_page=home_page)

# Inserts it right before the last entry
contact_menu_entry = MainMenuItem.objects.filter(menu=main_menu).last()
MainMenuItem.objects.update_or_create(
link_page=menu_page, menu=main_menu, defaults={"sort_order": contact_menu_entry.sort_order}
)
contact_menu_entry.sort_order += 1
contact_menu_entry.save()

elif slug == "form":
menu_page = ContentPage.objects.get(slug="menu_page")
self.create_form_page("form_with_all_fields", parent_page=menu_page)
else:
raise ValueError(f"Valeur inconnue : {slug}")

Expand Down Expand Up @@ -151,3 +168,140 @@ def create_publication_pages(self, site, home_page, main_menu):

publications_mega_menu.categories.add(menu_category)
publications_mega_menu.save()

def create_form_page(self, slug: str, parent_page: ContentPage) -> None:
"""
Creates a form page with all the different forms
"""

# Don't replace a manually created page
already_exists = ContentPage.objects.filter(slug=slug).first()
if already_exists:
self.stdout.write(f"The page seem to already exist with id {already_exists.id}")
return

# Create the form page
title = "Formulaire avec tous les champs"
intro = RichText("<p>Texte d’introduction</p>")

thank_you_text = RichText("<p>Merci pour votre message !</p>")

form_page = parent_page.add_child(
instance=FormPage(title=title, slug=slug, intro=intro, thank_you_text=thank_you_text, show_in_menus=True)
)

# Create the form fields
fields = [
{
"sort_order": 0,
"clean_name": "champ_texte",
"label": "Champ texte",
"required": True,
"choices": "",
"default_value": "",
"help_text": "",
"page": form_page,
"field_type": "singleline",
},
{
"sort_order": 1,
"clean_name": "zone_de_texte",
"label": "Zone de texte",
"required": True,
"page": form_page,
"field_type": "multiline",
},
{
"sort_order": 2,
"clean_name": "adresse_email",
"label": "Adresse email",
"required": True,
"page": form_page,
"field_type": "email",
},
{
"sort_order": 3,
"clean_name": "nombre",
"label": "Nombre",
"default_value": 42,
"required": True,
"page": form_page,
"field_type": "number",
},
{
"sort_order": 4,
"clean_name": "url",
"label": "URL",
"required": True,
"page": form_page,
"field_type": "url",
},
{
"sort_order": 5,
"clean_name": "case_a_cocher",
"label": "Case à cocher",
"required": True,
"page": form_page,
"field_type": "checkbox",
},
{
"sort_order": 6,
"clean_name": "cases_a_cocher",
"label": "Cases à cocher",
"required": True,
"choices": "1\r\n2\r\n3",
"default_value": "",
"help_text": "",
"page": form_page,
"field_type": "checkboxes",
},
{
"sort_order": 7,
"clean_name": "liste_deroulante",
"label": "Liste déroulante",
"required": True,
"choices": "4\r\n5\r\n6",
"default_value": "",
"help_text": "",
"page": form_page,
"field_type": "dropdown",
},
{
"sort_order": 8,
"clean_name": "boutons_radio",
"label": "Boutons radio",
"required": True,
"choices": "7\r\n8\r\n9",
"default_value": "",
"help_text": "",
"page": form_page,
"field_type": "radio",
},
{
"sort_order": 9,
"clean_name": "date",
"label": "Date",
"required": True,
"choices": "",
"default_value": "",
"help_text": "",
"page": form_page,
"field_type": "date",
},
{
"sort_order": 10,
"clean_name": "champ_cache",
"label": "Champ caché",
"required": True,
"choices": "",
"default_value": "valeur",
"help_text": "",
"page": form_page,
"field_type": "hidden",
},
]

for field_data in fields:
FormField.objects.create(**field_data)

self.stdout.write(self.style.SUCCESS(f"Page {slug} created with id {form_page.id}"))
105 changes: 102 additions & 3 deletions content_manager/management/commands/create_starter_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
from wagtail.images.models import Image
from wagtail.models import Page, Site
from wagtail.rich_text import RichText
from wagtailmenus.models.menuitems import FlatMenuItem
from wagtailmenus.models.menuitems import FlatMenuItem, MainMenuItem

from content_manager.models import ContentPage
from content_manager.utils import get_or_create_footer_menu
from content_manager.utils import get_or_create_footer_menu, get_or_create_main_menu
from forms.models import FormField, FormPage

ALL_ALLOWED_SLUGS = ["home", "mentions-legales", "accessibilite"]
ALL_ALLOWED_SLUGS = ["home", "mentions-legales", "accessibilite", "contact"]


class Command(BaseCommand):
Expand Down Expand Up @@ -80,6 +81,8 @@ def handle(self, *args, **kwargs):

body.append(("alert", alert_block))
self.create_page(slug=slug, title=title, body=body, footer_label="Accessibilité : non conforme")
elif slug == "contact":
self.create_contact_page(slug)
else:
raise ValueError(f"Valeur inconnue : {slug}")

Expand Down Expand Up @@ -171,3 +174,99 @@ def create_page(self, slug: str, title: str, body: list, footer_label: str = "")
FlatMenuItem.objects.create(**footer_item)

self.stdout.write(self.style.SUCCESS(f"Page {slug} created with id {new_page.id}"))

def create_contact_page(self, slug: str = "contact") -> None:
"""
Creates a contact page for the site and adds it the main menu
"""

# Don't replace a manually created page
already_exists = ContentPage.objects.filter(slug=slug).first()
if already_exists:
self.stdout.write(f"The contact page seem to already exist with id {already_exists.id}")
return

# Create the form page
title = "Contact"
intro = RichText(
"""
<p>Bonjour, n’hésitez pas à nous contacter via le formulaire ci-dessous.</p>
<p></p>
<p>Vous pouvez également nous contacter via &lt;autres méthodes&gt;.</p>
<p></p>
<p>Les champs marqués d’une astérisque (*) sont obligatoires.</p>"""
)

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()
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)
)

# Create the form fields
fields = [
{
"sort_order": 0,
"clean_name": "votre_nom_complet",
"label": "Votre nom complet",
"required": True,
"page": contact_page,
"field_type": "singleline",
},
{
"sort_order": 1,
"clean_name": "votre_adresse_electronique",
"label": "Votre adresse électronique",
"required": True,
"choices": "",
"default_value": "",
"help_text": "Format attendu : [email protected]",
"page": contact_page,
"field_type": "email",
},
{
"sort_order": 2,
"clean_name": "votre_numero_de_telephone",
"label": "Votre numéro de téléphone",
"required": False,
"page": contact_page,
"field_type": "singleline",
},
{
"sort_order": 3,
"clean_name": "titre_de_votre_message",
"label": "Titre de votre message",
"required": True,
"page": contact_page,
"field_type": "singleline",
},
{
"sort_order": 4,
"clean_name": "votre_message",
"label": "Votre message",
"required": True,
"choices": "",
"default_value": "",
"help_text": "",
"page": contact_page,
"field_type": "multiline",
},
]

for field_data in fields:
FormField.objects.create(**field_data)

# Menu item
main_menu = get_or_create_main_menu()

menu_item = {
"sort_order": MainMenuItem.objects.filter(menu=main_menu).count(),
"link_page": contact_page,
"link_text": title,
"menu": main_menu,
}
MainMenuItem.objects.create(**menu_item)

self.stdout.write(self.style.SUCCESS(f"Form page {slug} created with id {contact_page.id}"))
4 changes: 1 addition & 3 deletions content_manager/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,7 @@ def setUp(self) -> None:
self.site = Site.objects.filter(is_default_site=True).first()
self.home_page = self.site.root_page

self.main_menu = MainMenu.objects.create(site=self.site)

MainMenuItem.objects.create(link_page=self.home_page, menu=self.main_menu, link_text="Accueil", sort_order=0)
self.main_menu = MainMenu.objects.first()

body = []

Expand Down
34 changes: 31 additions & 3 deletions content_manager/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from django.core.files.images import ImageFile
from wagtail.images.models import Image
from wagtail.models import Collection, Site
from wagtailmenus.models.menus import FlatMenu
from wagtailmenus.models.menuitems import MainMenuItem
from wagtailmenus.models.menus import FlatMenu, MainMenu


def import_image(full_path: str, title: str) -> Image:
Expand Down Expand Up @@ -39,15 +40,42 @@ def get_or_create_footer_menu() -> FlatMenu:
In any case, return it.
"""

footer_menu = FlatMenu.objects.filter(handle="footer").first()
default_site = Site.objects.filter(is_default_site=True).first()
footer_menu = FlatMenu.objects.filter(handle="footer", site=default_site).first()

if not footer_menu:
default_site = Site.objects.filter(is_default_site=True).first()
footer_menu = FlatMenu.objects.create(title="Pied de page", handle="footer", site=default_site)

return footer_menu


def get_or_create_main_menu() -> MainMenu:
"""
Get the main menu or create it if it doesn't already exist
In any case, return it.
"""

default_site = Site.objects.filter(is_default_site=True).first()
main_menu = MainMenu.objects.filter(site=default_site).first()

if not main_menu:
main_menu = MainMenu.objects.create(site=default_site, max_levels=2)

# Init the main menu with the home page
home_page = default_site.root_page

menu_item = {
"sort_order": 0,
"link_page": home_page,
"link_text": "Accueil",
"menu": main_menu,
}
MainMenuItem.objects.create(**menu_item)

return main_menu


def get_streamblock_raw_text(block) -> str:
"""
Get the raw text of a streamblock.
Expand Down

0 comments on commit 8876da3

Please sign in to comment.