diff --git a/kadenios/settings.py b/kadenios/settings.py deleted file mode 120000 index f1c999f..0000000 --- a/kadenios/settings.py +++ /dev/null @@ -1 +0,0 @@ -settings_dev.py \ No newline at end of file diff --git a/kadenios/settings/.gitignore b/kadenios/settings/.gitignore new file mode 100644 index 0000000..2142506 --- /dev/null +++ b/kadenios/settings/.gitignore @@ -0,0 +1 @@ +secret.py diff --git a/kadenios/settings_prod.py b/kadenios/settings/__init__.py similarity index 100% rename from kadenios/settings_prod.py rename to kadenios/settings/__init__.py diff --git a/kadenios/settings_base.py b/kadenios/settings/common.py similarity index 54% rename from kadenios/settings_base.py rename to kadenios/settings/common.py index 8a115c5..ca579f5 100644 --- a/kadenios/settings_base.py +++ b/kadenios/settings/common.py @@ -1,33 +1,49 @@ """ -Django settings for kadenios project. - -Generated by 'django-admin startproject' using Django 2.2.12. - -For more information on this file, see -https://docs.djangoproject.com/en/2.2/topics/settings/ - -For the full list of settings and their values, see -https://docs.djangoproject.com/en/2.2/ref/settings/ +Paramètres communs entre dev et prod """ import os +import sys from django.urls import reverse_lazy -# Build paths inside the project like this: os.path.join(BASE_DIR, ...) -BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +# ############################################################################# +# Secrets +# ############################################################################# + +try: + from . import secret +except ImportError: + raise ImportError( + "The secret.py file is missing.\n" + "For a development environment, simply copy secret_example.py" + ) -# Quick-start development settings - unsuitable for production -# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/ - -# SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = "f*!6tw8c74)&k_&4$toiw@e=8m00xv_(tmjf9_#wq30wg_7n^8" - -ALLOWED_HOSTS = [] +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)) -# Application definition +SECRET_KEY = import_secret("SECRET_KEY") +ADMINS = import_secret("ADMINS") +SERVER_EMAIL = import_secret("SERVER_EMAIL") +EMAIL_HOST = import_secret("EMAIL_HOST") + + +# ############################################################################# +# Paramètres par défaut pour Django +# ############################################################################# + +DEBUG = False +TESTING = len(sys.argv) > 1 and sys.argv[1] == "test" +BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) INSTALLED_APPS = [ "django.contrib.admin", @@ -70,9 +86,11 @@ TEMPLATES = [ WSGI_APPLICATION = "kadenios.wsgi.application" +DEFAULT_FROM_EMAIL = "Kadenios " -# Password validation and authentication -# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators +# ############################################################################# +# Paramètres d'authentification +# ############################################################################# AUTH_PASSWORD_VALIDATORS = [ { @@ -99,29 +117,22 @@ AUTHENTICATION_BACKENDS = [ LOGIN_URL = reverse_lazy("authens:login") LOGIN_REDIRECT_URL = "/" -AUTHENS_USE_OLDCAS = False # On n'utilise que le CAS normal pour l'instant +AUTHENS_USE_OLDCAS = False -# Internationalization -# https://docs.djangoproject.com/en/2.2/topics/i18n/ +# ############################################################################# +# Paramètres de langage +# ############################################################################# LANGUAGE_CODE = "fr-fr" - TIME_ZONE = "Europe/Paris" USE_I18N = True - USE_L10N = True - USE_TZ = True - -# Mail configuration - -DEFAULT_FROM_EMAIL = "Kadenios " - - -# Static files (CSS, JavaScript, Images) -# https://docs.djangoproject.com/en/2.2/howto/static-files/ +# ############################################################################# +# Paramètres des fichiers statiques +# ############################################################################# STATIC_URL = "/static/" STATICFILES_DIRS = [BASE_DIR + "/shared/static"] diff --git a/kadenios/settings/local.py b/kadenios/settings/local.py new file mode 100644 index 0000000..54ac597 --- /dev/null +++ b/kadenios/settings/local.py @@ -0,0 +1,55 @@ +""" +Paramètre pour le développement local +""" + +import os + +from .common import * # noqa +from .common import BASE_DIR, INSTALLED_APPS, MIDDLEWARE, TESTING + +# ############################################################################# +# Paramètres Django +# ############################################################################# + +ALLOWED_HOSTS = [] + +DEBUG = True +EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend" + +STATIC_URL = "/static/" + +DATABASES = { + "default": { + "ENGINE": "django.db.backends.sqlite3", + "NAME": os.path.join(BASE_DIR, "db.sqlite3"), + } +} + +# Use the default cache backend for local development +CACHES = {"default": {"BACKEND": "django.core.cache.backends.locmem.LocMemCache"}} + +# Pas besoin de sécurité en local +AUTH_PASSWORD_VALIDATORS = [] +PASSWORD_HASHERS = ["django.contrib.auth.hashers.MD5PasswordHasher"] + +# ############################################################################# +# Paramètres pour la Django Debug Toolbar +# ############################################################################# + + +def show_toolbar(request): + """ + On active la debug-toolbar en mode développement local sauf : + - dans l'admin où ça ne sert pas à grand chose; + - si la variable d'environnement DJANGO_NO_DDT est à 1 → ça permet de la désactiver + sans modifier ce fichier en exécutant `export DJANGO_NO_DDT=1` dans le terminal + qui lance `./manage.py runserver`. + """ + env_no_ddt = bool(os.environ.get("DJANGO_NO_DDT", None)) + return DEBUG and not env_no_ddt and not request.path.startswith("/admin/") + + +if not TESTING: + INSTALLED_APPS = INSTALLED_APPS + ["debug_toolbar"] + MIDDLEWARE = ["debug_toolbar.middleware.DebugToolbarMiddleware"] + MIDDLEWARE + DEBUG_TOOLBAR_CONFIG = {"SHOW_TOOLBAR_CALLBACK": show_toolbar} diff --git a/kadenios/settings/prod.py b/kadenios/settings/prod.py new file mode 100644 index 0000000..d463e6c --- /dev/null +++ b/kadenios/settings/prod.py @@ -0,0 +1,58 @@ +""" +Paramètres pour la mise en production +""" + +import os + +from .common import * # noqa +from .common import BASE_DIR, import_secret + +# ############################################################################# +# Secrets de production +# ############################################################################# + +REDIS_PASSWD = import_secret("REDIS_PASSWD") +REDIS_DB = import_secret("REDIS_DB") +REDIS_HOST = import_secret("REDIS_HOST") +REDIS_PORT = import_secret("REDIS_PORT") + +DBNAME = import_secret("DBNAME") +DBUSER = import_secret("DBUSER") +DBPASSWD = import_secret("DBPASSWD") + +# ############################################################################# +# À modifier possiblement lors de la mise en production +# ############################################################################# + +ALLOWED_HOSTS = ["kadenios.eleves.ens.fr", "www.kadenios.eleves.ens.fr"] + +STATIC_ROOT = os.path.join( + os.path.dirname(os.path.dirname(BASE_DIR)), "public", "kadenios", "static" +) + +# ############################################################################# +# Paramètres du cache +# ############################################################################# + +CACHES = { + "default": { + "BACKEND": "redis_cache.RedisCache", + "LOCATION": "redis://:{passwd}@{host}:{port}/{db}".format( + passwd=REDIS_PASSWD, host=REDIS_HOST, port=REDIS_PORT, db=REDIS_DB + ), + } +} + +# ############################################################################# +# Paramètres de la base de données +# ############################################################################# + +DATABASES = { + "default": { + "ENGINE": "django.db.backends.postgresql_psycopg2", + "NAME": DBNAME, + "USER": DBUSER, + "PASSWORD": DBPASSWD, + "HOST": os.environ.get("DBHOST", "localhost"), + } +} diff --git a/kadenios/settings/secret_example.py b/kadenios/settings/secret_example.py new file mode 100644 index 0000000..e61fe2a --- /dev/null +++ b/kadenios/settings/secret_example.py @@ -0,0 +1,14 @@ +SECRET_KEY = "f*!6tw8c74)&k_&4$toiw@e=8m00xv_(tmjf9_#wq30wg_7n^8" +ADMINS = None +SERVER_EMAIL = "root@localhost" +EMAIL_HOST = None + + +DBUSER = "kadenios" +DBNAME = "kadenios" +DBPASSWD = "O1LxCADDA6Px5SiKvifjvdp3DSjfbp" + +REDIS_PASSWD = "dummy" +REDIS_PORT = 6379 +REDIS_DB = 0 +REDIS_HOST = "127.0.0.1" diff --git a/kadenios/settings_dev.py b/kadenios/settings_dev.py deleted file mode 100644 index 06accf6..0000000 --- a/kadenios/settings_dev.py +++ /dev/null @@ -1,32 +0,0 @@ -import os - -from .settings_base import * # noqa -from .settings_base import BASE_DIR, INSTALLED_APPS, MIDDLEWARE - -ALLOWED_HOSTS = [] - -DEBUG = True -EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend" - -INSTALLED_APPS += [ - "debug_toolbar", -] - -MIDDLEWARE += [ - "debug_toolbar.middleware.DebugToolbarMiddleware", -] - -# Database -# https://docs.djangoproject.com/en/2.2/ref/settings/#databases - -DATABASES = { - "default": { - "ENGINE": "django.db.backends.sqlite3", - "NAME": os.path.join(BASE_DIR, "db.sqlite3"), - } -} - -INTERNAL_IPS = [ - "127.0.0.1", - "localhost", -] diff --git a/manage.py b/manage.py index 7f9ab98..cc4c0ec 100755 --- a/manage.py +++ b/manage.py @@ -5,7 +5,7 @@ import sys def main(): - os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'kadenios.settings') + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "kadenios.settings.local") try: from django.core.management import execute_from_command_line except ImportError as exc: @@ -17,5 +17,5 @@ def main(): execute_from_command_line(sys.argv) -if __name__ == '__main__': +if __name__ == "__main__": main()