Skip to content

Commit

Permalink
feat(django): Add env basic config settings #138 (#139)
Browse files Browse the repository at this point in the history
Basic env config with django-environ and 12 Factor principles.

closes #138
  • Loading branch information
imAsparky authored Oct 14, 2021
1 parent fa36521 commit 8127aef
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 12 deletions.
14 changes: 12 additions & 2 deletions tests/test_bake_django.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def test_baked_django_with_allauth_settings_ok(cookies):
assert (
' "django.template.context_processors.request",' in settings_file
)
assert (
"ACCOUNT_UNIQUE_EMAIL = True # Default dj-allauth"
in settings_file
)


def test_baked_django_without_allauth_settings_ok(cookies):
Expand All @@ -66,6 +70,10 @@ def test_baked_django_without_allauth_settings_ok(cookies):
assert (
' "django.template.context_processors.request",' in settings_file
)
assert (
"ACCOUNT_UNIQUE_EMAIL = True # Default dj-allauth"
not in settings_file
)


def test_baked_django_with_allauth_url_ok(cookies):
Expand Down Expand Up @@ -779,7 +787,7 @@ def test_baked_django_without_semantic_release(cookies):
assert " :alt: Python Sementic Release" not in readme_file


def test_baked_django_settings_base_file_ok(cookies):
def test_baked_django_base_settings_base_file_ok(cookies):
"""Test Django settings.py file has generated correctly."""
default_django = cookies.bake()

Expand All @@ -788,7 +796,6 @@ def test_baked_django_settings_base_file_ok(cookies):
settings_file = settings_path.read_text().splitlines()

assert '"""Django base settings for django-boilerplate project.' in settings_file
assert 'ALLOWED_HOSTS = ["www.example.com"]' in settings_file
assert 'INTERNAL_IPS = ["127.0.0.1"]' in settings_file
assert 'ROOT_URLCONF = "django_boilerplate.urls"' in settings_file
assert 'WSGI_APPLICATION = "django_boilerplate.wsgi.application"' in settings_file
Expand Down Expand Up @@ -822,6 +829,7 @@ def test_baked_django_settings_production_file_ok(cookies):
'"""Django production settings for django-boilerplate project."""'
in settings_file
)
assert 'ALLOWED_HOSTS = ["www.example.com"]' in settings_file


def test_baked_django_settings_staging_file_ok(cookies):
Expand All @@ -834,6 +842,7 @@ def test_baked_django_settings_staging_file_ok(cookies):
assert (
'"""Django staging settings for django-boilerplate project."""' in settings_file
)
assert 'ALLOWED_HOSTS = ["www.example.com"]' in settings_file


def test_baked_django_settings_test_file_ok(cookies):
Expand All @@ -844,6 +853,7 @@ def test_baked_django_settings_test_file_ok(cookies):
settings_file = settings_path.read_text().splitlines()

assert '"""Django test settings for django-boilerplate project."""' in settings_file
assert 'ALLOWED_HOSTS = ["www.example.com"]' in settings_file


def test_baked_django_urls_file_ok(cookies):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ Django==3.2.8
{% if cookiecutter.use_django_allauth == "y" %}
django-allauth==0.45.0
{% endif %}
django-environ==0.7.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-r base.txt
-r test.txt
django-debug-toolbar==3.2.2
django-debug-toolbar-template-profiler==2.0.2

pre-commit==2.15.0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-r base.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-r base.txt
14 changes: 10 additions & 4 deletions {{cookiecutter.git_project_name}}/config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@
import os
from django.utils.translation import ugettext_lazy as _
from .username_blacklist import data as username_blacklist
import environ


env = environ.Env(
# set casting, default value
DEBUG=(bool, False)
)

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

Expand All @@ -27,9 +33,8 @@
'DJANGO_SECRET_KEY', '!!!SET DJANGO_SECRET_KEY!!!')

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

ALLOWED_HOSTS = ["{{cookiecutter.ALLOWED_HOSTS}}"]
# False if not in os.environ because of casting above
DEBUG = env('DEBUG')

