Skip to content

Commit

Permalink
Merge pull request #386 from datamade/django_environ
Browse files Browse the repository at this point in the history
use django-environ for managing environmental settings in cookicutters
  • Loading branch information
fgregg authored Oct 10, 2024
2 parents 20f5ab4 + 55e6eac commit de6c218
Show file tree
Hide file tree
Showing 17 changed files with 86 additions and 73 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ jobs:
nodeenv -p && \
npm install -g npm-check-updates && \
find ./docker -name package.json -exec ncu --packageFile {} -u \;
- name: Push to main branch
run: |
git config --global user.name "xmedr"
git config --global user.email "[email protected]"
# - name: Push to main branch
# run: |
# git config --global user.name "xmedr"
# git config --global user.email "[email protected]"

git add .
git commit -m "Update pypi and node dependencies"
git push origin ${{ github.ref_name }}
# git add .
# git commit -m "Update pypi and node dependencies"
# git push origin ${{ github.ref_name }}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@
"css-loader": "^7.1.2",
"eslint": "^9.12.0",
"eslint-config-prettier": "^9.1.0",
"eslint-config-react-app": "^7.0.1",
"eslint-plugin-flowtype": "^8.0.3",
"eslint-plugin-import": "^2.31.0",
"eslint-plugin-jsx-a11y": "^6.10.0",
"eslint-plugin-react": "^7.37.1",
"eslint-plugin-react-hooks": "^4.6.2",
"style-loader": "^4.0.0",
"webpack": "^5.95.0",
"webpack-bundle-tracker": "^3.1.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Django
psycopg2
gunicorn
dj-database-url
django-environ
whitenoise
django-webpack-loader
sentry-sdk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,37 @@
"""
import os

import dj_database_url
import environ
import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration

from {{cookiecutter.module_name}}.logging import before_send


env = environ.Env(DJANGO_DEBUG=(bool, True),
DJANGO_ALLOWED_HOSTS=(list, []),
SENTRY_DSN=(str, ''),
POSTGRES_REQUIRE_SSL=(bool, False),
DJANGO_ALLOW_SEARCH_INDEXING=(bool, False))

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Retrieve the secret key from the DJANGO_SECRET_KEY environment variable
SECRET_KEY = os.environ['DJANGO_SECRET_KEY']
SECRET_KEY = env('DJANGO_SECRET_KEY')

# Set the DJANGO_DEBUG environment variable to False to disable debug mode
DEBUG = False if os.getenv('DJANGO_DEBUG', True) == 'False' else True
DEBUG = env("DJANGO_DEBUG")

# Define DJANGO_ALLOWED_HOSTS as a comma-separated list of valid hosts,
# e.g. localhost,127.0.0.1,.herokuapp.com
allowed_hosts = os.getenv('DJANGO_ALLOWED_HOSTS', [])
ALLOWED_HOSTS = allowed_hosts.split(',') if allowed_hosts else []
ALLOWED_HOSTS = env('DJANGO_ALLOWED_HOSTS')


# Configure Sentry for error logging
if os.getenv('SENTRY_DSN'):
if env('SENTRY_DSN'):
sentry_sdk.init(
dsn=os.environ['SENTRY_DSN'],
dsn=env('SENTRY_DSN'),
before_send=before_send,
integrations=[DjangoIntegration()],
)
Expand Down Expand Up @@ -90,17 +96,21 @@

DATABASES = {}

DATABASES['default'] = dj_database_url.parse(
os.getenv('DATABASE_URL', 'postgres://postgres:postgres@postgres:5432/{{cookiecutter.pg_db}}'),
conn_max_age=600,
ssl_require=True if os.getenv('POSTGRES_REQUIRE_SSL') else False{% if cookiecutter.postgis == 'True' %},
engine='django.contrib.gis.db.backends.postgis'{% endif %}
# env.db_url returns a dictionary of database connection settings derived
# from the connection string. we have some settings we want to always use
# so we uniont those settings with one coming from the db string
DATABASES["default"] = {
"CONN_MAX_AGE": 600,
"OPTIONS": {"sslmode": "require" if env("POSTGRES_REQUIRE_SSL") else "prefer"},
} | env.db_url(
"DATABASE_URL",
default='{{ "postgis" if cookiecutter.postgis else "postgres" }}://postgres:postgres@postgres:5432/{{cookiecutter.pg_db}}',
)

# Caching
# https://docs.djangoproject.com/en/3.0/topics/cache/

cache_backend = 'dummy.DummyCache' if DEBUG is True else 'db.DatabaseCache'
cache_backend = 'dummy.DummyCache' if DEBUG else 'db.DatabaseCache'
CACHES = {
'default': {
'BACKEND': f'django.core.cache.backends.{cache_backend}',
Expand Down Expand Up @@ -146,9 +156,9 @@
STATIC_URL = '/static/'
STATIC_ROOT = '/static'
STATICFILES_DIRS = (os.path.join(BASE_DIR, "assets"),)
STATICFILES_STORAGE = os.getenv(
STATICFILES_STORAGE = env(
'DJANGO_STATICFILES_STORAGE',
'whitenoise.storage.CompressedManifestStaticFilesStorage'
default='whitenoise.storage.CompressedManifestStaticFilesStorage'
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
Expand All @@ -171,7 +181,7 @@

# Disable search indexing by default. Set DJANGO_ALLOW_SEARCH_INDEXING env
# variable to True in container or deploymeny environment to enable indexing.
if os.getenv('DJANGO_ALLOW_SEARCH_INDEXING', False) == 'True':
if env('DJANGO_ALLOW_SEARCH_INDEXING'):
ALLOW_SEARCH_INDEXING = True
else:
ALLOW_SEARCH_INDEXING = False
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,3 @@ ENV DJANGO_DEBUG 'False'

# Build static files into the container
RUN python manage.py collectstatic --noinput
RUN python manage.py compress
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,9 @@
"@babel/eslint-parser": "^7.25.7",
"eslint": "^9.12.0",
"eslint-config-prettier": "^9.1.0",
"eslint-config-react-app": "^7.0.1",
"eslint-plugin-flowtype": "^8.0.3",
"eslint-plugin-import": "^2.31.0",
"eslint-plugin-jsx-a11y": "^6.10.0",
"eslint-plugin-react": "^7.37.1",
"eslint-plugin-react-hooks": "^4.6.2"
"eslint-plugin-react": "^7.37.1"
},
"scripts": {
"develop": "sass --watch /app/{{ cookiecutter.module_name }}/static/scss/custom.scss:/app/{{ cookiecutter.module_name }}/static/css/bootstrap.custom.css"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
Django==5.1.1
psycopg2==2.9.9
gunicorn==23.0.0
dj-database-url==2.2.0
whitenoise==6.7.0
django-environ==0.11.2
django-compressor==4.5.1
django-storages==1.14.4
boto3==1.35.34
sentry-sdk==2.15.0
wagtail==6.2.2
csvkit==2.0.1
wagtail-modeladmin==2.0.0

pytest==8.3.3
pytest-django==4.9.0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from wagtail.core import blocks
from wagtail import blocks
from wagtail.contrib.table_block.blocks import TableBlock as WagtailTableBlock
from wagtail.images.blocks import ImageChooserBlock

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import os
import json

from django.core.files.storage import default_storage
from django.files.storage import default_storage
from django.apps import apps
from django.core.management.base import BaseCommand
from django.core.management import call_command
from django.core.exceptions import ObjectDoesNotExist
from django.management.base import BaseCommand
from django.management import call_command
from django.exceptions import ObjectDoesNotExist

from wagtail.core.models import Site, Page, PageRevision
from wagtail.models import Site, Page, PageRevision
from wagtail.images.models import Image


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from django.db import migrations, models
import django.db.models.deletion
import {{ cookiecutter.module_name }}.blocks
import wagtail.core.blocks
import wagtail.core.fields
import wagtail.blocks
import wagtail.fields
import wagtail.embeds.blocks
import wagtail.images.blocks

Expand All @@ -22,8 +22,8 @@ class Migration(migrations.Migration):
name='HomePage',
fields=[
('page_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='wagtailcore.page')),
('body', wagtail.core.fields.StreamField([('paragraph', wagtail.core.blocks.RichTextBlock()), ('heading', wagtail.core.blocks.CharBlock(form_classname='full title', icon='title')), ('accordion', wagtail.core.blocks.ListBlock(wagtail.core.blocks.StructBlock([('title', wagtail.core.blocks.CharBlock()), ('paragraph', wagtail.core.blocks.RichTextBlock())]), icon='list-ul')), ('button', wagtail.core.blocks.StructBlock([('text', wagtail.core.blocks.CharBlock()), ('link', wagtail.core.blocks.URLBlock())])), ('callout', wagtail.core.blocks.StructBlock([('paragraph', wagtail.core.blocks.RichTextBlock())])), ('table', {{ cookiecutter.module_name }}.blocks.TableBlock()), ('embedded_media', wagtail.core.blocks.StructBlock([('title', wagtail.core.blocks.CharBlock(required=False)), ('media_link', wagtail.embeds.blocks.EmbedBlock()), ('description', wagtail.core.blocks.TextBlock(required=False))], icon='media')), ('team_members', wagtail.core.blocks.ListBlock(wagtail.core.blocks.StructBlock([('first_name', wagtail.core.blocks.CharBlock()), ('last_name', wagtail.core.blocks.CharBlock()), ('position', wagtail.core.blocks.CharBlock()), ('photo', wagtail.images.blocks.ImageChooserBlock(required=False))]), icon='group'))])),
('intro_text', wagtail.core.fields.RichTextField(blank=True)),
('body', wagtail.fields.StreamField([('paragraph', wagtail.blocks.RichTextBlock()), ('heading', wagtail.blocks.CharBlock(form_classname='full title', icon='title')), ('accordion', wagtail.blocks.ListBlock(wagtail.blocks.StructBlock([('title', wagtail.blocks.CharBlock()), ('paragraph', wagtail.blocks.RichTextBlock())]), icon='list-ul')), ('button', wagtail.blocks.StructBlock([('text', wagtail.blocks.CharBlock()), ('link', wagtail.blocks.URLBlock())])), ('callout', wagtail.blocks.StructBlock([('paragraph', wagtail.blocks.RichTextBlock())])), ('table', {{ cookiecutter.module_name }}.blocks.TableBlock()), ('embedded_media', wagtail.blocks.StructBlock([('title', wagtail.blocks.CharBlock(required=False)), ('media_link', wagtail.embeds.blocks.EmbedBlock()), ('description', wagtail.blocks.TextBlock(required=False))], icon='media')), ('team_members', wagtail.blocks.ListBlock(wagtail.blocks.StructBlock([('first_name', wagtail.blocks.CharBlock()), ('last_name', wagtail.blocks.CharBlock()), ('position', wagtail.blocks.CharBlock()), ('photo', wagtail.images.blocks.ImageChooserBlock(required=False))]), icon='group'))])),
('intro_text', wagtail.fields.RichTextField(blank=True)),
],
options={
'abstract': False,
Expand All @@ -34,7 +34,7 @@ class Migration(migrations.Migration):
name='StaticPage',
fields=[
('page_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='wagtailcore.page')),
('body', wagtail.core.fields.StreamField([('paragraph', wagtail.core.blocks.RichTextBlock()), ('heading', wagtail.core.blocks.CharBlock(form_classname='full title', icon='title')), ('accordion', wagtail.core.blocks.ListBlock(wagtail.core.blocks.StructBlock([('title', wagtail.core.blocks.CharBlock()), ('paragraph', wagtail.core.blocks.RichTextBlock())]), icon='list-ul')), ('button', wagtail.core.blocks.StructBlock([('text', wagtail.core.blocks.CharBlock()), ('link', wagtail.core.blocks.URLBlock())])), ('callout', wagtail.core.blocks.StructBlock([('paragraph', wagtail.core.blocks.RichTextBlock())])), ('table', {{ cookiecutter.module_name }}.blocks.TableBlock()), ('embedded_media', wagtail.core.blocks.StructBlock([('title', wagtail.core.blocks.CharBlock(required=False)), ('media_link', wagtail.embeds.blocks.EmbedBlock()), ('description', wagtail.core.blocks.TextBlock(required=False))], icon='media')), ('team_members', wagtail.core.blocks.ListBlock(wagtail.core.blocks.StructBlock([('first_name', wagtail.core.blocks.CharBlock()), ('last_name', wagtail.core.blocks.CharBlock()), ('position', wagtail.core.blocks.CharBlock()), ('photo', wagtail.images.blocks.ImageChooserBlock(required=False))]), icon='group'))])),
('body', wagtail.fields.StreamField([('paragraph', wagtail.blocks.RichTextBlock()), ('heading', wagtail.blocks.CharBlock(form_classname='full title', icon='title')), ('accordion', wagtail.blocks.ListBlock(wagtail.blocks.StructBlock([('title', wagtail.blocks.CharBlock()), ('paragraph', wagtail.blocks.RichTextBlock())]), icon='list-ul')), ('button', wagtail.blocks.StructBlock([('text', wagtail.blocks.CharBlock()), ('link', wagtail.blocks.URLBlock())])), ('callout', wagtail.blocks.StructBlock([('paragraph', wagtail.blocks.RichTextBlock())])), ('table', {{ cookiecutter.module_name }}.blocks.TableBlock()), ('embedded_media', wagtail.blocks.StructBlock([('title', wagtail.blocks.CharBlock(required=False)), ('media_link', wagtail.embeds.blocks.EmbedBlock()), ('description', wagtail.blocks.TextBlock(required=False))], icon='media')), ('team_members', wagtail.blocks.ListBlock(wagtail.blocks.StructBlock([('first_name', wagtail.blocks.CharBlock()), ('last_name', wagtail.blocks.CharBlock()), ('position', wagtail.blocks.CharBlock()), ('photo', wagtail.images.blocks.ImageChooserBlock(required=False))]), icon='group'))])),
],
options={
'abstract': False,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from wagtail.core.models import Page
from wagtail.core.fields import RichTextField, StreamField
from wagtail.core import blocks
from wagtail.models import Page
from wagtail.fields import RichTextField, StreamField
from wagtail import blocks
from wagtail.embeds.blocks import EmbedBlock
from wagtail.admin.edit_handlers import StreamFieldPanel, FieldPanel
from wagtail.admin.panels import FieldPanel

from {{cookiecutter.module_name}}.blocks import (
AccordionBlock,
Expand Down Expand Up @@ -53,7 +53,7 @@ class Meta:

class StaticPage(BasePage):
content_panels = Page.content_panels + [
StreamFieldPanel('body'),
FieldPanel('body'),
]

parent_page_types = ['{{ cookiecutter.module_name }}.HomePage']
Expand All @@ -64,7 +64,7 @@ class HomePage(BasePage):

content_panels = Page.content_panels + [
FieldPanel('intro_text'),
StreamFieldPanel('body')
FieldPanel('body')
]

max_count = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,38 @@
"""
import os

