🔧(backend) add MEDIA_BASE_URL setting

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.
This commit is contained in:
Anthony LC
2024-11-15 11:31:09 +01:00
committed by Anthony LC
parent 0a37a8ea6d
commit 52dea8fa2f
4 changed files with 14 additions and 2 deletions

View File

@@ -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

View File

@@ -902,6 +902,7 @@ class ConfigView(views.APIView):
"""
array_settings = [
"ENVIRONMENT",
"MEDIA_BASE_URL",
"LANGUAGES",
"LANGUAGE_CODE",
"SENTRY_DSN",

View File

@@ -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/",
}

View File

@@ -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