Skip to content

Commit

Permalink
fix(settings): Fixed how settings are determined
Browse files Browse the repository at this point in the history
  • Loading branch information
b32147 committed Nov 7, 2024
1 parent fe1abfc commit a721858
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 57 deletions.
22 changes: 4 additions & 18 deletions starter/manage.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""

import os
import sys


def main():
"""Run administrative tasks."""
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "starter.settings")
os.environ.setdefault("DJANGO_CONFIGURATION", "Production")
try:
# Install django-configurations importer
from configurations.importer import install

install(check_options=True)
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
from configurations.management import execute_from_command_line


if __name__ == "__main__":
main()
execute_from_command_line(sys.argv)
12 changes: 4 additions & 8 deletions starter/starter/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@
"""

import os

# Install django-configurations importer
from configurations.importer import install

install(check_options=True)

from django.core.asgi import get_asgi_application
import sys

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "starter.settings")
os.environ.setdefault("DJANGO_CONFIGURATION", "Production")

application = get_asgi_application()
from configurations.management import execute_from_command_line

execute_from_command_line(sys.argv)
73 changes: 49 additions & 24 deletions starter/starter/settings.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"""
Django settings for starter project.
Generated by 'django-admin startproject' using Django 3.2.5.
Generated by 'django-admin startproject' using Django 5.1.3.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/
https://docs.djangoproject.com/en/5.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
https://docs.djangoproject.com/en/5.1/ref/settings/
"""

from pathlib import Path
Expand Down Expand Up @@ -63,7 +63,7 @@ class Common(Configuration):
WSGI_APPLICATION = "starter.wsgi.application"

# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
# https://docs.djangoproject.com/en/5.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
Expand All @@ -81,7 +81,7 @@ class Common(Configuration):
]

# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/
# https://docs.djangoproject.com/en/5.1/topics/i18n/

LANGUAGE_CODE = "en-us"

Expand All @@ -94,27 +94,22 @@ class Common(Configuration):
USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
# https://docs.djangoproject.com/en/5.1/howto/static-files/

STATIC_URL = values.Value("/static", environ_name="DBMI_APP_STATIC_URL_PATH") + "/"
STATIC_ROOT = values.Value("/var/static", environ_name="DBMI_APP_STATIC_ROOT")

# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

# Set a test runner to hand test runs off to pytest
# https://docs.djangoproject.com/en/3.2/ref/settings/#test-runner

TEST_RUNNER = "tests.runner.PytestTestRunner"


class Production(Common):
"""This is the production site settings configuration."""

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = values.SecretValue(environ_prefix="", environ_required=True)
Expand All @@ -123,19 +118,49 @@ class Production(Common):
DEBUG = False

# Allowed Hosts
# https://docs.djangoproject.com/en/3.2/ref/settings/#allowed-hosts
# https://docs.djangoproject.com/en/5.1/ref/settings/#allowed-hosts
ALLOWED_HOSTS = values.ListValue(environ_prefix="", environ_required=True)

# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases
DATABASES = {
"default": {
"ENGINE": values.Value(environ_prefix="DB", environ_name="ENGINE", environ_required=True),
"NAME": values.Value(environ_prefix="DB", environ_name="NAME", environ_required=True),
"USER": values.Value(environ_prefix="DB", environ_name="USER", environ_required=True),
"PASSWORD": values.Value(environ_prefix="DB", environ_name="PASSWORD", environ_required=True),
"HOST": values.Value(environ_prefix="DB", environ_name="HOST", environ_required=True),
"PORT": values.Value(environ_prefix="DB", environ_name="PORT", environ_required=True),
}
}


class Development(Common):
"""This is the settings configuration to use for development."""

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "thisisasecretkeythatisnotusedinanyactualenvironment"

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

# Allowed Hosts
# https://docs.djangoproject.com/en/5.1/ref/settings/#allowed-hosts
ALLOWED_HOSTS = ["*"]

# Database
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases
DATABASES = {
"default": {
"ENGINE": values.Value(environ_prefix="DB", environ_required=True),
"NAME": values.Value(environ_prefix="DB", environ_required=True),
"USER": values.Value(environ_prefix="DB", environ_required=True),
"PASSWORD": values.Value(environ_prefix="DB", environ_required=True),
"HOST": values.Value(environ_prefix="DB", environ_required=True),
"PORT": values.Value(environ_prefix="DB", environ_required=True),
"ENGINE": values.Value(environ_prefix="DB", environ_name="ENGINE", environ_required=True),
"NAME": values.Value(environ_prefix="DB", environ_name="NAME", environ_required=True),
"USER": values.Value(environ_prefix="DB", environ_name="USER", environ_required=True),
"PASSWORD": values.Value(environ_prefix="DB", environ_name="PASSWORD", environ_required=True),
"HOST": values.Value(environ_prefix="DB", environ_name="HOST", environ_required=True),
"PORT": values.Value(environ_prefix="DB", environ_name="PORT", environ_required=True),
}
}

Expand All @@ -144,7 +169,7 @@ class Test(Common):
"""This is the settings configuration to use for testing."""

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "thisisasecretkeythatisnotusedinanyactualenvironment"
Expand All @@ -153,11 +178,11 @@ class Test(Common):
DEBUG = True

# Allowed Hosts
# https://docs.djangoproject.com/en/3.2/ref/settings/#allowed-hosts
# https://docs.djangoproject.com/en/5.1/ref/settings/#allowed-hosts
ALLOWED_HOSTS = ["*"]

# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
Expand Down
9 changes: 2 additions & 7 deletions starter/starter/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,9 @@

import os

# Install django-configurations importer
from configurations.importer import install

install(check_options=True)

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "starter.settings")
os.environ.setdefault("DJANGO_CONFIGURATION", "Production")

from configurations.wsgi import get_wsgi_application

application = get_wsgi_application()

0 comments on commit a721858

Please # to comment.