Ensure that empty environment values get defaults

The `os.environ.get` default is _not_ used if the environment variable is set to an empty value. This can lead to confusion if, for example, the `TIME_ZONE` environment variable is set to empty. The expected behavior is a fallback on "Etc/UTC".
This commit is contained in:
Christopher C. Wells 2020-02-04 11:28:16 -08:00
parent c0db24555a
commit 8e5364d891
3 changed files with 16 additions and 11 deletions

View File

@ -20,8 +20,8 @@ load_dotenv(find_dotenv())
# Required settings # Required settings
ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', '*').split(',') ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', '*').split(',')
SECRET_KEY = os.environ.get('SECRET_KEY', None) SECRET_KEY = os.environ.get('SECRET_KEY') or None
DEBUG = os.environ.get('DEBUG', False) DEBUG = os.environ.get('DEBUG') or False
# Applications # Applications
@ -123,7 +123,7 @@ LOGOUT_REDIRECT_URL = '/login/'
USE_TZ = True USE_TZ = True
TIME_ZONE = os.environ.get('TIME_ZONE', 'Etc/UTC') TIME_ZONE = os.environ.get('TIME_ZONE') or 'Etc/UTC'
# Internationalization # Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/ # https://docs.djangoproject.com/en/3.0/topics/i18n/
@ -179,11 +179,11 @@ MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/' MEDIA_URL = '/media/'
AWS_STORAGE_BUCKET_NAME = os.environ.get('AWS_STORAGE_BUCKET_NAME', None) AWS_STORAGE_BUCKET_NAME = os.environ.get('AWS_STORAGE_BUCKET_NAME') or None
AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID', None) AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID') or None
AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY', None) AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY') or None
if AWS_STORAGE_BUCKET_NAME: if AWS_STORAGE_BUCKET_NAME:
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage' DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
@ -217,7 +217,7 @@ REST_FRAMEWORK = {
# See README.md#configuration for details about these settings. # See README.md#configuration for details about these settings.
BABY_BUDDY = { BABY_BUDDY = {
'NAP_START_MIN': os.environ.get('NAP_START_MIN', '06:00'), 'NAP_START_MIN': os.environ.get('NAP_START_MIN') or '06:00',
'NAP_START_MAX': os.environ.get('NAP_START_MAX', '18:00'), 'NAP_START_MAX': os.environ.get('NAP_START_MAX') or '18:00',
'ALLOW_UPLOADS': os.environ.get('ALLOW_UPLOADS', True) 'ALLOW_UPLOADS': os.environ.get('ALLOW_UPLOADS') or True
} }

View File

@ -1,7 +1,10 @@
from .base import * from .base import *
# Default to not allow uploads.
# This requires extra setup in an Elastic Beanstalk environment so it should be
# set explicitly.
BABY_BUDDY['ALLOW_UPLOADS'] = os.environ.get('ALLOW_UPLOADS', False) BABY_BUDDY['ALLOW_UPLOADS'] = os.environ.get('ALLOW_UPLOADS') or False
# Database # Database

View File

@ -2,8 +2,10 @@ import dj_database_url
from .base import * from .base import *
# Default to not allow uploads.
# Heroku does not support file storage for this functionality.
BABY_BUDDY['ALLOW_UPLOADS'] = os.environ.get('ALLOW_UPLOADS', False) BABY_BUDDY['ALLOW_UPLOADS'] = os.environ.get('ALLOW_UPLOADS') or False
# Database # Database