From 3c5aba9ae77ca9127efb981e0ec1b15f568e56f9 Mon Sep 17 00:00:00 2001 From: Christopher Charbonneau Wells Date: Thu, 30 Nov 2017 22:54:50 -0500 Subject: [PATCH] Check for and load environment variables from a `.env` file. --- .gitignore | 3 ++- README.md | 18 ++++++++++++++---- babybuddy/settings/base.py | 6 ++++++ babybuddy/settings/development.py | 1 - babybuddy/wsgi.py | 9 +++++---- manage.py | 10 +++++++--- 6 files changed, 34 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 274b54c5..5fbeff4b 100644 --- a/.gitignore +++ b/.gitignore @@ -5,7 +5,8 @@ # database *.sqlite3 -# docker +# enviornment +.env docker.env # npm folder diff --git a/README.md b/README.md index b617d716..be1cb35d 100644 --- a/README.md +++ b/README.md @@ -78,9 +78,8 @@ redeploy the app (e.g. if there are errors or settings are changed). A Docker deploy requires [Docker](http://docker.com/) and [Docker Compose](https://docs.docker.com/compose/overview/) to create two -containers - one for the database and one for the application. - -Baby Buddy uses a [multi-stage build](https://docs.docker.com/engine/userguide/eng-image/multistage-build/), +containers - one for the database and one for the application. Baby Buddy uses a +[multi-stage build](https://docs.docker.com/engine/userguide/eng-image/multistage-build/), which requires Docker version 17.05 or newer. 1. Copy the `docker.env.example` to `docker.env` and set the `ALLOWED_HOSTS` and @@ -280,7 +279,18 @@ Python 3.x, nginx, uwsgi and sqlite and should be sufficient for a few users ## Configuration -Environment variables can be used to define a number of configuration settings: +Environment variables can be used to define a number of configuration settings. +Baby Buddy will check the application directory structure for an `.env` file or +take these variables from the system environment. **System environment variables +take precedence over the contents of an `.env` file.** + +- [`ALLOWED_HOSTS`](#allowed_hosts) +- [`ALLOW_UPLOADS`](#allow_uploads) +- [`DEBUG`](#debug) +- [`NAP_START_MAX`](#nap_start_max) +- [`NAP_START_MIN`](#nap_start_min) +- [`SECRET_KEY`](#secret_key) +- [`TIME_ZONE`](#time_zone) ### `ALLOWED_HOSTS` diff --git a/babybuddy/settings/base.py b/babybuddy/settings/base.py index 05e23e3c..826a44be 100644 --- a/babybuddy/settings/base.py +++ b/babybuddy/settings/base.py @@ -1,5 +1,7 @@ import os +from dotenv import load_dotenv, find_dotenv + # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname( os.path.dirname( @@ -9,6 +11,10 @@ BASE_DIR = os.path.dirname( ) ) +# Environment variables +# Check for and load environment variables from a .env file. + +load_dotenv(find_dotenv()) # Required settings diff --git a/babybuddy/settings/development.py b/babybuddy/settings/development.py index ffccea49..0accffab 100644 --- a/babybuddy/settings/development.py +++ b/babybuddy/settings/development.py @@ -5,7 +5,6 @@ from .base import * SECRET_KEY = 'CHANGE ME' DEBUG = True -TIME_ZONE = 'America/New_York' # Database diff --git a/babybuddy/wsgi.py b/babybuddy/wsgi.py index 6ed1d486..4fbac142 100644 --- a/babybuddy/wsgi.py +++ b/babybuddy/wsgi.py @@ -1,8 +1,9 @@ -import os - from django.core.wsgi import get_wsgi_application -os.environ.setdefault( - "DJANGO_SETTINGS_MODULE", "babybuddy.settings.development") +from dotenv import load_dotenv, find_dotenv + +# Environment variables +# Check for and load environment variables from a .env file. +load_dotenv(find_dotenv()) application = get_wsgi_application() diff --git a/manage.py b/manage.py index 4c4a55c1..1da361f3 100755 --- a/manage.py +++ b/manage.py @@ -1,10 +1,14 @@ #!/usr/bin/env python -import os import sys +from dotenv import load_dotenv, find_dotenv + if __name__ == "__main__": - os.environ.setdefault( - "DJANGO_SETTINGS_MODULE", "babybuddy.settings.development") + + # Environment variables + # Check for and load environment variables from a .env file. + load_dotenv(find_dotenv()) + try: from django.core.management import execute_from_command_line except ImportError: