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'un générateur de formulaire #169

Merged
merged 9 commits into from
Jul 1, 2024
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: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,4 @@ runserver:

.PHONY: test
test:
$(EXEC_CMD) poetry run python manage.py test
$(EXEC_CMD) poetry run python manage.py test --buffer
2 changes: 2 additions & 0 deletions config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
INSTALLED_APPS = [
"storages",
"dashboard",
"wagtail.contrib.forms",
"wagtail.contrib.redirects",
"wagtail.contrib.settings",
"wagtail.embeds",
Expand All @@ -76,6 +77,7 @@
"sass_processor",
"content_manager",
"blog",
"forms",
]

# Only add these on a dev machine, outside of tests
Expand Down
Binary file modified content_manager/locale/fr/LC_MESSAGES/django.mo
Binary file not shown.
5 changes: 3 additions & 2 deletions content_manager/locale/fr/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-06-24 12:44+0200\n"
"PO-Revision-Date: 2024-06-24 12:45+0200\n"
"POT-Creation-Date: 2024-06-26 16:26+0200\n"
"PO-Revision-Date: 2024-06-26 17:57+0200\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: fr\n"
Expand Down Expand Up @@ -946,6 +946,7 @@ msgstr "Méga menu"
#: content_manager/templates/content_manager/blocks/card_horizontal.html:11
#: content_manager/templates/content_manager/blocks/card_vertical.html:11
#: content_manager/templates/content_manager/blocks/link.html:9
#: content_manager/templates/content_manager/blocks/tile.html:12
msgid "Opens a new window"
msgstr "Ouvre une nouvelle fenêtre"

Expand Down
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
Loading
Loading