From c278556b39e5cac9237c501ee54902faf60ad24c Mon Sep 17 00:00:00 2001 From: Sylvain Boissel Date: Mon, 19 Aug 2024 12:39:11 +0200 Subject: [PATCH 1/9] dsfr_form: Add deprecation warning --- dsfr/forms.py | 8 +++++--- dsfr/templatetags/dsfr_tags.py | 10 +++++++++- example_app/templates/example_app/page_form.html | 2 +- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/dsfr/forms.py b/dsfr/forms.py index 379c5d51a..0ba437e08 100644 --- a/dsfr/forms.py +++ b/dsfr/forms.py @@ -16,19 +16,21 @@ def engine(self): "APP_DIRS": True, "DIRS": [ Path(__file__).resolve().parent / self.backend.app_dirname, - Path(forms.__path__[0]).resolve() / "templates", + Path(forms.__path__[0]).resolve() / "templates", # type: ignore ], "NAME": "djangoforms", "OPTIONS": {}, - } + } # type: ignore ) class DsfrBaseForm(Form): """ - A base form that adds the necessary class on relevant fields + A base form that adds the necessary classes on relevant fields """ + template_name = "dsfr/form_snippet.html" # type: ignore + def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) for visible in self.visible_fields(): diff --git a/dsfr/templatetags/dsfr_tags.py b/dsfr/templatetags/dsfr_tags.py index 52753fc2b..9e361bcbc 100644 --- a/dsfr/templatetags/dsfr_tags.py +++ b/dsfr/templatetags/dsfr_tags.py @@ -1168,7 +1168,7 @@ def dsfr_table(*args, **kwargs) -> dict: warnings.warn( f"Due to changes in the DSFR v1.12, class {dc} is deprecated in django-dsfr v1.3 or superior", DeprecationWarning, - stacklevel=2, + stacklevel=3, ) # Deprecated in DSFR 1.12 @@ -1572,6 +1572,14 @@ def dsfr_form(context: Context, form=None) -> dict: **Usage**: `{% dsfr_form %}` """ # noqa + + warnings.warn( + """The dsfr_form tag is deprecated and will be removed from django-dsfr at the end of 2024. + Please use a normal {{ form }} tag (requires Django 4 or superior)""", + DeprecationWarning, + stacklevel=2, + ) + return context.update({"form": form}) if form else context # type: ignore diff --git a/example_app/templates/example_app/page_form.html b/example_app/templates/example_app/page_form.html index f77ad7860..e48046e3b 100644 --- a/example_app/templates/example_app/page_form.html +++ b/example_app/templates/example_app/page_form.html @@ -6,7 +6,7 @@

{% csrf_token %} - {% dsfr_form %} + {{ form }}
+
+ + {% endfor %} + - {% endfor %} - - -
-{% endif %} +
+ {% endif %} {% endblock form_set %} {% block extra_js %} diff --git a/example_app/templates/example_app/formsets/book.html b/example_app/templates/example_app/formsets/book.html new file mode 100644 index 000000000..5783a3e71 --- /dev/null +++ b/example_app/templates/example_app/formsets/book.html @@ -0,0 +1,11 @@ +{# Instead of extending base.html, extend formset-base.html #} +{# Then, include this template in another template which extends form_base.html #} +{% extends "dsfr/formset_base.html" %} +{# You can add some information to the formset inside the blocks of formset-base.html #} +{% block formset_title %} + {% if formset %} +

+ Livres +

+ {% endif %} +{% endblock formset_title %} diff --git a/example_app/templates/example_app/example_form.html b/example_app/templates/example_app/page_formset_main_form.html similarity index 92% rename from example_app/templates/example_app/example_form.html rename to example_app/templates/example_app/page_formset_main_form.html index e72bac711..25bb06a16 100644 --- a/example_app/templates/example_app/example_form.html +++ b/example_app/templates/example_app/page_formset_main_form.html @@ -1,18 +1,22 @@ {% extends "dsfr/form_base.html" %} {% load static dsfr_tags %} + {% block header %} {% include "example_app/blocks/header.html" %} {% endblock header %} + {# You can add some information to the form inside the blocks of form_base.html #} {% block head_form %}

Exemple de formulaire avec formset

{% endblock head_form %} + {# If you use formset, you need to include a template which extends formset-base.html, and put it in a block of form_base.html #} {% block extra_formset_after %} - {% include "example_app/example_formset.html" %} + {% include "example_app/formsets/book.html" %} {% endblock extra_formset_after %} + {# Don't forget you dsfr button to submit the form #} {% block foot_form %} {% dsfr_button btn_submit %} diff --git a/example_app/views.py b/example_app/views.py index b20eecbd0..ce0329de9 100644 --- a/example_app/views.py +++ b/example_app/views.py @@ -295,12 +295,12 @@ class AuthorCreateView(CreateView): model = Author form_class = AuthorCreateForm formset = None - template_name = "example_app/example_form.html" + template_name = "example_app/page_formset_main_form.html" # /!\ Your template needs to extends form_base.html. If you use formset, # your template needs to include another template which extends formset_base.html def get(self, request, *args, **kwargs): - instance = None # noqa: F841 + instance = None # noqa: F841 NOSONAR try: if self.object: instance = self.object # noqa: F841 @@ -311,7 +311,7 @@ def get(self, request, *args, **kwargs): form = self.get_form(form_class) self.formset = self.get_formset(request) formset = self.formset - book_formhelper = BookCreateFormHelper() # noqa: F841 + book_formhelper = BookCreateFormHelper() # noqa: F841 NOSONAR return self.render_to_response( self.get_context_data(form=form, formset=formset) From cbfc6bd4ce00cbeaee01d7ba1a096026c1deba3f Mon Sep 17 00:00:00 2001 From: Sylvain Boissel Date: Mon, 19 Aug 2024 12:47:56 +0200 Subject: [PATCH 3/9] Missing translation --- .../migrations/0003_alter_book_author.py | 23 +++++++++++++++++++ example_app/models.py | 6 ++++- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 example_app/migrations/0003_alter_book_author.py diff --git a/example_app/migrations/0003_alter_book_author.py b/example_app/migrations/0003_alter_book_author.py new file mode 100644 index 000000000..18b13ec43 --- /dev/null +++ b/example_app/migrations/0003_alter_book_author.py @@ -0,0 +1,23 @@ +# Generated by Django 4.2.15 on 2024-08-19 10:47 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ("example_app", "0002_alter_author_options_alter_book_options_and_more"), + ] + + operations = [ + migrations.AlterField( + model_name="book", + name="author", + field=models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to="example_app.author", + verbose_name="Author", + ), + ), + ] diff --git a/example_app/models.py b/example_app/models.py index 3ea8474b5..d97c296b4 100644 --- a/example_app/models.py +++ b/example_app/models.py @@ -40,7 +40,11 @@ class Meta: class Book(models.Model): author = models.ForeignKey( - Author, on_delete=models.CASCADE, null=False, blank=False + Author, + on_delete=models.CASCADE, + null=False, + blank=False, + verbose_name=_("Author"), ) title = models.CharField(_("Title"), max_length=250, null=False, blank=False) number_of_pages = models.CharField(_("Number of pages"), max_length=6, blank=True) From 182444c4aa0d5493af035c5a2a5ba4079f4b28b1 Mon Sep 17 00:00:00 2001 From: Sylvain Boissel Date: Mon, 19 Aug 2024 12:48:57 +0200 Subject: [PATCH 4/9] Update messages --- dsfr/locale/fr/LC_MESSAGES/django.mo | Bin 6956 -> 6956 bytes dsfr/locale/fr/LC_MESSAGES/django.po | 8 ++++---- example_app/locale/fr/LC_MESSAGES/django.mo | Bin 1352 -> 1352 bytes example_app/locale/fr/LC_MESSAGES/django.po | 14 +++++++------- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/dsfr/locale/fr/LC_MESSAGES/django.mo b/dsfr/locale/fr/LC_MESSAGES/django.mo index d18a2bcb49d7b3583def68fb149d0ccd7cc011c5..3e72743e8d473af4b9bdefc9b8fded889fd156bd 100644 GIT binary patch delta 20 bcmZ2uw#ID3b^&%v1w$h%6N}CJ1oF55OZf(# delta 20 bcmZ2uw#ID3b^&%{1w#`nBlFGs1oF55ORol( diff --git a/dsfr/locale/fr/LC_MESSAGES/django.po b/dsfr/locale/fr/LC_MESSAGES/django.po index c123130d3..2e0bef32d 100644 --- a/dsfr/locale/fr/LC_MESSAGES/django.po +++ b/dsfr/locale/fr/LC_MESSAGES/django.po @@ -7,8 +7,8 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-08-13 14:26+0200\n" -"PO-Revision-Date: 2024-08-13 14:27+0200\n" +"POT-Creation-Date: 2024-08-19 12:48+0200\n" +"PO-Revision-Date: 2024-08-19 12:48+0200\n" "Last-Translator: \n" "Language-Team: \n" "Language: fr\n" @@ -310,11 +310,11 @@ msgstr "" "Sauf mention explicite de propriété intellectuelle détenue par des tiers, " "les contenus de ce site sont proposés sous" -#: dsfr/templates/dsfr/formset_base.html:27 +#: dsfr/templates/dsfr/formset_base.html:31 msgid "Delete this item" msgstr "Supprimer cet élément" -#: dsfr/templates/dsfr/formset_base.html:37 +#: dsfr/templates/dsfr/formset_base.html:39 msgid "Add an item" msgstr "Ajouter un élément" diff --git a/example_app/locale/fr/LC_MESSAGES/django.mo b/example_app/locale/fr/LC_MESSAGES/django.mo index c044218754550ee4ca055296521efb4f8d872bb3..5997befc5277108c4945e4e218e2a2a1c828f18a 100644 GIT binary patch delta 20 bcmX@Xb%JX{C^NgIf}xR>iN)p^<~NK0Lcs Date: Mon, 19 Aug 2024 12:59:26 +0200 Subject: [PATCH 5/9] Remove obsolete file --- .../templates/example_app/example_formset.html | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 example_app/templates/example_app/example_formset.html diff --git a/example_app/templates/example_app/example_formset.html b/example_app/templates/example_app/example_formset.html deleted file mode 100644 index 5783a3e71..000000000 --- a/example_app/templates/example_app/example_formset.html +++ /dev/null @@ -1,11 +0,0 @@ -{# Instead of extending base.html, extend formset-base.html #} -{# Then, include this template in another template which extends form_base.html #} -{% extends "dsfr/formset_base.html" %} -{# You can add some information to the formset inside the blocks of formset-base.html #} -{% block formset_title %} - {% if formset %} -

- Livres -

- {% endif %} -{% endblock formset_title %} From ffb9a000b1ed2aa930755c9418a59c271ceb0513 Mon Sep 17 00:00:00 2001 From: Sylvain Boissel Date: Mon, 19 Aug 2024 14:17:10 +0200 Subject: [PATCH 6/9] Improve documentation and fix example uses --- doc/forms.md | 19 +++++-------------- example_app/forms.py | 4 ++-- .../templates/example_app/page_colors.html | 4 ++-- 3 files changed, 9 insertions(+), 18 deletions(-) diff --git a/doc/forms.md b/doc/forms.md index 107891aab..da1444f56 100644 --- a/doc/forms.md +++ b/doc/forms.md @@ -53,17 +53,8 @@ def __init__(self, *args, **kwargs): self.fields["password"].widget.attrs["class"] += " my custom class" ``` -## Utilisation avec les `Formsets` - -La balise `{% dsfr_form %}` prend un paramètre optionnel `form` qui permet de surcharger le formulaire à rendre, ce qui est pratique lors de l'utilisation de `Formsets` : - -```{ .django } - - {{ formset.management_form }} - - {% for subform in formset %} - {% dsfr_form subform %} - {% endfor %} -
- -``` +## Utilisation + +La balise `{% dsfr_form %}` est maintenant dépréciée et sera retirée avant la fin de l’année 2024. + +Il faut donc remplacer les instances de `{% dsfr_form %}` par ``{{ form }}`` et `{% dsfr_form my_custom_form %}` par `{{ my_custom_form }}`. diff --git a/example_app/forms.py b/example_app/forms.py index 88a7b997d..61c68901d 100644 --- a/example_app/forms.py +++ b/example_app/forms.py @@ -137,7 +137,7 @@ def __init__(self, *args, **kwargs): class AuthorCreateForm(ModelForm, DsfrBaseForm): class Meta: model = Author - exclude = [] + exclude = [] # NOSONAR widgets = { "first_name": forms.TextInput(), "last_name": forms.TextInput(), @@ -159,7 +159,7 @@ class Meta: class BookCreateForm(ModelForm, DsfrBaseForm): class Meta: model = Book - exclude = [] + exclude = [] # NOSONAR widgets = { "title": forms.TextInput(), "number_of_pages": forms.NumberInput(), diff --git a/example_app/templates/example_app/page_colors.html b/example_app/templates/example_app/page_colors.html index dc27e2384..f150ea22b 100644 --- a/example_app/templates/example_app/page_colors.html +++ b/example_app/templates/example_app/page_colors.html @@ -55,7 +55,7 @@

Couleurs de fond

- {% dsfr_form full_color_form %} + {{ full_color_form }}

Il est possible de changer la couleur de fond d’un paragraphe ou d’une div. Les couleurs primaires, neutres et illustratives sont prises en compte. @@ -85,7 +85,7 @@

Certains composants prennent une couleur d’accentuation comme paramètre. Seules les couleurs illustratives sont prises en compte.

- {% dsfr_form accent_color_form %} + {{ accent_color_form }}

From df6e26ee84ced8811d7180e993a8c434e34f4835 Mon Sep 17 00:00:00 2001 From: Sylvain Boissel Date: Mon, 19 Aug 2024 14:59:32 +0200 Subject: [PATCH 7/9] Improve warnings --- config/settings.py | 10 ++++++++-- dsfr/templatetags/dsfr_tags.py | 19 ++++++++++++------- .../templates/example_app/doc_markdown.html | 8 ++++++++ .../templates/example_app/page_component.html | 9 +++++---- .../templates/example_app/page_form.html | 1 + 5 files changed, 34 insertions(+), 13 deletions(-) diff --git a/config/settings.py b/config/settings.py index 69ae4dd57..ccc1f175d 100644 --- a/config/settings.py +++ b/config/settings.py @@ -13,6 +13,7 @@ from pathlib import Path import os from django import VERSION as DJANGO_VERSION +import warnings # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -28,6 +29,13 @@ ALLOWED_HOSTS = [] +warnings.simplefilter("default") +if DJANGO_VERSION < (4, 0): + warnings.warn( + "Support for Django < 4.0 will be deprecated in django-dsfr at the end of 2024.", + PendingDeprecationWarning, + stacklevel=3, + ) # Application definition @@ -125,8 +133,6 @@ USE_I18N = True -USE_L10N = True - USE_TZ = True LANGUAGES = [ diff --git a/dsfr/templatetags/dsfr_tags.py b/dsfr/templatetags/dsfr_tags.py index 9e361bcbc..81d3c1f77 100644 --- a/dsfr/templatetags/dsfr_tags.py +++ b/dsfr/templatetags/dsfr_tags.py @@ -26,17 +26,15 @@ register = template.Library() -warnings.simplefilter("default") - # Check for obsolete parameters inside tags? # Deprecated tags will always issue a warning if ( hasattr(settings, "DSFR_CHECK_DEPRECATED_PARAMS") and settings.DSFR_CHECK_DEPRECATED_PARAMS ): - CHECK_DEPRECATED = True + CHECK_DEPRECATED_PARAMS = True else: - CHECK_DEPRECATED = False + CHECK_DEPRECATED_PARAMS = False """ Tags used in the "DSFR" templates. @@ -1154,7 +1152,7 @@ def dsfr_table(*args, **kwargs) -> dict: ] tag_data = parse_tag_args(args, kwargs, allowed_keys) - if "extra_classes" in tag_data and CHECK_DEPRECATED: + if "extra_classes" in tag_data and CHECK_DEPRECATED_PARAMS: extra_classes = tag_data["extra_classes"] # Deprecated in DSFR 1.12 deprecated_layout_classes = [ @@ -1558,11 +1556,18 @@ def _render_alert_tag(message): @register.inclusion_tag("dsfr/form_snippet.html", takes_context=True) def dsfr_form(context: Context, form=None) -> dict: """ - Returns the HTML for a form snippet + Returns the HTML for a form snippet. + + ```python data_dict = { - "form": an optionnal form to render instead of the form already present in context + "form": an optional form to render instead of the form already present in context } ``` diff --git a/example_app/templates/example_app/doc_markdown.html b/example_app/templates/example_app/doc_markdown.html index 16b754338..d2c423209 100644 --- a/example_app/templates/example_app/doc_markdown.html +++ b/example_app/templates/example_app/doc_markdown.html @@ -1,5 +1,6 @@ {% extends "example_app/base.html" %} {% load static dsfr_tags %} + {% block content %}

{{ title }} @@ -13,6 +14,13 @@

{{ documentation|safe }} {% endblock content %} + {% block extra_css %} + {% endblock extra_css %} diff --git a/example_app/templates/example_app/page_component.html b/example_app/templates/example_app/page_component.html index 0e1df8b12..b584cb8fb 100644 --- a/example_app/templates/example_app/page_component.html +++ b/example_app/templates/example_app/page_component.html @@ -13,10 +13,11 @@ font-family: monospace; font-size: 1rem; } -p code, li code { - background: var(--background-alt-grey); - padding: 0.2em; -} + + p code, li code { + background: var(--background-alt-grey); + padding: 0.2em; + } {% endblock extra_css %} diff --git a/example_app/templates/example_app/page_form.html b/example_app/templates/example_app/page_form.html index e48046e3b..b374e6c75 100644 --- a/example_app/templates/example_app/page_form.html +++ b/example_app/templates/example_app/page_form.html @@ -1,5 +1,6 @@ {% extends "example_app/base.html" %} {% load static dsfr_tags %} + {% block content %}

Exemple de formulaire From bfdbf4762b74ff5a7a7dd45c3d1ecc96eaf2b697 Mon Sep 17 00:00:00 2001 From: Sylvain Boissel Date: Mon, 19 Aug 2024 15:00:52 +0200 Subject: [PATCH 8/9] Consistency --- doc/forms.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/forms.md b/doc/forms.md index da1444f56..b850a55a1 100644 --- a/doc/forms.md +++ b/doc/forms.md @@ -55,6 +55,6 @@ def __init__(self, *args, **kwargs): ## Utilisation -La balise `{% dsfr_form %}` est maintenant dépréciée et sera retirée avant la fin de l’année 2024. +La balise `{% dsfr_form %}` est maintenant dépréciée et sera retirée à la fin de l’année 2024. Il faut donc remplacer les instances de `{% dsfr_form %}` par ``{{ form }}`` et `{% dsfr_form my_custom_form %}` par `{{ my_custom_form }}`. From 26b81bbd2187b9cd502e2e166b4264d87c037851 Mon Sep 17 00:00:00 2001 From: Sylvain Boissel Date: Mon, 19 Aug 2024 15:03:17 +0200 Subject: [PATCH 9/9] Add deprecation warning in the README file --- README.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.rst b/README.rst index f732abf49..ef1f948d3 100644 --- a/README.rst +++ b/README.rst @@ -36,6 +36,8 @@ Note: Only supported versions of Django and Python are supported by this project - `Python `_ - `Django `_ +Warning: Support for Django < 4.0 and Python < 3.10 will be removed at the end of 2024. + Quick start -----------