diff --git a/Dockerfile b/Dockerfile index 1060ffb..31e74a9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,9 +9,11 @@ COPY requirements.txt $APPS_DIR/ WORKDIR $APPS_DIR # uWSGI requires linux-headers +# django-split-settings needs ca-certificates as they changed on PyPi RUN set -ex \ && buildDeps=' \ build-base \ + ca-certificates \ linux-headers \ python3-dev \ ' \ diff --git a/TODO b/TODO index cba15df..32d505e 100644 --- a/TODO +++ b/TODO @@ -1,6 +1,5 @@ # À faire - * tester django-split-settings pour voir ce que ça donne * étudier la possibilité à l'utilisateur, via des variables d'environnement, de configurer un email, une autre BDD, etc. ## Dépôt / code diff --git a/collection/collection/components/__init__.py b/collection/collection/components/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/collection/collection/components/common.py b/collection/collection/components/common.py new file mode 100644 index 0000000..a0532e3 --- /dev/null +++ b/collection/collection/components/common.py @@ -0,0 +1,89 @@ +""" +Django settings for collection project. + +Generated by 'django-admin startproject' using Django 1.11. + +For more information on this file, see +https://docs.djangoproject.com/en/1.11/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/1.11/ref/settings/ +""" +import os + + +# Build paths inside the project like this: os.path.join(BASE_DIR, ...) +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + +DEBUG = False + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'core', + 'games.apps.GamesConfig', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.locale.LocaleMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'collection.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'collection.wsgi.application' + +# Password validation +# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/1.11/howto/static-files/ + +STATIC_URL = '/static/' +STATIC_ROOT = os.path.join(os.path.abspath(os.path.curdir), 'static') +if os.getenv('STATIC_ROOT', None): + STATIC_ROOT = os.path.abspath(os.getenv('STATIC_ROOT')) + diff --git a/collection/collection/components/database.py b/collection/collection/components/database.py new file mode 100644 index 0000000..e73197f --- /dev/null +++ b/collection/collection/components/database.py @@ -0,0 +1,15 @@ +# Database +# https://docs.djangoproject.com/en/1.11/ref/settings/#databases + +DB_DIR = os.getenv('DB_DIR', os.path.join(BASE_DIR, 'db')) + +if not os.path.exists(DB_DIR): + os.makedirs(DB_DIR) + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(BASE_DIR, 'db', 'db.sqlite3'), + } +} + diff --git a/collection/collection/components/i18n.py b/collection/collection/components/i18n.py new file mode 100644 index 0000000..2f0bb9d --- /dev/null +++ b/collection/collection/components/i18n.py @@ -0,0 +1,25 @@ +# Internationalization +# https://docs.djangoproject.com/en/1.11/topics/i18n/ +from django.utils.translation import ugettext_lazy as _ + +LANGUAGE_CODE = 'fr' + +LANGUAGES = ( + ('fr', _('French')), + ('en', _('English')), +) + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_L10N = False + +USE_TZ = False + +# Translation directories +LOCALE_PATHS = [ + os.path.join(BASE_DIR, 'conf/locale'), + os.path.join(BASE_DIR, 'collection/locale') +] + diff --git a/collection/collection/environments/__init__.py b/collection/collection/environments/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/collection/collection/environments/development.py b/collection/collection/environments/development.py new file mode 100644 index 0000000..8ecbabf --- /dev/null +++ b/collection/collection/environments/development.py @@ -0,0 +1,11 @@ +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'tqma23#v!#ecse_gz_u(1oa6+x%1uyi718an9%nefqhi$0q_eg' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + diff --git a/collection/collection/production.py b/collection/collection/environments/production.py similarity index 90% rename from collection/collection/production.py rename to collection/collection/environments/production.py index cc27de8..e56ce98 100644 --- a/collection/collection/production.py +++ b/collection/collection/environments/production.py @@ -1,8 +1,6 @@ """ Django production settings """ -import os -from collection.settings import * # NOQA DEBUG = os.getenv('DEBUG', False) diff --git a/collection/collection/settings.py b/collection/collection/settings.py index 744e7a3..1cf75c8 100644 --- a/collection/collection/settings.py +++ b/collection/collection/settings.py @@ -1,139 +1,29 @@ """ -Django settings for collection project. +This is a django-split-settings main file. +For more information read this: +https://github.com/sobolevn/django-split-settings -Generated by 'django-admin startproject' using Django 1.11. +Default environment is `developement`. -For more information on this file, see -https://docs.djangoproject.com/en/1.11/topics/settings/ - -For the full list of settings and their values, see -https://docs.djangoproject.com/en/1.11/ref/settings/ +To change settings file: +`DJANGO_ENV=production python manage.py runserver` """ -import os -from django.utils.translation import ugettext_lazy as _ +from split_settings.tools import optional, include +from os import environ +ENV = environ.get('DJANGO_ENV') or 'development' -# Build paths inside the project like this: os.path.join(BASE_DIR, ...) -BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +base_settings = [ + 'components/common.py', # standard django settings + 'components/database.py', # SQLite 3 + 'components/i18n.py', # Internationalisation and localization - -# Quick-start development settings - unsuitable for production -# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/ - -# SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'tqma23#v!#ecse_gz_u(1oa6+x%1uyi718an9%nefqhi$0q_eg' - -# SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True - -ALLOWED_HOSTS = [] - - -# Application definition - -INSTALLED_APPS = [ - 'django.contrib.admin', - 'django.contrib.auth', - 'django.contrib.contenttypes', - 'django.contrib.sessions', - 'django.contrib.messages', - 'django.contrib.staticfiles', - 'core', - 'games.apps.GamesConfig', + # Select the right env: + 'environments/%s.py' % ENV, + # Optionally override some settings: + optional('environments/local_settings.py'), ] -MIDDLEWARE = [ - 'django.middleware.security.SecurityMiddleware', - 'django.contrib.sessions.middleware.SessionMiddleware', - 'django.middleware.locale.LocaleMiddleware', - 'django.middleware.common.CommonMiddleware', - 'django.middleware.csrf.CsrfViewMiddleware', - 'django.contrib.auth.middleware.AuthenticationMiddleware', - 'django.contrib.messages.middleware.MessageMiddleware', - 'django.middleware.clickjacking.XFrameOptionsMiddleware', -] - -ROOT_URLCONF = 'collection.urls' - -TEMPLATES = [ - { - 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [], - 'APP_DIRS': True, - 'OPTIONS': { - 'context_processors': [ - 'django.template.context_processors.debug', - 'django.template.context_processors.request', - 'django.contrib.auth.context_processors.auth', - 'django.contrib.messages.context_processors.messages', - ], - }, - }, -] - -WSGI_APPLICATION = 'collection.wsgi.application' - - -# Database -# https://docs.djangoproject.com/en/1.11/ref/settings/#databases - -DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': os.path.join(BASE_DIR, 'db', 'db.sqlite3'), - } -} - - -# Password validation -# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators - -AUTH_PASSWORD_VALIDATORS = [ - { - 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', - }, - { - 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', - }, - { - 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', - }, - { - 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', - }, -] - - -# Internationalization -# https://docs.djangoproject.com/en/1.11/topics/i18n/ - -LANGUAGE_CODE = 'fr' - -LANGUAGES = ( - ('fr', _('French')), - ('en', _('English')), -) - -TIME_ZONE = 'UTC' - -USE_I18N = True - -USE_L10N = False - -USE_TZ = False - - -# Static files (CSS, JavaScript, Images) -# https://docs.djangoproject.com/en/1.11/howto/static-files/ - -STATIC_URL = '/static/' -STATIC_ROOT = os.path.join(os.path.abspath(os.path.curdir), 'static') -if os.getenv('STATIC_ROOT', None): - STATIC_ROOT = os.path.abspath(os.getenv('STATIC_ROOT')) - -# Translation directories -LOCALE_PATHS = [ - os.path.join(BASE_DIR, 'conf/locale'), - os.path.join(BASE_DIR, 'collection/locale') -] +# Include settings: +include(*base_settings) diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 063b041..073a4aa 100755 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -5,10 +5,10 @@ set -e chown -R guest "$DB_DIR" if [ "$1" = 'dev' ]; then - export DJANGO_SETTINGS_MODULE=collection.settings + export DJANGO_ENV='development' exec python3 manage.py runserver 0.0.0.0:8000 elif [ "$1" = 'prod' ]; then - export DJANGO_SETTINGS_MODULE=collection.production + export DJANGO_ENV='production' # Collect static files python3 manage.py collectstatic --noinput --clear -v 0 exec uwsgi --ini uwsgi.ini --pythonpath "$APPS_DIR" --static-map=/static/="$STATIC_ROOT" diff --git a/requirements.txt b/requirements.txt index 0b4bbf6..7b2ed4b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ Django==1.11.4 +django-split-settings==0.2.5 PyYAML==3.12 uWSGI==2.0.15