Skip to content

Commit

Permalink
Merge pull request #2271 from opensafely-core/Jongmassey/remove-environs
Browse files Browse the repository at this point in the history
Remove `environs`
  • Loading branch information
Jongmassey authored Jan 2, 2025
2 parents 853d92a + 3a4e3df commit 89f96a2
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 45 deletions.
3 changes: 3 additions & 0 deletions docker/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ services:
# env vars supplied by just
- BUILD_DATE
- GITREF
# paths relative to docker-compose.yaml file
env_file:
- ../.env
# use dockers builitin PID daemon
init: true
ports:
Expand Down
4 changes: 2 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ docker-build env="dev": _env


# run js checks in docker container
docker-check-js:
docker-check-js: _env
{{ just_executable() }} docker/check-js


Expand All @@ -259,7 +259,7 @@ docker-test-py *args="": _env


# run js tests in docker container
docker-test-js:
docker-test-js: _env
{{ just_executable() }} docker/test-js


Expand Down
64 changes: 44 additions & 20 deletions opencodelists/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import sys
from pathlib import Path

import dj_database_url
import sentry_sdk
from django.contrib.messages import constants as messages
from environs import Env
from furl import furl
from sentry_sdk.integrations.django import DjangoIntegration
from sentry_sdk.integrations.logging import ignore_logger
Expand All @@ -30,29 +30,50 @@
sys.modules["sqlite3"] = sys.modules.pop("sqlean")


# Read env file/vars
env = Env()
env.read_env()
_missing_env_var_hint = """\
If you are running commands locally outside of `just` then you should
make sure that your `.env` file is being loaded into the environment,
which you can do in Bash using:
set -a; source .env; set +a
If you are seeing this error when running via `just` (which should
automatically load variables from `.env`) then you should check that
`.env` contains all the variables listed in `dotenv-sample` (which may
have been updated since `.env` was first created).
If you are seeing this error in production then you haven't configured
things properly.
"""


def get_env_var(name):
try:
return os.environ[name]
except KeyError:
raise RuntimeError(
f"Missing environment variable: {name}\n\n{_missing_env_var_hint}"
)


# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env.str("SECRET_KEY")
SECRET_KEY = get_env_var("SECRET_KEY")

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = env.bool("DEBUG", False)
DEBUG = os.environ.get("DEBUG", default=False) == "True"

DEBUG_TOOLBAR = env.bool("DJANGO_DEBUG_TOOLBAR", default=False)
DEBUG_TOOLBAR = os.environ.get("DJANGO_DEBUG_TOOLBAR", default=False) == "True"

BASE_URLS = env.list("BASE_URLS", [])
BASE_URLS = os.environ.get("BASE_URLS", default="").split(",")
# note localhost is required on production for dokku checks
BASE_URLS += ["http://localhost:7000"]

ALLOWED_HOSTS = [furl(base_url).host for base_url in BASE_URLS]
ALLOWED_HOSTS = [furl(base_url).host for base_url in BASE_URLS if base_url]

IN_PRODUCTION = env.bool("IN_PRODUCTION", False)
IN_PRODUCTION = os.environ.get("IN_PRODUCTION", default=False) == "True"

if IN_PRODUCTION:
# This setting causes infinite redirects
Expand Down Expand Up @@ -153,13 +174,14 @@
# see coding_systems.versioning.models.update_coding_system_database_connections (called
# from coding_systems.versioning.apps)
DATABASES = {
"default": env.dj_db_url("DATABASE_URL", "sqlite:///db.sqlite3"),
"default": dj_database_url.parse(
os.environ.get("DATABASE_URL", default="sqlite:///db.sqlite3")
),
"OPTIONS": {"timeout": 45},
}

DATABASE_DIR = Path(
env("DATABASE_DIR", BASE_DIR)
) # location of sqlite files e.g. /storage/
DATABASE_DIR = Path(os.environ.get("DATABASE_DIR", default=BASE_DIR))
# location of sqlite files e.g. /storage/
DATABASE_DUMP_DIR = DATABASE_DIR / "sql_dump"
CODING_SYSTEMS_DATABASE_DIR = DATABASE_DIR / "coding_systems"
DATABASE_ROUTERS = ["opencodelists.db_utils.CodingSystemReleaseRouter"]
Expand Down Expand Up @@ -215,15 +237,17 @@

