|
11 | 11 | """ |
12 | 12 |
|
13 | 13 | import os |
| 14 | +import sys |
14 | 15 | from pathlib import Path |
15 | 16 | from urllib.parse import urlparse |
16 | 17 |
|
|
25 | 26 | if _load_env.exists(): |
26 | 27 | load_dotenv(_load_env) |
27 | 28 |
|
| 29 | +# Sous pytest : forcer S3 local (RustFS) pour éviter InvalidAccessKeyId avec des clés .env |
| 30 | +_run_by_pytest = "pytest" in sys.argv[0] or "pytest" in str(sys.argv) |
| 31 | +if _run_by_pytest: |
| 32 | + os.environ["AWS_S3_ENDPOINT_URL"] = "http://localhost:9000" |
| 33 | + os.environ["AWS_ACCESS_KEY_ID"] = "minioadmin" |
| 34 | + os.environ["AWS_SECRET_ACCESS_KEY"] = "minioadmin" |
| 35 | + os.environ["AWS_STORAGE_BUCKET_NAME"] = "eu-fact-force-files" |
28 | 36 |
|
29 | 37 | # Quick-start development settings - unsuitable for production |
30 | 38 | # See https://docs.djangoproject.com/en/6.0/howto/deployment/checklist/ |
@@ -106,21 +114,15 @@ def _get_databases(): |
106 | 114 | "PASSWORD": parsed.password, |
107 | 115 | "HOST": parsed.hostname, |
108 | 116 | "PORT": parsed.port or "5432", |
| 117 | + "TEST": { |
| 118 | + "NAME": f"test_{parsed.path.lstrip('/')}", |
| 119 | + }, |
109 | 120 | } |
110 | 121 | } |
111 | 122 |
|
112 | 123 |
|
113 | 124 | DATABASES = _get_databases() |
114 | 125 |
|
115 | | -# Use a dedicated test database name so tests do not overwrite dev/prod data |
116 | | -if ( |
117 | | - "default" in DATABASES |
118 | | - and DATABASES["default"]["ENGINE"] == "django.db.backends.postgresql" |
119 | | -): |
120 | | - _db_name = DATABASES["default"].get("NAME", "eu_fact_force") |
121 | | - DATABASES["default"].setdefault("TEST", {})["NAME"] = f"test_{_db_name}" |
122 | | - |
123 | | - |
124 | 126 | # Password validation |
125 | 127 | # https://docs.djangoproject.com/en/6.0/ref/settings/#auth-password-validators |
126 | 128 |
|
@@ -159,17 +161,28 @@ def _get_databases(): |
159 | 161 | # Must be an absolute filesystem path (string) for collectstatic / StaticFilesStorage |
160 | 162 | STATIC_ROOT = str((BASE_DIR.parent / "staticfiles").resolve()) |
161 | 163 |
|
162 | | -# S3 / LocalStack storage (switch via AWS_S3_ENDPOINT_URL or USE_LOCAL_STACK) |
| 164 | +# S3 / MinIO / LocalStack storage (switch via AWS_S3_ENDPOINT_URL or USE_LOCAL_STACK) |
163 | 165 | # django-storages reads AWS_S3_ENDPOINT_URL from this module |
164 | | -AWS_S3_ENDPOINT_URL = os.environ.get("AWS_S3_ENDPOINT_URL") or ( |
165 | | - "http://localhost:4566" if os.environ.get("USE_LOCAL_STACK") else None |
| 166 | +# Valeurs par défaut : RustFS local (9000) ou LocalStack (4566) si USE_LOCAL_STACK=1 |
| 167 | +AWS_S3_ENDPOINT_URL = os.environ.get("AWS_S3_ENDPOINT_URL") or "http://localhost:9000" |
| 168 | +if AWS_S3_ENDPOINT_URL and ( |
| 169 | + "localhost" in AWS_S3_ENDPOINT_URL or "127.0.0.1" in AWS_S3_ENDPOINT_URL |
| 170 | +): |
| 171 | + os.environ.setdefault("AWS_ACCESS_KEY_ID", "minioadmin") |
| 172 | + os.environ.setdefault("AWS_SECRET_ACCESS_KEY", "minioadmin") |
| 173 | +# Must match eu_fact_force.ingestion.s3.save_file_to_s3 / get_default_bucket(): uploads use |
| 174 | +# boto3 with this default bucket even when AWS_STORAGE_BUCKET_NAME is unset, so default_storage |
| 175 | +# must use the same bucket or opens fall back to FileSystemStorage and FileNotFoundError. |
| 176 | +_DEFAULT_FILES_BUCKET = "eu-fact-force-files" |
| 177 | +_AWS_STORAGE_BUCKET_NAME = ( |
| 178 | + os.environ.get("AWS_STORAGE_BUCKET_NAME") or _DEFAULT_FILES_BUCKET |
166 | 179 | ) |
167 | | -if os.environ.get("AWS_STORAGE_BUCKET_NAME"): |
| 180 | +if _AWS_STORAGE_BUCKET_NAME: |
168 | 181 | STORAGES = { |
169 | 182 | "default": { |
170 | 183 | "BACKEND": "storages.backends.s3boto3.S3Boto3Storage", |
171 | 184 | "OPTIONS": { |
172 | | - "bucket_name": os.environ.get("AWS_STORAGE_BUCKET_NAME"), |
| 185 | + "bucket_name": _AWS_STORAGE_BUCKET_NAME, |
173 | 186 | "region_name": os.environ.get("AWS_S3_REGION_NAME", "eu-west-1"), |
174 | 187 | "custom_domain": False, |
175 | 188 | }, |
|
0 commit comments