import dj_database_url
import environ
import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration
from django.templatetags.static import static

from {{cookiecutter.module_name}}.logging import before_send


env = environ.Env(DJANGO_DEBUG=(bool, True),
DJANGO_ALLOWED_HOSTS=(list, []),
SENTRY_DSN=(str, ''),
POSTGRES_REQUIRE_SSL=(bool, False),
DJANGO_ALLOW_SEARCH_INDEXING=(bool, False))

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Retrieve the secret key from the DJANGO_SECRET_KEY environment variable
SECRET_KEY = os.environ['DJANGO_SECRET_KEY']
SECRET_KEY = env('DJANGO_SECRET_KEY')

# Set the DJANGO_DEBUG environment variable to False to disable debug mode
DEBUG = False if os.getenv('DJANGO_DEBUG', True) == 'False' else True
DEBUG = env('DJANGO_DEBUG')

# Define DJANGO_ALLOWED_HOSTS as a comma-separated list of valid hosts,
# e.g. localhost,127.0.0.1,.herokuapp.com
allowed_hosts = os.getenv('DJANGO_ALLOWED_HOSTS', [])
ALLOWED_HOSTS = allowed_hosts.split(',') if allowed_hosts else []
ALLOWED_HOSTS = env('DJANGO_ALLOWED_HOSTS')


