Skip to content

Commit

Permalink
Moved to a new app
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash-Crow committed Jun 26, 2024
1 parent 37a7c5a commit bf8af49
Show file tree
Hide file tree
Showing 16 changed files with 197 additions and 113 deletions.
1 change: 1 addition & 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",
"forms",
"wagtail.contrib.forms",
"wagtail.contrib.redirects",
"wagtail.contrib.settings",
Expand Down
99 changes: 1 addition & 98 deletions content_manager/models.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
from django.db import models
from django.forms import widgets
from django.template.response import TemplateResponse
from django.utils.translation import gettext_lazy as _
from modelcluster.fields import ParentalKey
from modelcluster.models import ClusterableModel
from modelcluster.tags import ClusterTaggableManager
from taggit.models import Tag as TaggitTag, TaggedItemBase
from wagtail.admin.panels import FieldPanel, FieldRowPanel, InlinePanel, MultiFieldPanel, ObjectList, TabbedInterface
from wagtail.contrib.forms.models import AbstractEmailForm, AbstractFormField
from wagtail.contrib.forms.panels import FormSubmissionsPanel
from wagtail.admin.panels import FieldPanel, InlinePanel, MultiFieldPanel, ObjectList, TabbedInterface
from wagtail.contrib.settings.models import BaseSiteSetting, register_setting
from wagtail.fields import RichTextField
from wagtail.images import get_image_model_string
Expand Down Expand Up @@ -335,97 +332,3 @@ def get_categories(self):

class Meta:
verbose_name = _("Mega menu")


class FormField(AbstractFormField):
FORM_FIELD_CHOICES = (
("singleline", _("Single line text")),
("multiline", _("Multi-line text")),
("email", _("Email")),
("number", _("Number")),
("url", _("URL")),
("checkbox", _("Checkbox")),
("cmsfr_checkboxes", _("Checkboxes")),
("dropdown", _("Drop down")),
("cmsfr_radio", _("Radio buttons")),
("cmsfr_date", _("Date")),
("cmsfr_datetime", _("Date/time")),
("hidden", _("Hidden field")),
)

page = ParentalKey("FormPage", on_delete=models.CASCADE, related_name="form_fields")


class FormPage(AbstractEmailForm):
intro = RichTextField(blank=True)
thank_you_text = RichTextField(blank=True)

content_panels = AbstractEmailForm.content_panels + [
FormSubmissionsPanel(),
FieldPanel("intro", heading="Introduction"),
InlinePanel("form_fields", label="Champs de formulaire"),
FieldPanel("thank_you_text", heading="Texte de remerciement"),
MultiFieldPanel(
[
FieldRowPanel(
[
FieldPanel("from_address", classname="col6"),
FieldPanel("to_address", classname="col6"),
]
),
FieldPanel("subject"),
],
"Courriel",
help_text="Facultatif",
),
]

class Meta:
verbose_name = "Page de formulaire"
verbose_name_plural = "Pages de formulaire"

def serve(self, request, *args, **kwargs):
# These input widgets don't need the fr-input class
if request.method == "POST":
form = self.get_form(request.POST, request.FILES, page=self, user=request.user)

if form.is_valid():
form_submission = self.process_form_submission(form)
return self.render_landing_page(request, form_submission, *args, **kwargs)
else:
form = self.get_form(page=self, user=request.user)

WIDGETS_NO_FR_INPUT = [
widgets.CheckboxInput,
widgets.FileInput,
widgets.ClearableFileInput,
]

for visible in form.visible_fields():
"""
Depending on the widget, we have to add some classes:
- on the outer group
- on the form field itsef
If a class is already set, we don't force the DSFR-specific classes.
"""
if "class" not in visible.field.widget.attrs:
if type(visible.field.widget) in [
widgets.Select,
widgets.SelectMultiple,
]:
visible.field.widget.attrs["class"] = "fr-select"
visible.field.widget.group_class = "fr-select-group"
elif isinstance(visible.field.widget, widgets.DateInput):
visible.field.widget.attrs["class"] = "fr-input"
visible.field.widget.attrs["type"] = "date"
elif isinstance(visible.field.widget, widgets.RadioSelect):
visible.field.widget.attrs["dsfr"] = "dsfr"
visible.field.widget.group_class = "fr-radio-group"
elif isinstance(visible.field.widget, widgets.CheckboxSelectMultiple):
visible.field.widget.attrs["dsfr"] = "dsfr"
elif type(visible.field.widget) not in WIDGETS_NO_FR_INPUT:
visible.field.widget.attrs["class"] = "fr-input"