# https://docs.djangoproject.com/en/5.0/howto/static-files/
# Note: these *must* be strings. If they are paths, we cannot cleanly extract them in ./scripts/collect-me-maybe.sh
BUILT_ASSETS = env.path("BUILT_ASSETS", default=BASE_DIR / "assets" / "dist")
BUILT_ASSETS = Path(
os.environ.get("BUILT_ASSETS", default=BASE_DIR / "assets" / "dist")
)
STATICFILES_DIRS = [
str(BASE_DIR / "static"),
str(BUILT_ASSETS),
]
STATIC_ROOT = env.path("STATIC_ROOT", default=BASE_DIR / "staticfiles")
STATIC_ROOT = Path(os.environ.get("STATIC_ROOT", default=BASE_DIR / "staticfiles"))
STATIC_URL = "/static/"

ASSETS_DEV_MODE = env.bool("ASSETS_DEV_MODE", default=False)
ASSETS_DEV_MODE = os.environ.get("ASSETS_DEV_MODE", default=False) == "True"

DJANGO_VITE = {
"default": {
Expand Down Expand Up @@ -324,15 +348,15 @@ def immutable_file_test(path, url):
# EMAIL
# Anymail
ANYMAIL = {
"MAILGUN_API_KEY": env.str("MAILGUN_API_KEY", default=None),
"MAILGUN_API_KEY": os.environ.get("MAILGUN_API_KEY", default=None),
"MAILGUN_API_URL": "https://api.eu.mailgun.net/v3",
"MAILGUN_SENDER_DOMAIN": "mg.opencodelists.org",
}
EMAIL_BACKEND = env.str(
EMAIL_BACKEND = os.environ.get(
"EMAIL_BACKEND", default="django.core.mail.backends.console.EmailBackend"
)
DEFAULT_FROM_EMAIL = "[email protected]"
SERVER_EMAIL = "[email protected]"

# API key for dm+d imports
TRUD_API_KEY = env.str("TRUD_API_KEY")
TRUD_API_KEY = get_env_var("TRUD_API_KEY")
2 changes: 1 addition & 1 deletion requirements.prod.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ antlr4-python3-runtime
beautifulsoup4
bleach
crispy-bootstrap4
dj-database-url
django~=4.2
djangorestframework
django-anymail[mailgun]
Expand All @@ -16,7 +17,6 @@ django-extensions
django-structlog
django-taggit
django-vite
environs[django]
furl
gunicorn
lxml
Expand Down
23 changes: 1 addition & 22 deletions requirements.prod.txt
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,7 @@ deprecated==1.2.15 \
dj-database-url==2.3.0 \
--hash=sha256:ae52e8e634186b57e5a45e445da5dc407a819c2ceed8a53d1fac004cc5288787 \
--hash=sha256:bb0d414ba0ac5cd62773ec7f86f8cc378a9dbb00a80884c2fc08cc570452521e
# via environs
dj-email-url==1.0.6 \
--hash=sha256:55ffe3329e48f54f8a75aa36ece08f365e09d61f8a209773ef09a1d4760e699a \
--hash=sha256:cbd08327fbb08b104eac160fb4703f375532e4c0243eb230f5b960daee7a96db
# via environs
# via -r requirements.prod.in
django==4.2.17 \
--hash=sha256:3a93350214ba25f178d4045c0786c61573e7dbfa3c509b3551374f1e11ba8de0 \
--hash=sha256:6b56d834cc94c8b21a8f4e775064896be3b4a4ca387f2612d4406a5927cd2fdc
Expand All @@ -302,10 +298,6 @@ django-anymail[mailgun]==12.0 \
--hash=sha256:65789c1b0f42915aa0450a4f173f77572d4c552979b748ddd8125af41972ad30 \
--hash=sha256:de8458d713d0f9776da9ed04dcd3a0161be23e9ecbcb49dcf149219700ecd274
# via -r requirements.prod.in
django-cache-url==3.4.5 \
--hash=sha256:5f350759978483ab85dc0e3e17b3d53eed3394a28148f6bf0f53d11d0feb5b3c \
--hash=sha256:eb9fb194717524348c95cad9905b70b647452741c1d9e481fac6d2125f0ad917
# via environs
django-cors-headers==4.6.0 \
--hash=sha256:14d76b4b4c8d39375baeddd89e4f08899051eeaf177cb02a29bd6eae8cf63aa8 \
--hash=sha256:8edbc0497e611c24d5150e0055d3b178c6534b8ed826fb6f53b21c63f5d48ba3
Expand Down Expand Up @@ -340,10 +332,6 @@ djangorestframework==3.15.2 \
--hash=sha256:2b8871b062ba1aefc2de01f773875441a961fefbf79f5eed1e32b2f096944b20 \
--hash=sha256:36fe88cd2d6c6bec23dca9804bab2ba5517a8bb9d8f47ebc68981b56840107ad
# via -r requirements.prod.in
environs[django]==11.2.1 \
--hash=sha256:9d2080cf25807a26fc0d4301e2d7b62c64fbf547540f21e3a30cc02bc5fbe948 \
--hash=sha256:e068ae3174cef52ba4b95ead22e639056a02465f616e62323e04ae08e86a75a4
# via -r requirements.prod.in
et-xmlfile==2.0.0 \
--hash=sha256:7a91720bc756843502c3b7504c77b8fe44217c85c537d85037f0f536151b2caa \
--hash=sha256:dab3f4764309081ce75662649be815c4c9081e88f0837825f90fd28317d4da54
Expand Down Expand Up @@ -571,10 +559,6 @@ markdown2==2.5.2 \
--hash=sha256:3ac02226a901c4b2f6fc21dbd17c26d118d2c25bcbb28cee093a1f8b5c46f3f1 \
--hash=sha256:bed80d301a33845be633acde47a67cf265c57ddf9cbe3cb11c49c18016c2f581
# via -r requirements.prod.in
marshmallow==3.23.2 \
--hash=sha256:bcaf2d6fd74fb1459f8450e85d994997ad3e70036452cbfa4ab685acb19479b3 \
--hash=sha256:c448ac6455ca4d794773f00bae22c2f351d62d739929f761dce5eacb5c468d7f
# via environs
openpyxl==3.1.5 \
--hash=sha256:5282c12b107bffeef825f4617dc029afaf41d0ea60823bbb665ef3079dc79de2 \
--hash=sha256:cf0e3cf56142039133628b5acffe8ef0c12bc902d2aadd3e0fe5878dc08d1050
Expand Down Expand Up @@ -659,7 +643,6 @@ packaging==24.2 \
--hash=sha256:c228a6dc5e932d346bc5739379109d49e8853dd8223571c7c5b55260edc0b97f
# via
# gunicorn
# marshmallow
# opentelemetry-instrumentation
protobuf==5.29.2 \
--hash=sha256:13d6d617a2a9e0e82a88113d7191a1baa1e42c2cc6f5f1398d3b054c8e7e714a \
Expand All @@ -676,10 +659,6 @@ protobuf==5.29.2 \
# via
# googleapis-common-protos
# opentelemetry-proto
python-dotenv==1.0.1 \
--hash=sha256:e324ee90a023d808f1959c46bcbc04446a10ced277783dc6ee09987c37ec10ca \
--hash=sha256:f7b63ef50f1b690dddf550d03497b66d609393b40b564ed0d674909a68ebf16a
# via environs
python-ipware==3.0.0 \
--hash=sha256:9117b1c4dddcb5d5ca49e6a9617de2fc66aec2ef35394563ac4eecabdf58c062 \
--hash=sha256:fc936e6e7ec9fcc107f9315df40658f468ac72f739482a707181742882e36b60
Expand Down

0 comments on commit 89f96a2

Please sign in to comment.