# Configure Sentry for error logging
if os.getenv('SENTRY_DSN'):
if env('SENTRY_DSN'):
sentry_sdk.init(
dsn=os.environ['SENTRY_DSN'],
dsn=env('SENTRY_DSN'),
before_send=before_send,
integrations=[DjangoIntegration()],
)
Expand All @@ -51,7 +57,7 @@
'django.contrib.messages',
'django.contrib.staticfiles',
'wagtail.contrib.forms',
'wagtail.contrib.modeladmin',
'wagtail_modeladmin',
'wagtail.contrib.redirects',
'wagtail.contrib.simple_translation',
'wagtail.contrib.table_block',
Expand All @@ -64,7 +70,7 @@
'wagtail.images',
'wagtail.search',
'wagtail.admin',
'wagtail.core',
'wagtail',
'modelcluster',
'taggit',
'compressor',
Expand Down Expand Up @@ -117,12 +123,15 @@

DATABASES = {}

DATABASES['default'] = dj_database_url.parse(
os.getenv('DATABASE_URL',
'postgis://postgres:postgres@postgres:5432/{{ cookiecutter.module_name }}'),
conn_max_age=600,
ssl_require=True if os.getenv('POSTGRES_REQUIRE_SSL') else False,
engine='django.contrib.gis.db.backends.postgis'
# env.db_url returns a dictionary of database connection settings derived
# from the connection string. we have some settings we want to always use
# so we uniont those settings with one coming from the db string
DATABASES["default"] = {
"CONN_MAX_AGE": 600,
"OPTIONS": {"sslmode": "require" if env("POSTGRES_REQUIRE_SSL") else "prefer"},
} | env.db_url(
"DATABASE_URL",
default='postgis://postgres:postgres@postgres:5432/{{cookiecutter.module_name}}',
)

# Caching
Expand Down Expand Up @@ -173,9 +182,9 @@

STATIC_URL = '/static/'
STATIC_ROOT = '/static'
STATICFILES_STORAGE = os.getenv(
STATICFILES_STORAGE = env(
'DJANGO_STATICFILES_STORAGE',
'whitenoise.storage.CompressedManifestStaticFilesStorage'
default='whitenoise.storage.CompressedManifestStaticFilesStorage'
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django import template
from wagtail.core.models import Page
from wagtail.models import Page

register = template.Library()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django import template
from wagtail.core.models import Page
from wagtail.models import Page

register = template.Library()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from .views import ReactView

from wagtail.admin import urls as wagtailadmin_urls
from wagtail.core import urls as wagtail_urls
from wagtail import urls as wagtail_urls
from wagtail.documents import urls as wagtaildocs_urls


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from wagtail.core.models import Page
from wagtail.models import Page


def get_site_menu():
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from django.utils.html import escape
from wagtail.admin.rich_text.converters.html_to_contentstate import BlockElementHandler
import wagtail.admin.rich_text.editors.draftail.features as draftail_features
from wagtail.core import hooks
from wagtail.core.rich_text import LinkHandler
from wagtail import hooks
from wagtail.rich_text import LinkHandler


@hooks.register('register_rich_text_features')
Expand Down

0 comments on commit de6c218

Please sign in to comment.