Skip to content
Open
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
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ HOST_URL=localhost
ALLOWED_HOSTS=localhost,127.0.0.1
HOST_PORT=8000
CONTAINER_PORT=$HOST_PORT
SITE_BASE_PATH=
# Used to set Django's FORCE_SCRIPT_NAME when serving the site under a sub-path
USE_WHITENOISE=0
SITE_NAME="Sites faciles"
MEDIA_ROOT=medias

Expand Down
44 changes: 29 additions & 15 deletions config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,21 @@
HOST_PROTO = os.getenv("HOST_PROTO", "https")
HOST_URL = os.getenv("HOST_URL", "localhost")
HOST_PORT = os.getenv("HOST_PORT", "")
FORCE_SCRIPT_NAME = os.getenv("FORCE_SCRIPT_NAME")
if not FORCE_SCRIPT_NAME or FORCE_SCRIPT_NAME == "None":
FORCE_SCRIPT_NAME = ""
# Prefix of the application when served under a sub-path.
# ``FORCE_SCRIPT_NAME`` is the Django setting handling this behaviour:
# https://docs.djangoproject.com/en/5.2/ref/settings/#force-script-name
# We expose a clearer ``SITE_BASE_PATH`` environment variable and assign its
# value to ``FORCE_SCRIPT_NAME``.
SITE_BASE_PATH = os.getenv("SITE_BASE_PATH", "")
if SITE_BASE_PATH in ("", "None"):
SITE_BASE_PATH = ""
else:
# Remove trailing slash to keep paths consistent
FORCE_SCRIPT_NAME = FORCE_SCRIPT_NAME.rstrip("/")
SITE_BASE_PATH = SITE_BASE_PATH.rstrip("/")

FORCE_SCRIPT_NAME = SITE_BASE_PATH

# Allow enabling WhiteNoise via an environment variable (disabled by default)
USE_WHITENOISE = os.getenv("USE_WHITENOISE", "0") != "0"

INTERNAL_IPS = [
"127.0.0.1",
Expand All @@ -57,7 +66,6 @@

INSTALLED_APPS = [
# The order is important for overriding templates and using contexts, please change it carefully.
"whitenoise.runserver_nostatic",
"storages",
"wagtail.contrib.forms",
"wagtail.contrib.redirects",
Expand Down Expand Up @@ -96,6 +104,9 @@
"wagtail.admin",
]

if USE_WHITENOISE:
INSTALLED_APPS.insert(0, "whitenoise.runserver_nostatic")

# Only add these on a dev machine, outside of tests
if not TESTING and DEBUG and "localhost" in HOST_URL:
INSTALLED_APPS += [
Expand All @@ -114,17 +125,20 @@
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"wagtail.contrib.redirects.middleware.RedirectMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
]

if DEBUG:
STATICFILES_STORAGE = "whitenoise.storage.CompressedStaticFilesStorage"
# Allow WhiteNoise to load files directly from app directories without
# running ``collectstatic`` each time and reload them on changes.
WHITENOISE_USE_FINDERS = True
WHITENOISE_AUTOREFRESH = True
else:
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
if USE_WHITENOISE:
MIDDLEWARE.append("whitenoise.middleware.WhiteNoiseMiddleware")

if USE_WHITENOISE:
if DEBUG:
STATICFILES_STORAGE = "whitenoise.storage.CompressedStaticFilesStorage"
# Allow WhiteNoise to load files directly from app directories without
# running ``collectstatic`` each time and reload them on changes.
WHITENOISE_USE_FINDERS = True
WHITENOISE_AUTOREFRESH = True
else:
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
# Only add this on a dev machine, outside of tests
if not TESTING and DEBUG and "localhost" in HOST_URL:
MIDDLEWARE += [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from django.conf import settings
from django.core.management import call_command
from django.core.management.base import BaseCommand
from django.urls import reverse
Expand Down
5 changes: 2 additions & 3 deletions content_manager/templatetags/wagtail_dsfr_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def mega_menu(context: Context, parent_menu_id: int) -> dict:
def settings_value(name):
return getattr(settings, name, "")


@register.simple_tag
def root_url() -> str:
"""Return the site's base path, taking ``FORCE_SCRIPT_NAME`` into account."""
Expand All @@ -34,8 +35,6 @@ def root_url() -> str:
return "/"




@register.simple_tag(takes_context=True)
def canonical_url(context):
"""
Expand All @@ -58,7 +57,7 @@ def canonical_url(context):
if site.port != 80:
hostname = f"{hostname}:{site.port}"
else:
hostname = request.get_host
hostname = request.get_host()

return f"{scheme}://{hostname}{request.path}"

Expand Down
4 changes: 2 additions & 2 deletions dashboard/wagtail_hooks.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from django.conf import settings
from django.templatetags.static import static
from django.urls import reverse
from django.utils.html import escape, format_html
from django.utils.translation import gettext_lazy as _
from django.conf import settings
from wagtail import hooks
from wagtail.admin.menu import MenuItem
from wagtail.rich_text import LinkHandler
Expand All @@ -29,7 +29,7 @@ def insert_custom_editor_scripts():

@hooks.register("register_admin_menu_item")
def register_site_menu_item():
index_url = f"{settings.FORCE_SCRIPT_NAME or ''}/"
index_url = f"{settings.FORCE_SCRIPT_NAME or ''}/"
return MenuItem(
_("Visit site"),
index_url,
Expand Down