Skip to content

Commit

Permalink
Move from django-environ to environs
Browse files Browse the repository at this point in the history
  • Loading branch information
medihack committed Jan 13, 2025
1 parent 8b89da9 commit 668116d
Show file tree
Hide file tree
Showing 7 changed files with 251 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import sys
from typing import Any

from django.contrib.auth.models import Group
from django.core.management.base import BaseCommand, CommandParser
from environ import sys
from faker import Faker

from adit_radis_shared.accounts.factories import GroupFactory
Expand Down
1 change: 1 addition & 0 deletions docker-compose.base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ x-app: &default-app
DJANGO_CSRF_TRUSTED_ORIGINS: ${DJANGO_CSRF_TRUSTED_ORIGINS:-}
DJANGO_SECRET_KEY: ${DJANGO_SECRET_KEY:?}
DJANGO_SERVER_EMAIL: ${DJANGO_SERVER_EMAIL:?}
IS_DOCKER_CONTAINER: "true"
PROJECT_VERSION: ${PROJECT_VERSION:-vX.Y.Z}
SITE_DOMAIN: ${SITE_DOMAIN:?}
SITE_NAME: ${SITE_NAME:?}
Expand Down
32 changes: 15 additions & 17 deletions example_project/example_project/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,12 @@

from pathlib import Path

import environ

# During development and calling `manage.py` from the host most environment variables are
# set using the `.env` file (see `manage.py`). But some variables are only used in the Docker
# compose setup, so we need to provide defaults here.
env = environ.Env()
llamacpp_dev_port = env.int("LLAMACPP_DEV_PORT", default=8080) # type: ignore
postgres_dev_port = env.int("POSTGRES_DEV_PORT", default=5432) # type: ignore
env = environ.Env(
DATABASE_URL=(str, f"postgres://postgres:postgres@localhost:{postgres_dev_port}/postgres"),
DBBACKUP_STORAGE_LOCATION=(str, "/temp/backups-radis"),
LLAMACPP_URL=(str, f"http://localhost:{llamacpp_dev_port}"),
)
from environs import env

# During development and calling `manage.py` from the host we have to load the .env file manually.
# Some env variables will still need a default value, as those are only set in the compose file.
if not env.bool("IS_DOCKER_CONTAINER", default=False):
env.read_env()

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent.parent
Expand All @@ -33,7 +26,7 @@
SOURCE_FOLDERS = [BASE_DIR / ".." / "adit_radis_shared", BASE_DIR / ".." / "example_project"] # noqa: F405

# Fetch version from the environment which is passed through from the latest git version tag
PROJECT_VERSION = env.str("PROJECT_VERSION", default="vX.Y.Z") # type: ignore
PROJECT_VERSION = env.str("PROJECT_VERSION", default="vX.Y.Z")

# Needed by sites framework
SITE_ID = 1
Expand Down Expand Up @@ -120,8 +113,11 @@
# This seems to be important for Cloud IDEs as CookieStorage does not work there.
MESSAGE_STORAGE = "django.contrib.messages.storage.session.SessionStorage"

# Loads the DB setup from the DATABASE_URL environment variable.
DATABASES = {"default": env.db()}
# DATABASE_URL is only set in the compose file. For local development on the host
# we use the port from the .env file directly.
postgres_dev_port = env.int("POSTGRES_DEV_PORT", default=5432)
database_url = f"postgres://postgres:postgres@localhost:{postgres_dev_port}/postgres"
DATABASES = {"default": env.dj_db_url("DATABASE_URL", default=database_url)}

# Default primary key field type
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field
Expand Down Expand Up @@ -217,5 +213,7 @@

# django-dbbackup
DBBACKUP_STORAGE = "django.core.files.storage.FileSystemStorage"
DBBACKUP_STORAGE_OPTIONS = {"location": env.str("DBBACKUP_STORAGE_LOCATION")}
DBBACKUP_STORAGE_OPTIONS = {
"location": env.str("DBBACKUP_STORAGE_LOCATION", default="/tmp/backups-radis")
}
DBBACKUP_CLEANUP_KEEP = 30
2 changes: 1 addition & 1 deletion example_project/example_project/settings/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_TIMEOUT = 60
email_config = env.email_url("DJANGO_EMAIL_URL")
email_config = env.dj_email_url("DJANGO_EMAIL_URL")
EMAIL_HOST = email_config["EMAIL_HOST"]
EMAIL_PORT = email_config.get("EMAIL_PORT", 25)
EMAIL_HOST_USER = email_config.get("EMAIL_HOST_USER")
Expand Down
4 changes: 0 additions & 4 deletions example_project/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
import os
import sys

from dotenv import load_dotenv

load_dotenv()


def initialize_debugger():
"""Enable remote debugging."""
Expand Down
Loading

0 comments on commit 668116d

Please sign in to comment.