From 87eddec5ecd47b3af6d2ba7041575a81e7ca7c4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Fri, 13 Oct 2017 14:17:23 +0200 Subject: [PATCH] Better organization of the settings files Fixes #8 --- .gitignore | 2 +- .../common.py} | 97 ++++++++----------- Ernestophone/settings/local.py | 26 +++++ Ernestophone/settings/prod.py | 22 +++++ Ernestophone/settings/secret_example.py | 7 ++ manage.py | 5 +- requirements-devel.txt | 4 + requirements.txt | 2 +- 8 files changed, 105 insertions(+), 60 deletions(-) rename Ernestophone/{settings-example.py => settings/common.py} (52%) create mode 100644 Ernestophone/settings/local.py create mode 100644 Ernestophone/settings/prod.py create mode 100644 Ernestophone/settings/secret_example.py create mode 100644 requirements-devel.txt diff --git a/.gitignore b/.gitignore index 006567c..3f4eebc 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,5 @@ *.swp media/ db.sqlite3 -Ernestophone/settings.py +Ernestophone/settings/secret.py __pycache__ diff --git a/Ernestophone/settings-example.py b/Ernestophone/settings/common.py similarity index 52% rename from Ernestophone/settings-example.py rename to Ernestophone/settings/common.py index 99b2cbf..31d6568 100644 --- a/Ernestophone/settings-example.py +++ b/Ernestophone/settings/common.py @@ -1,41 +1,38 @@ -""" -Django settings for Ernestophone project. - -For more information on this file, see -https://docs.djangoproject.com/en/1.7/topics/settings/ - -For the full list of settings and their values, see -https://docs.djangoproject.com/en/1.7/ref/settings/ -""" - -# Build paths inside the project like this: os.path.join(BASE_DIR, ...) import os -BASE_DIR = os.path.dirname(os.path.dirname(__file__)) +try: + from . import secret +except ImportError: + raise ImportError( + "The secret.py file is missing.\n" + "For a development environment, simply copy secret_example.py" + ) -EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' -MEDIA_URL = '/media/' -MEDIA_ROOT = os.path.join(BASE_DIR, 'media') +def import_secret(name): + """ + Shorthand for importing a value from the secret module and raising an + informative exception if a secret is missing. + """ + try: + return getattr(secret, name) + except AttributeError: + raise RuntimeError("Secret missing: {}".format(name)) -STATIC_URL = '/static/' +SECRET_KEY = import_secret("SECRET_KEY") +ADMINS = import_secret("ADMINS") +SERVER_EMAIL = import_secret("SERVER_EMAIL") -# Quick-start development settings - unsuitable for production -# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/ +DBNAME = import_secret("DBNAME") +DBUSER = import_secret("DBUSER") +DBPASSWD = import_secret("DBPASSWD") -# SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'ax0|dG^AhtYfXPXgu3*e|k3l^yKpbK;$~(_%q/5C!H9yHA(Z(2' -# SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +BASE_DIR = os.path.join( + os.path.dirname(__file__), "..", ".." +) -ALLOWED_HOSTS = [] - -ACCOUNT_CREATION_PASS = 'dummy' - -# Application definition - -INSTALLED_APPS = ( +INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', @@ -47,9 +44,9 @@ INSTALLED_APPS = ( 'calendrier', 'propositions', 'pads', -) +] -MIDDLEWARE_CLASSES = ( +MIDDLEWARE_CLASSES = [ 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', @@ -57,24 +54,9 @@ MIDDLEWARE_CLASSES = ( 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', -) +] -ROOT_URLCONF = 'Ernestophone.urls' - -WSGI_APPLICATION = 'Ernestophone.wsgi.application' - - -# Database -# https://docs.djangoproject.com/en/1.7/ref/settings/#databases - -DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), - } -} - -# Templates +ROOT_URLCONF = "Ernestophone.urls" TEMPLATES = [{ 'BACKEND': 'django.template.backends.django.DjangoTemplates', @@ -95,24 +77,25 @@ TEMPLATES = [{ } }] +DATABASES = { + "default": { + "ENGINE": "django.db.backends.postgresql_psycopg2", + "NAME": DBNAME, + "USER": DBUSER, + "PASSWORD": DBPASSWD, + "HOST": "localhost", + } +} -# Internationalization -# https://docs.djangoproject.com/en/1.7/topics/i18n/ +# I18n LANGUAGE_CODE = 'fr-fr' - TIME_ZONE = 'UTC' - USE_I18N = True - USE_L10N = True - USE_TZ = True -# Static files (CSS, JavaScript, Images) -# https://docs.djangoproject.com/en/1.7/howto/static-files/ - STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), os.path.join(BASE_DIR, 'media/partitions'), diff --git a/Ernestophone/settings/local.py b/Ernestophone/settings/local.py new file mode 100644 index 0000000..08c7016 --- /dev/null +++ b/Ernestophone/settings/local.py @@ -0,0 +1,26 @@ +import os + + +from .common import * # noqa +from .common import BASE_DIR, INSTALLED_APPS, MIDDLEWARE_CLASSES + + +EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend" + +DEBUG = True + +INSTALLED_APPS += ["debug_toolbar"] +MIDDLEWARE_CLASSES = ( + ["debug_toolbar.middleware.DebugToolbarMiddleware"] + + MIDDLEWARE_CLASSES +) + +STATIC_URL = "/static/" +MEDIA_URL = "/media/" + +DATABASES = { + "default": { + "ENGINE": "django.db.backends.sqlite3", + "NAME": os.path.join(BASE_DIR, "db.sqlite3"), + } +} diff --git a/Ernestophone/settings/prod.py b/Ernestophone/settings/prod.py new file mode 100644 index 0000000..2721521 --- /dev/null +++ b/Ernestophone/settings/prod.py @@ -0,0 +1,22 @@ +import os + +from .common import * # noqa +from .common import BASE_DIR + + +DEBUG = False + +ALLOWED_HOSTS = [ + "ernestophone.ens.fr", + "www.ernestophone.ens.fr", +] + +STATIC_URL = "/static/" +STATIC_ROOT = os.path.join( + BASE_DIR, "..", "..", "public", "ernesto", "static" +) + +MEDIA_URL = "/media/" +MEDIA_ROOT = os.path.join( + BASE_DIR, "..", "media" +) diff --git a/Ernestophone/settings/secret_example.py b/Ernestophone/settings/secret_example.py new file mode 100644 index 0000000..6b633af --- /dev/null +++ b/Ernestophone/settings/secret_example.py @@ -0,0 +1,7 @@ +SECRET_KEY = "vpb%-1@$ha98w5^ji#@9v2_kxj)zdk5+e!9!fqniu2$#eg+46=" +ADMINS = None +SERVER_EMAIL = "ernesto@localhost" + +DBNAME = "ernesto" +DBUSER = "ernesto" +DBPASSWD = "YNp2rrXowJnDAFF3" diff --git a/manage.py b/manage.py index 4e11537..81b678e 100755 --- a/manage.py +++ b/manage.py @@ -3,7 +3,10 @@ import os import sys if __name__ == "__main__": - os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Ernestophone.settings") + os.environ.setdefault( + "DJANGO_SETTINGS_MODULE", + "Ernestophone.settings.local" + ) from django.core.management import execute_from_command_line diff --git a/requirements-devel.txt b/requirements-devel.txt new file mode 100644 index 0000000..5486dd6 --- /dev/null +++ b/requirements-devel.txt @@ -0,0 +1,4 @@ +-r requirements.txt + +django-debug-toolbar +ipython diff --git a/requirements.txt b/requirements.txt index 8db5ae2..310e059 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ Django==1.8.* # Pour la prod -ipython +psycopg2 gunicorn