context = self.get_context(request)
context["form"] = form
return TemplateResponse(request, self.get_template(request), context)
Empty file added forms/__init__.py
Empty file.
1 change: 1 addition & 0 deletions forms/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Register your models here.
6 changes: 6 additions & 0 deletions forms/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class FormsConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "forms"
Binary file added forms/locale/fr/LC_MESSAGES/django.mo
Binary file not shown.
68 changes: 68 additions & 0 deletions forms/locale/fr/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-06-25 10:02+0200\n"
"PO-Revision-Date: 2024-06-25 10:09+0200\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: fr\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"
"X-Generator: Poedit 3.4.2\n"

#: forms/models.py:14
msgid "Single line text"
msgstr "Texte sur une ligne"

#: forms/models.py:15
msgid "Multi-line text"
msgstr "Texte sur plusieurs lignes"

#: forms/models.py:16
msgid "Email"
msgstr "Adresse e-mail"

#: forms/models.py:17
msgid "Number"
msgstr "Nombre"

#: forms/models.py:18
msgid "URL"
msgstr "URL"

#: forms/models.py:19
msgid "Checkbox"
msgstr "Case à cocher"

#: forms/models.py:20
msgid "Checkboxes"
msgstr "Cases à cocher"

#: forms/models.py:21
msgid "Drop down"
msgstr "Liste déroulante"

#: forms/models.py:22
msgid "Radio buttons"
msgstr "Boutons radio"

#: forms/models.py:23
msgid "Date"
msgstr "Date"

#: forms/models.py:24
msgid "Date/time"
msgstr "Date et heure"

#: forms/models.py:25
msgid "Hidden field"
msgstr "Champ caché"
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 5.0.6 on 2024-06-24 14:34
# Generated by Django 5.0.6 on 2024-06-25 07:10

import django.db.models.deletion
import modelcluster.fields
Expand All @@ -8,8 +8,9 @@


class Migration(migrations.Migration):
initial = True

dependencies = [
("content_manager", "0034_alter_contentpage_body"),
("wagtailcore", "0093_uploadedfile"),
]

Expand Down Expand Up @@ -111,9 +112,7 @@ class Migration(migrations.Migration):
(
"page",
modelcluster.fields.ParentalKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="form_fields",
to="content_manager.formpage",
on_delete=django.db.models.deletion.CASCADE, related_name="form_fields", to="forms.formpage"
),
),
],
Expand Down
Empty file added forms/migrations/__init__.py
Empty file.
103 changes: 103 additions & 0 deletions forms/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
from django.db import models
from django.forms import widgets
from django.template.response import TemplateResponse
from django.utils.translation import gettext_lazy as _
from modelcluster.fields import ParentalKey
from wagtail.admin.panels import FieldPanel, FieldRowPanel, InlinePanel, MultiFieldPanel
from wagtail.contrib.forms.models import AbstractEmailForm, AbstractFormField
from wagtail.contrib.forms.panels import FormSubmissionsPanel
from wagtail.fields import RichTextField


class FormField(AbstractFormField):
FORM_FIELD_CHOICES = (
("singleline", _("Single line text")),
("multiline", _("Multi-line text")),
("email", _("Email")),
("number", _("Number")),
("url", _("URL")),
("checkbox", _("Checkbox")),
("cmsfr_checkboxes", _("Checkboxes")),
("dropdown", _("Drop down")),
("cmsfr_radio", _("Radio buttons")),
("cmsfr_date", _("Date")),
("cmsfr_datetime", _("Date/time")),
("hidden", _("Hidden field")),
)

page = ParentalKey("FormPage", on_delete=models.CASCADE, related_name="form_fields")


class FormPage(AbstractEmailForm):
intro = RichTextField(blank=True)
thank_you_text = RichTextField(blank=True)

