From 52dea8fa2f8b94ab2fda89becc552ca0dda5c5b2 Mon Sep 17 00:00:00 2001 From: Anthony LC Date: Fri, 15 Nov 2024 11:31:09 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7(backend)=20add=20MEDIA=5FBASE=5FUR?= =?UTF-8?q?L=20setting?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The frontend need to know the base url for the media files, so we need to add a new setting to the backend, in order to expose this value to the frontend. If the setting is not defined, the frontend current domain will be used as the base url. In production this setting do not need to be defined since we have nginx capturing the media requests, but in development we need to define it to target the nginx server. --- env.d/development/common.dist | 1 + src/backend/core/api/viewsets.py | 1 + src/backend/core/tests/test_api_config.py | 11 +++++++++-- src/backend/impress/settings.py | 3 +++ 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/env.d/development/common.dist b/env.d/development/common.dist index 97b8e08a..ec20b2d5 100644 --- a/env.d/development/common.dist +++ b/env.d/development/common.dist @@ -21,6 +21,7 @@ STORAGES_STATICFILES_BACKEND=django.contrib.staticfiles.storage.StaticFilesStora AWS_S3_ENDPOINT_URL=http://minio:9000 AWS_S3_ACCESS_KEY_ID=impress AWS_S3_SECRET_ACCESS_KEY=password +MEDIA_BASE_URL=http://localhost:8083 # OIDC OIDC_OP_JWKS_ENDPOINT=http://nginx:8083/realms/impress/protocol/openid-connect/certs diff --git a/src/backend/core/api/viewsets.py b/src/backend/core/api/viewsets.py index d6bee63e..203c1942 100644 --- a/src/backend/core/api/viewsets.py +++ b/src/backend/core/api/viewsets.py @@ -902,6 +902,7 @@ class ConfigView(views.APIView): """ array_settings = [ "ENVIRONMENT", + "MEDIA_BASE_URL", "LANGUAGES", "LANGUAGE_CODE", "SENTRY_DSN", diff --git a/src/backend/core/tests/test_api_config.py b/src/backend/core/tests/test_api_config.py index ac5d32db..405ff27a 100644 --- a/src/backend/core/tests/test_api_config.py +++ b/src/backend/core/tests/test_api_config.py @@ -15,7 +15,10 @@ from core import factories pytestmark = pytest.mark.django_db -@override_settings(SENTRY_DSN="https://sentry.test/123") +@override_settings( + SENTRY_DSN="https://sentry.test/123", + MEDIA_BASE_URL="http://testserver/", +) def test_api_config_anonymous(): """Anonymous users should be allowed to get the configuration.""" client = APIClient() @@ -26,10 +29,13 @@ def test_api_config_anonymous(): "LANGUAGE_CODE": "en-us", "SENTRY_DSN": "https://sentry.test/123", "ENVIRONMENT": "test", + "MEDIA_BASE_URL": "http://testserver/", } -@override_settings(SENTRY_DSN="https://sentry.test/123") +@override_settings( + SENTRY_DSN="https://sentry.test/123", MEDIA_BASE_URL="http://testserver/" +) def test_api_config_authenticated(): """Authenticated users should be allowed to get the configuration.""" user = factories.UserFactory() @@ -44,4 +50,5 @@ def test_api_config_authenticated(): "LANGUAGE_CODE": "en-us", "SENTRY_DSN": "https://sentry.test/123", "ENVIRONMENT": "test", + "MEDIA_BASE_URL": "http://testserver/", } diff --git a/src/backend/impress/settings.py b/src/backend/impress/settings.py index e1994644..ec62e86d 100755 --- a/src/backend/impress/settings.py +++ b/src/backend/impress/settings.py @@ -104,6 +104,9 @@ class Base(Configuration): STATIC_ROOT = os.path.join(DATA_DIR, "static") MEDIA_URL = "/media/" MEDIA_ROOT = os.path.join(DATA_DIR, "media") + MEDIA_BASE_URL = values.Value( + None, environ_name="MEDIA_BASE_URL", environ_prefix=None + ) SITE_ID = 1