Skip to content

Commit 03d599b

Browse files
authored
Ajout d’un template tag "canonical_url" (numerique-gouv#309)
* New template tag * Fix URL
1 parent 36ccfd9 commit 03d599b

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

config/settings.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
ALLOWED_HOSTS = os.getenv("ALLOWED_HOSTS", "127.0.0.1,.localhost").replace(" ", "").split(",")
3939

40+
HOST_PROTO = os.getenv("HOST_PROTO", "https")
4041
HOST_URL = os.getenv("HOST_URL", "localhost")
4142

4243
INTERNAL_IPS = [
@@ -266,7 +267,7 @@ def show_toolbar(request):
266267

267268
# Base URL to use when referring to full URLs within the Wagtail admin backend -
268269
# e.g. in notification emails. Don't include '/admin' or a trailing slash
269-
WAGTAILADMIN_BASE_URL = f"{os.getenv('HOST_PROTO', 'https')}://{HOST_URL}"
270+
WAGTAILADMIN_BASE_URL = f"{HOST_PROTO}://{HOST_URL}"
270271

271272
HOST_PORT = os.getenv("HOST_PORT", "")
272273
if HOST_PORT != "":

content_manager/templatetags/wagtail_dsfr_tags.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from django.conf import settings
44
from django.template.context import Context
55
from django.utils.html import mark_safe
6+
from wagtail.models import Site
67
from wagtail.rich_text import RichText
78

89
from content_manager.models import MegaMenu
@@ -25,6 +26,29 @@ def settings_value(name):
2526
return getattr(settings, name, "")
2627

2728

29+
@register.simple_tag(takes_context=True)
30+
def canonical_url(context):
31+
"""
32+
Make the best effort to get a canonical URL for the current page, considering that:
33+
- Multiple schemes can be allowed (http vs https), but only one should be canonical
34+
- Sites can answer to multiple URLs, but only one should be canonical
35+
- Multiple sites can exist on the same instance
36+
- Some pages are not instances of Wagtail Pages (eg. search results, 404, etc.)
37+
"""
38+
request = context["request"]
39+
scheme = settings.HOST_PROTO
40+
site = Site.find_for_request(request)
41+
42+
if site:
43+
hostname = site.hostname
44+
if site.port != 80:
45+
hostname = f"{hostname}:{site.port}"
46+
else:
47+
hostname = request.get_host
48+
49+
return f"{scheme}://{hostname}{request.path}"
50+
51+
2852
@register.filter
2953
def richtext_p_add_class(value, class_name: str):
3054
"""

templates/base.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% load i18n static dsfr_tags wagtailuserbar wagtailsettings_tags sass_tags %}
1+
{% load i18n static dsfr_tags wagtailuserbar wagtailsettings_tags sass_tags wagtail_dsfr_tags %}
22

33
{% get_settings %}
44
{% with SiteConfig=settings.content_manager.CmsDsfrConfig %}
@@ -19,6 +19,10 @@
1919
{% block description %}
2020
{% endblock description %}
2121

22+
{% block canonical_url %}
23+
<link rel="canonical" href="{% canonical_url %}" />
24+
{% endblock canonical_url %}
25+
2226
{% dsfr_favicon %}
2327

2428
{% dsfr_css %}

0 commit comments

Comments
 (0)