content_panels = AbstractEmailForm.content_panels + [
FormSubmissionsPanel(),
FieldPanel("intro", heading="Introduction"),
InlinePanel("form_fields", label="Champs de formulaire"),
FieldPanel("thank_you_text", heading="Texte de remerciement"),
MultiFieldPanel(
[
FieldRowPanel(
[
FieldPanel("from_address", classname="col6"),
FieldPanel("to_address", classname="col6"),
]
),
FieldPanel("subject"),
],
"Courriel",
help_text="Facultatif",
),
]

class Meta:
verbose_name = "Page de formulaire"
verbose_name_plural = "Pages de formulaire"

def serve(self, request, *args, **kwargs):
# These input widgets don't need the fr-input class
if request.method == "POST":
form = self.get_form(request.POST, request.FILES, page=self, user=request.user)

if form.is_valid():
form_submission = self.process_form_submission(form)
return self.render_landing_page(request, form_submission, *args, **kwargs)
else:
form = self.get_form(page=self, user=request.user)

WIDGETS_NO_FR_INPUT = [
widgets.CheckboxInput,
widgets.FileInput,
widgets.ClearableFileInput,
]

for visible in form.visible_fields():
"""
Depending on the widget, we have to add some classes:
- on the outer group
- on the form field itsef
If a class is already set, we don't force the DSFR-specific classes.
"""
if "class" not in visible.field.widget.attrs:
if type(visible.field.widget) in [
widgets.Select,
widgets.SelectMultiple,
]:
visible.field.widget.attrs["class"] = "fr-select"
visible.field.widget.group_class = "fr-select-group"
elif isinstance(visible.field.widget, widgets.DateInput):
visible.field.widget.attrs["class"] = "fr-input"
visible.field.widget.attrs["type"] = "date"
elif isinstance(visible.field.widget, widgets.RadioSelect):
visible.field.widget.attrs["dsfr"] = "dsfr"
visible.field.widget.group_class = "fr-radio-group"
elif isinstance(visible.field.widget, widgets.CheckboxSelectMultiple):
visible.field.widget.attrs["dsfr"] = "dsfr"
elif type(visible.field.widget) not in WIDGETS_NO_FR_INPUT:
visible.field.widget.attrs["class"] = "fr-input"

context = self.get_context(request)
context["form"] = form
return TemplateResponse(request, self.get_template(request), context)
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ <h1>{{ page.title }}</h1>
{% csrf_token %}
{% dsfr_form %}
<input class="fr-btn" type="submit">
<ul>
{% for field in form %}

<!--
<ul>
{% for field in form %}
<li>{{ field.label }}, {{ field.type }}, {{ field.field.widget.attrs }} {{ field.field.widget }}</li>
{% endfor %}
</ul>
{% endfor %}
</ul>
-->
</form>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions forms/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Create your tests here.
1 change: 1 addition & 0 deletions forms/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Create your views here.
Binary file modified locale/fr/LC_MESSAGES/django.mo
Binary file not shown.
10 changes: 4 additions & 6 deletions locale/fr/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-06-04 15:08+0200\n"
"PO-Revision-Date: 2024-06-04 15:16+0200\n"
"POT-Creation-Date: 2024-06-25 10:02+0200\n"
"PO-Revision-Date: 2024-06-25 10:10+0200\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: fr\n"
Expand All @@ -22,7 +22,7 @@ msgstr ""
#: templates/blocks/follow.html:9
msgctxt "Section title"
msgid "Subscribe to our newsletter"
msgstr " Abonnez-vous à notre lettre d’information "
msgstr "Abonnez-vous à notre lettre d’information"

#: templates/blocks/follow.html:13
msgctxt "Button title"
Expand All @@ -35,9 +35,7 @@ msgstr "S’abonner"

#: templates/blocks/follow.html:30
msgid "Follow us <br /> on social media"
msgstr ""
"Suivez-nous\n"
"sur les réseaux sociaux "
msgstr "Suivez-nous<br />sur les réseaux sociaux"

#: templates/blocks/footer.html:5
msgid "Back to home page"
Expand Down

0 comments on commit bf8af49

Please sign in to comment.