[deployment] select .env according to env var

This commit is contained in:
bronsen 2025-03-14 14:15:39 +01:00
parent 3fdc60d9e5
commit ecd80c291a
5 changed files with 17 additions and 15 deletions

View file

@ -15,7 +15,7 @@ well with fish shell.
Have Python >= 3.12 installed. I use [pyenv] (which in turn is installed by [homebrew]).
Set up Postgresql, a role and a schema. I have it on localhost, using the
project name for role and schema name, and password. See `.env.sample` for
project name for role and schema name, and password. See `.env.test` for
providing connection information.
Clone the repo. `cd` into the project directory. ([direnv] should activate the

View file

@ -10,7 +10,6 @@ https://docs.djangoproject.com/en/5.1/ref/settings/
import environ
from pathlib import Path
import sys
BASE_DIR = Path(__file__).resolve().parent.parent
@ -18,10 +17,7 @@ env = environ.Env(
DEBUG=(bool, False),
DEPLOYMENT=(str, "Dev"),
)
if "pytest/__main__.py" in sys.argv[0]:
env.read_env(BASE_DIR / ".env.test")
else:
env.read_env(BASE_DIR / ".env")
env.read_env(env.str("ENV_PATH", BASE_DIR / ".env")) # pyright: ignore[reportArgumentType]
# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/
@ -44,12 +40,10 @@ INSTALLED_APPS = [
"collector.apps.CollectorConfig",
]
if env("DEPLOYMENT") == "Dev":
INSTALLED_APPS.extend(
[
"django_extensions",
"debug_toolbar",
]
)
INSTALLED_APPS += [
"django_extensions",
"debug_toolbar",
]
SHELL_PLUS = "ipython"
MIDDLEWARE = [
@ -62,7 +56,9 @@ MIDDLEWARE = [
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]
if env("DEPLOYMENT") == "Dev":
MIDDLEWARE.append("debug_toolbar.middleware.DebugToolbarMiddleware")
MIDDLEWARE += [
"debug_toolbar.middleware.DebugToolbarMiddleware",
]
INTERNAL_IPS = [
"127.0.0.1",
]

5
config/test.py Normal file
View file

@ -0,0 +1,5 @@
import os
def test_we_are_using_the_correct_envpath():
assert os.environ.get("ENV_PATH", "") == ".env.test"

View file

@ -16,4 +16,4 @@ serve:
python -m django runserver
test *ARGS:
python -m pytest {{ARGS}}
ENV_PATH=.env.test python -m pytest {{ARGS}}

View file

@ -15,7 +15,8 @@ authors = [
pythonpath = "."
python_files = [
"test_*.py",
"tests.py"
"tests.py",
"config/test.py",
]
addopts = [
"--create-db",