Skip to content

Commit

Permalink
Deprecate tag {% dsfr_form %}
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash-Crow committed Jan 6, 2025
1 parent a274f37 commit c22ce58
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 85 deletions.
6 changes: 2 additions & 4 deletions config/settings.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
"""
Django settings for django-dsfr project.
Generated by 'django-admin startproject' using Django 3.2.5.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/
https://docs.djangoproject.com/en/dev/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
https://docs.djangoproject.com/en/dev/ref/settings/
"""

from pathlib import Path
Expand Down
5 changes: 2 additions & 3 deletions doc/forms.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ def __init__(self, *args, **kwargs):
```

## Utilisation
Les formulaires sont appelés avec la balise `{{ form }}` (ou `{{ my_custom_form }}` le cas échéant).

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 }}`.
La balise `{% dsfr_form %}` est maintenant dépréciée depuis Django-DSFR 2.0.0.

## Composants

Expand Down
44 changes: 9 additions & 35 deletions dsfr/templatetags/dsfr_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -1567,41 +1567,6 @@ 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.
<div role="alert" class="fr-alert fr-alert--warning">
<h3 class="fr-alert__title">This tag is obsolete and it will be removed at the end of 2024.</h3>
<p>
Please directly include the form with <code>{{ form }}</code> (or the name of the form context variable if different).
</p>
</div>
```python
data_dict = {
"form": an optional form to render instead of the form already present in context
}
```
**Tag name**:
dsfr_form
**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


@register.inclusion_tag("dsfr/form_field_snippets/field_snippet.html")
def dsfr_form_field(field) -> dict:
"""
Expand Down Expand Up @@ -1713,3 +1678,12 @@ def dsfr_inline(field):
"""
field.field.widget.inline = True
return field


# Deprecated tags
@register.simple_tag(takes_context=True)
def dsfr_form(context: Context):
raise ValueError(
"""The dsfr_form tag is deprecated since django-dsfr 2.0.0.
Please use a normal {{ form }} tag.""",
)
41 changes: 0 additions & 41 deletions dsfr/test/test_templatetags.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import warnings

from django import forms
from django.test import SimpleTestCase
from django.template import Context, Template
from unittest.mock import MagicMock
Expand All @@ -14,7 +11,6 @@
INTEGRITY_JS_MODULE,
INTEGRITY_JS_NOMODULE,
)
from dsfr.forms import DsfrBaseForm
from dsfr.templatetags.dsfr_tags import concatenate, hyphenate


Expand Down Expand Up @@ -505,43 +501,6 @@ def test_consent_tag_rendered(self):
)


class DsfrFormTagTest(SimpleTestCase):
class TestForm(DsfrBaseForm):
test = forms.CharField(label="Ceci est un test")

class TestForm2(DsfrBaseForm):
test = forms.CharField(label="Ceci est un autre test")

context = Context({"form": TestForm(), "form2": TestForm2()})

def setUp(self) -> None:
warnings.simplefilter("ignore", category=DeprecationWarning, lineno=258)

def test_dsfr_form_renders(self):
rendered_template = Template("{% load dsfr_tags %} {% dsfr_form %}").render(
self.context
)
self.assertInHTML(
"""
<label for="id_test" class="fr-label">Ceci est un test*</label>
<input type="text" name="test" class="fr-input" required id="id_test">
""",
rendered_template,
)

def test_dsfr_form_renders_with_form_override(self):
rendered_template = Template(
"{% load dsfr_tags %} {% dsfr_form form2 %}"
).render(self.context)
self.assertInHTML(
"""
<label for="id_test" class="fr-label">Ceci est un autre test*</label>
<input type="text" name="test" class="fr-input" required id="id_test">
""",
rendered_template,
)


class DsfrContentTagTest(SimpleTestCase):
test_data = {
"alt_text": "Silhouette stylisée représentant le soleil au-dessus de deux montagnes.",
Expand Down
10 changes: 9 additions & 1 deletion example_app/dsfr_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,6 @@
},
"css": {"title": "CSS global"},
"js": {"title": "JS global"},
"form": {"title": "Formulaire"},
"form_field": {"title": "Formulaire - champ"},
"django_messages": {
"title": "Messages Django dans une alerte",
Expand Down Expand Up @@ -1186,10 +1185,19 @@
},
}

DEPRECATED_COMPONENTS = {
"form": {
"title": "Formulaire",
"since": "2.0.0",
"reason": "replaced with standard call to {{ form }}.",
},
}

all_tags_unsorted = {
**IMPLEMENTED_COMPONENTS,
**EXTRA_COMPONENTS,
**NOT_YET_IMPLEMENTED_COMPONENTS,
**WONT_BE_IMPLEMENTED,
**DEPRECATED_COMPONENTS,
}
ALL_TAGS = dict(sorted(all_tags_unsorted.items()))
1 change: 0 additions & 1 deletion example_app/templates/example_app/page_form.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{% extends "example_app/base.html" %}
{% load static dsfr_tags %}

{% block content %}
<h1>
Expand Down

0 comments on commit c22ce58

Please sign in to comment.