From 0209ad53ca62f47408e0721eab22485cf8816068 Mon Sep 17 00:00:00 2001 From: Tom Hubrecht Date: Wed, 23 Oct 2024 20:04:53 +0200 Subject: [PATCH] feat: Merge settings --- app/settings.py | 242 +++++++++++++++++++++++++++++++++++++++++++ app/settings_base.py | 143 ------------------------- app/settings_dev.py | 45 -------- app/settings_prod.py | 49 --------- 4 files changed, 242 insertions(+), 237 deletions(-) create mode 100644 app/settings.py delete mode 100644 app/settings_base.py delete mode 100644 app/settings_dev.py delete mode 100644 app/settings_prod.py diff --git a/app/settings.py b/app/settings.py new file mode 100644 index 0000000..10cc1b9 --- /dev/null +++ b/app/settings.py @@ -0,0 +1,242 @@ +""" +Django settings for the experiENS project +""" + +from pathlib import Path + +from loadcredential import Credentials + +from django.urls import reverse_lazy +from django.utils.translation import gettext_lazy as _ + +credentials = Credentials(env_prefix="EXPERIENS_") + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + +# WARNING: keep the secret key used in production secret! +SECRET_KEY = credentials["SECRET_KEY"] + +# WARNING: don't run with debug turned on in production! +DEBUG = credentials.get_json("DEBUG", False) + +ALLOWED_HOSTS = credentials.get_json("ALLOWED_HOSTS", []) + +ADMINS = credentials.get_json("ADMINS", []) + +SITE_ID = 1 + + +### +# ElasticSearch configuration + +USE_ELASTICSEARCH = credentials.get_json("USE_ELASTICSEARCH", False) +ELASTICSEARCH_DSL = credentials.get_json( + "ELASTICSEARCH_DSL", {"default": {"hosts": "127.0.0.1:9200"}} +) + + +### +# Libraries configuration + +GDAL_LIBRARY_PATH = credentials.get("GDAL_LIBRARY_PATH") +GEOS_LIBRARY_PATH = credentials.get("GEOS_LIBRARY_PATH") + + +### +# List the installed applications + +INSTALLED_APPS = [ + "django.contrib.admin", + "django.contrib.auth", + "django.contrib.contenttypes", + "django.contrib.sessions", + "django.contrib.messages", + "django.contrib.staticfiles", + "django.contrib.gis", + "django.contrib.sites", + *(["django_elasticsearch_dsl"] if USE_ELASTICSEARCH else []), + "simple_email_confirmation", + "authens", + "tastypie", + "braces", + "tinymce", + "taggit", + "taggit_autosuggest", + "avisstage", +] + + +### +# List the installed middlewares + +MIDDLEWARE = [ + "django.middleware.security.SecurityMiddleware", + "django.contrib.sessions.middleware.SessionMiddleware", + "django.middleware.common.CommonMiddleware", + "django.middleware.csrf.CsrfViewMiddleware", + "django.contrib.auth.middleware.AuthenticationMiddleware", + "django.contrib.messages.middleware.MessageMiddleware", + "django.middleware.clickjacking.XFrameOptionsMiddleware", +] + + +### +# The main url configuration + +ROOT_URLCONF = "app.urls" + + +### +# Template configuration: +# - Django Templating Language is used +# - Application directories can be used + + +TEMPLATES = [ + { + "BACKEND": "django.template.backends.django.DjangoTemplates", + "DIRS": [], + "APP_DIRS": True, + "OPTIONS": { + "context_processors": [ + "django.contrib.auth.context_processors.auth", + "django.template.context_processors.debug", + "django.template.context_processors.i18n", + "django.template.context_processors.media", + "django.template.context_processors.static", + "django.template.context_processors.tz", + "django.template.context_processors.request", + "django.contrib.messages.context_processors.messages", + ], + }, + }, +] + + +### +# Database configuration +# -> https://docs.djangoproject.com/en/4.2/ref/settings/#databases + +DEFAULT_AUTO_FIELD = "django.db.models.AutoField" + +DATABASES = credentials.get_json( + "DATABASES", + { + "default": { + "ENGINE": "django.contrib.gis.db.backends.spatialite", + "NAME": BASE_DIR / "db.sqlite3", + } + }, +) + +CACHES = credentials.get_json( + "CACHES", + default={ + "default": { + "BACKEND": "django.core.cache.backends.locmem.LocMemCache", + }, + }, +) + + +### +# WSGI application configuration + +WSGI_APPLICATION = "app.wsgi.application" + + +### +# Staticfiles configuration + +STATIC_ROOT = credentials["STATIC_ROOT"] +STATIC_URL = "/static/" + +MEDIA_ROOT = credentials.get("MEDIA_ROOT", BASE_DIR / "media") +MEDIA_URL = "/media/" + + +### +# Internationalization configuration +# -> https://docs.djangoproject.com/en/4.2/topics/i18n/ + +LANGUAGE_CODE = "fr-fr" +TIME_ZONE = "Europe/Paris" + +USE_I18N = True +USE_L10N = True +USE_TZ = True + +LANGUAGES = [ + ("fr", _("Français")), +] + + +### +# Authentication configuration + +AUTHENTICATION_BACKENDS = [ + "django.contrib.auth.backends.ModelBackend", + "experiENS.auth.ENSCASBackend", +] + +CAS_SERVER_URL = "https://cas.eleves.ens.fr/" # SPI CAS + +AUTHENS_USE_OLDCAS = False + +LOGIN_URL = reverse_lazy("authens:login") +LOGOUT_URL = reverse_lazy("authens:logout") +LOGIN_REDIRECT_URL = reverse_lazy("avisstage:perso") +LOGOUT_REDIRECT_URL = reverse_lazy("avisstage:index") + + +AUTH_PASSWORD_VALIDATORS = [ + {"NAME": f"django.contrib.auth.password_validation.{v}"} + for v in [ + "UserAttributeSimilarityValidator", + "MinimumLengthValidator", + "CommonPasswordValidator", + "NumericPasswordValidator", + ] +] + + +### +# Logging configuration + +LOGGING = { + "version": 1, + "disable_existing_loggers": False, + "handlers": { + "file": { + "level": "INFO", + "class": "logging.FileHandler", + "filename": credentials.get( + "RECHERCHE_LOG_FILE", BASE_DIR / "recherche.log" + ), + }, + }, + "loggers": { + "recherche": { + "handlers": ["file"], + "level": "INFO", + "propagate": True, + }, + }, +} + + +### +# LDAP configuration + +CLIPPER_LDAP_SERVER = credentials.get("CLIPPER_LDAP_SERVER", "ldaps://localhost:636") + + +# Development settings +if DEBUG: + EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend" + INSTALLED_APPS += [ + "debug_toolbar", + ] + + MIDDLEWARE = ["debug_toolbar.middleware.DebugToolbarMiddleware", *MIDDLEWARE] diff --git a/app/settings_base.py b/app/settings_base.py deleted file mode 100644 index 2aa9fee..0000000 --- a/app/settings_base.py +++ /dev/null @@ -1,143 +0,0 @@ -""" -Django settings for experiENS 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 - -from django.urls import reverse_lazy - -from .secrets import GOOGLE_API_KEY, MAPBOX_API_KEY, SECRET_KEY # noqa - -BASE_DIR = os.path.dirname(os.path.dirname(__file__)) - -# Quick-start development settings - unsuitable for production -# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/ - -ALLOWED_HOSTS = [] - -# Application definition - -INSTALLED_APPS = [ - "django.contrib.admin", - "django.contrib.auth", - "django.contrib.contenttypes", - "django.contrib.sessions", - "django.contrib.messages", - "django.contrib.staticfiles", - "django.contrib.gis", - "django.contrib.sites", - "django_elasticsearch_dsl", - # 'allauth', # Uncomment that part when you - # 'allauth.account', # apply migration - # 'allauth.socialaccount', # Allauth -> AuthENS - "simple_email_confirmation", - "authens", - "tastypie", - "braces", - "tinymce", - "taggit", - "taggit_autosuggest", - "avisstage", -] - -MIDDLEWARE = ( - "django.middleware.security.SecurityMiddleware", - "django.contrib.sessions.middleware.SessionMiddleware", - "django.middleware.common.CommonMiddleware", - "django.middleware.csrf.CsrfViewMiddleware", - "django.contrib.auth.middleware.AuthenticationMiddleware", - "django.contrib.messages.middleware.MessageMiddleware", - "django.middleware.clickjacking.XFrameOptionsMiddleware", -) - - -TEMPLATES = [ - { - "BACKEND": "django.template.backends.django.DjangoTemplates", - "DIRS": [ - # insert your TEMPLATE_DIRS here - ], - "APP_DIRS": True, - "OPTIONS": { - "context_processors": [ - "django.contrib.auth.context_processors.auth", - "django.template.context_processors.debug", - "django.template.context_processors.i18n", - "django.template.context_processors.media", - "django.template.context_processors.static", - "django.template.context_processors.tz", - "django.template.context_processors.request", - "django.contrib.messages.context_processors.messages", - ], - }, - }, -] - - -ROOT_URLCONF = "experiENS.urls" - -WSGI_APPLICATION = "experiENS.wsgi.application" - - -# Database -# https://docs.djangoproject.com/en/1.7/ref/settings/#databases - -# Internationalization -# https://docs.djangoproject.com/en/1.7/topics/i18n/ - -LANGUAGE_CODE = "fr" - -TIME_ZONE = "Europe/Paris" - -USE_I18N = True - -USE_L10N = True - -USE_TZ = True - -SITE_ID = 1 - -# Static files (CSS, JavaScript, Images) -# https://docs.djangoproject.com/en/1.7/howto/static-files/ - -STATIC_URL = "/static/" - -AUTHENTICATION_BACKENDS = ( - "django.contrib.auth.backends.ModelBackend", - "experiENS.auth.ENSCASBackend", -) - -CAS_SERVER_URL = "https://cas.eleves.ens.fr/" # SPI CAS - -AUTHENS_USE_OLDCAS = False - -LOGIN_URL = reverse_lazy("authens:login") -LOGOUT_URL = reverse_lazy("authens:logout") -LOGIN_REDIRECT_URL = reverse_lazy("avisstage:perso") -LOGOUT_REDIRECT_URL = reverse_lazy("avisstage:index") - -LOGGING = { - "version": 1, - "disable_existing_loggers": False, - "handlers": { - "file": { - "level": "INFO", - "class": "logging.FileHandler", - "filename": os.path.join(BASE_DIR, "recherche.log"), - }, - }, - "loggers": { - "recherche": { - "handlers": ["file"], - "level": "INFO", - "propagate": True, - }, - }, -} diff --git a/app/settings_dev.py b/app/settings_dev.py deleted file mode 100644 index e1fa2a4..0000000 --- a/app/settings_dev.py +++ /dev/null @@ -1,45 +0,0 @@ -import os - -from .settings_base import * # noqa -from .settings_base import BASE_DIR, INSTALLED_APPS, MIDDLEWARE - -DEBUG = True - -DATABASES = { - "default": { - "ENGINE": "django.contrib.gis.db.backends.spatialite", - "NAME": os.path.join(BASE_DIR, "db.sqlite3"), - } -} - -USE_DEBUG_TOOLBAR = False - -if USE_DEBUG_TOOLBAR: - INSTALLED_APPS += [ - "debug_toolbar", - ] - - MIDDLEWARE = ("debug_toolbar.middleware.DebugToolbarMiddleware",) + MIDDLEWARE - -INTERNAL_IPS = ["127.0.0.1"] - -SPATIALITE_LIBRARY_PATH = "mod_spatialite" - -STATIC_ROOT = "/home/evarin/Bureau/experiENS/static/" - -EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend" - -STATIC_URL = "/experiens/static/" - -ELASTICSEARCH_DSL = { - "default": {"hosts": "localhost:9200"}, -} - - -CLIPPER_LDAP_SERVER = "ldaps://localhost:636" - -# Changer à True pour développer avec ES -USE_ELASTICSEARCH = False - -if not USE_ELASTICSEARCH: - INSTALLED_APPS.remove("django_elasticsearch_dsl") diff --git a/app/settings_prod.py b/app/settings_prod.py deleted file mode 100644 index 721d2bf..0000000 --- a/app/settings_prod.py +++ /dev/null @@ -1,49 +0,0 @@ -import os - -from .settings_base import * # noqa - -PROJECT_DIR = os.path.dirname(os.path.abspath(__file__)) -BASE_DIR = os.path.dirname(PROJECT_DIR) - -DEBUG = False - -ALLOWED_HOSTS = ["www.eleves.ens.fr"] - -ADMINS = (("Robin Champenois", "champeno@clipper.ens.fr"),) - -ADMIN_LOGINS = [ - "champeno", -] - -SERVER_EMAIL = "experiens@www.eleves.ens.fr" - -ROOT_URL = "/experiens/" - -WSGI_APPLICATION = "experiENS.wsgi.application" - -DATABASES = { - "default": { - "ENGINE": "django.contrib.gis.db.backends.postgis", - "NAME": "experiens", - "USER": "experiens", - "PASSWORD": "", - "HOST": "", - "PORT": "5432", - } -} - -STATIC_URL = ROOT_URL + "static/" -MEDIA_URL = ROOT_URL + "media/" - -STATIC_ROOT = os.path.join(BASE_DIR, "static/") - -EMAIL_HOST = "nef.ens.fr" - - -ELASTICSEARCH_DSL = { - "default": {"hosts": "127.0.0.1:9200"}, -} - - -CLIPPER_LDAP_SERVER = "ldaps://ldap.spi.ens.fr:636" -DEFAULT_FROM_EMAIL = "experiens-no-reply@www.eleves.ens.fr"