INTERNAL_IPS = ["{{cookiecutter.INTERNAL_IPS}}"]

Expand Down Expand Up @@ -188,7 +193,7 @@

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"


{% if cookiecutter.use_django_allauth == "y" %}
# Django Allauth Settings
# https://django-allauth.readthedocs.io/en/latest/configuration.html
ACCOUNT_AUTHENTICATION_METHOD = "username_email" # Default dj-allauth == username
Expand All @@ -200,3 +205,4 @@
ACCOUNT_USERNAME_REQUIRED = True # Default dj-allauth
ACCOUNT_USERNAME_MIN_LENGTH = 3 # Default dj-allauth == 1
ACCOUNT_USERNAME_BLACKLIST = username_blacklist
{% endif %}
14 changes: 9 additions & 5 deletions {{cookiecutter.git_project_name}}/config/settings/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

from .base import * # noqa

SECRET_KEY = os.environ.get("DJANGO_SECRET_KEY", "!!!SET DJANGO_SECRET_KEY!!!")
# Read from environment variables file
environ.Env.read_env(os.path.join(BASE_DIR, ".env/.local"))

DEBUG = True
SECRET_KEY = env("SECRET_KEY")

DEBUG = env("DEBUG", default=False)

# https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts
ALLOWED_HOSTS = ["localhost", "0.0.0.0", "127.0.0.1"]

EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
ALLOWED_HOSTS = env.list("ALLOWED_HOSTS")

INTERNAL_IPS = env.list("INTERNAL_IPS")

INTERNAL_IPS = ["127.0.0.1", "10.0.2.2"]
EMAIL_BACKEND = env("EMAIL_BACKEND")


# django-debug-toolbar
Expand Down
18 changes: 18 additions & 0 deletions {{cookiecutter.git_project_name}}/config/settings/production.py
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
"""Django production settings for {{cookiecutter.git_project_name}} project."""

from .base import * # noqa
from django.conf import settings

# Read from environment variables file
environ.Env.read_env(os.path.join(BASE_DIR, ".env/.production"))

SECRET_KEY = env("SECRET_KEY")

DEBUG = env("DEBUG", default=False)

ALLOWED_HOSTS = ["{{cookiecutter.ALLOWED_HOSTS}}"]

# Check it is safe to run in producion.
assert not settings.Debug, "DEBUG mode should be off for production." # nosec
assert (
env("SECRET_KEY") != "!!!UNSECURE_PRODUCTION_SECRET!!!"
), "SECRET_KEY must be set for production." # nosec
18 changes: 18 additions & 0 deletions {{cookiecutter.git_project_name}}/config/settings/staging.py
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
"""Django staging settings for {{cookiecutter.git_project_name}} project."""

from .base import * # noqa
from django.conf import settings

# Read from environment variables file
environ.Env.read_env(os.path.join(BASE_DIR, ".env/.staging"))

SECRET_KEY = env("SECRET_KEY")

DEBUG = env("DEBUG", default=False)

ALLOWED_HOSTS = ["{{cookiecutter.ALLOWED_HOSTS}}"]

# Check it is safe to run in producion.
assert not settings.Debug, "DEBUG mode should be off for production." # nosec
assert (
env("SECRET_KEY") != "!!!UNSECURE_STAGING_SECRET!!!"
), "SECRET_KEY must be set for production." # nosec
9 changes: 8 additions & 1 deletion {{cookiecutter.git_project_name}}/config/settings/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
from .base import * # noqa
from django.conf import settings

DEBUG = False
# Read from environment variables file
environ.Env.read_env(os.path.join(BASE_DIR, ".env/.testing"))

SECRET_KEY = env("SECRET_KEY")

DEBUG = env("DEBUG", default=False)

ALLOWED_HOSTS = ["{{cookiecutter.ALLOWED_HOSTS}}"]

assert not settings.Debug, "DEBUG mode should be off for testing." # nosec

Expand Down

0 comments on commit 8127aef

Please sign in to comment.