Commit a4d682fe authored by Florent Chehab's avatar Florent Chehab

fix(django settings): cleaned and separated

* Cleaned main django settings file
* Added an app specific settings file

Fixes #90
parent 210966b7
Pipeline #37114 passed with stages
in 5 minutes and 25 seconds
# Is moderation activated for models marked with 1 as a moderation level
MODERATION_ACTIVATED = True
# File extensions recognized as being pictures
ALLOWED_PHOTOS_EXTENSION = ["jpg", "jpeg", "png", "svg"]
# Configuration of the CAS server
CAS_SERVER_URL = "https://cas.utc.fr/cas/"
CAS_APPLY_ATTRIBUTES_TO_USER = True # We want to map the attribute returned by the cas to the user model attribute
CAS_RENAME_ATTRIBUTES = {"mail": "email", "givenName": "first_name", "sn": "last_name"} # Mapping of the attribute
......@@ -10,15 +10,48 @@ https://docs.djangoproject.com/en/2.0/ref/settings/
import os
from os.path import dirname, normpath, join
from .app_settings import * # noqa: F403, F401 # We need to load app specific settings
import sys
# Build paths inside the project like this: join(BASE_DIR, ...)
# Is the root of the REPOSITORY
BASE_DIR = dirname(dirname(dirname(os.path.abspath(__file__))))
####################################
#
# Directory locations
#
####################################
BACKEND_ROOT_DIR = dirname(dirname(dirname(os.path.abspath(__file__))))
REPO_ROOT_DIR = dirname(BACKEND_ROOT_DIR)
#
#
#####################################
#
# Django essential settings
#
#####################################
SECRET_KEY = os.environ["SECRET_KEY"]
# Application definition
ROOT_URLCONF = "base_app.urls"
WSGI_APPLICATION = "base_app.wsgi.application"
STATIC_URL = "/static/"
STATIC_ROOT = normpath(join(BACKEND_ROOT_DIR, "static"))
MEDIA_ROOT = normpath(join(BACKEND_ROOT_DIR, "media"))
MEDIA_URL = "/media/"
#
#
#####################################
#
# Sub-applications in the app
#
#####################################
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
......@@ -36,6 +69,51 @@ INSTALLED_APPS = [
"webpack_loader",
]
#
#
#####################################
#
# Rest framework configuration
#
#####################################
REST_FRAMEWORK = {
# Use Django's standard `django.contrib.auth` permissions,
"DEFAULT_PERMISSION_CLASSES": (
"rest_framework.permissions.DjangoModelPermissions",
),
"DEFAULT_AUTHENTICATION_CLASSES": (
"rest_framework.authentication.SessionAuthentication",
"rest_framework.authentication.TokenAuthentication",
),
}
#
#
#####################################
#
# Webpack loader config to enable frontend hot module replacement
#
#####################################
WEBPACK_LOADER = {
"DEFAULT": {
"BUNDLE_DIR_NAME": "base_app/bundles/",
"STATS_FILE": join(REPO_ROOT_DIR, "frontend/webpack-stats.json"),
}
}
#
#
#####################################
#
# Middlewares applied to the request
#
#####################################
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
......@@ -46,7 +124,15 @@ MIDDLEWARE = [
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"base_app.middleware.LoginRequiredMiddleware",
]
MODERATION_ACTIVATED = True
#
#
#####################################
#
# Big variables settings
#
#####################################
if os.environ["ENV"] == "DEV":
DEBUG = True
......@@ -54,57 +140,60 @@ if os.environ["ENV"] == "DEV":
INSTALLED_APPS += ["django_extensions", "debug_toolbar"]
MIDDLEWARE = ["debug_toolbar.middleware.DebugToolbarMiddleware"] + MIDDLEWARE
# Force the debug toolbar to be shown
DEBUG_TOOLBAR_CONFIG = {"SHOW_TOOLBAR_CALLBACK": "base_app.utils.show_toolbar"}
else:
DEBUG = False
TESTING = "pytest" in sys.modules
# Use a custom User model for optmization
AUTH_USER_MODEL = "base_app.User"
WEBPACK_LOADER = {
"DEFAULT": {
"BUNDLE_DIR_NAME": "base_app/bundles/",
"STATS_FILE": join(BASE_DIR, "frontend/webpack-stats.json"),
}
}
#
#
#####################################
#
# Authentication related
#
#####################################
ALLOWED_PHOTOS_EXTENSION = ["jpg", "jpeg", "png", "svg"]
LOGIN_URL = "/user/login"
LOGIN_EXEMPT_URLS = [LOGIN_URL, "/admin/"]
# Use a custom User model for optimization
AUTH_USER_MODEL = "base_app.User"
AUTHENTICATION_BACKENDS = [
"django.contrib.auth.backends.ModelBackend",
"django_cas_ng.backends.CASBackend",
]
CAS_SERVER_URL = "https://cas.utc.fr/cas/"
CAS_APPLY_ATTRIBUTES_TO_USER = True
CAS_RENAME_ATTRIBUTES = {"mail": "email", "givenName": "first_name", "sn": "last_name"}
# Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators
TESTING = "pytest" in sys.modules
if TESTING:
LOGIN_EXEMPT_URLS.append("api")
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"},
]
LOGIN_URL = "/user/login"
LOGIN_EXEMPT_URLS = [LOGIN_URL, "/admin/"]
REST_FRAMEWORK = {
# Use Django's standard `django.contrib.auth` permissions,
"DEFAULT_PERMISSION_CLASSES": (
"rest_framework.permissions.DjangoModelPermissions",
),
"DEFAULT_AUTHENTICATION_CLASSES": (
"rest_framework.authentication.SessionAuthentication",
"rest_framework.authentication.TokenAuthentication",
),
}
if TESTING:
# In a testing environment we want to by pass the loginRequiredMiddleware
LOGIN_EXEMPT_URLS.append("api")
SESSION_EXPIRE_AT_BROWSER_CLOSE = True
ROOT_URLCONF = "base_app.urls"
#
#
#####################################
#
# Django Templating settings
#
#####################################
TEMPLATES = [
{
......@@ -122,10 +211,14 @@ TEMPLATES = [
}
]
WSGI_APPLICATION = "base_app.wsgi.application"
#
#
#####################################
#
# Database configuration
#
#####################################
# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
DATABASES = {
......@@ -139,31 +232,23 @@ DATABASES = {
}
}
# To use Sqlite, uncomment below
# PROJECT_DIR = os.path.abspath(os.path.dirname(__file__))
# To use Sqlite, uncomment below and comment above
# DATABASES = {
# "default": {
# "ENGINE": "django.db.backends.sqlite3",
# "NAME": join(PROJECT_DIR, "../database.db"),
# "NAME": join(BACKEND_ROOT_DIR, "./database.db"),
# }
# }
# Password validation
# https://docs.djangoproject.com/en/2.0/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
#
#####################################
# Internationalization
# https://docs.djangoproject.com/en/2.0/topics/i18n/
LANGUAGE_CODE = "fr-fr"
......@@ -171,9 +256,3 @@ TIME_ZONE = "Europe/Paris"
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_URL = "/static/"
STATIC_ROOT = normpath(join(BASE_DIR, "static"))
MEDIA_ROOT = normpath(join(BASE_DIR, "media"))
MEDIA_URL = "/media/"
......@@ -3,7 +3,7 @@ import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "base_app.settings")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "base_app.settings.main")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
......
[pytest]
DJANGO_SETTINGS_MODULE = base_app.settings
DJANGO_SETTINGS_MODULE = base_app.settings.main
python_files = tests.py test_*.py *_tests.py
addopts = --cov --cov-config .coveragerc --cov-report term
......@@ -8,16 +8,17 @@ There exist different kinds of *config files* throughout the app and all of them
## Django config files
Django's main config file in `backend/base_app/settings.py`. This file is a standard Django one and defines many important aspects of the app such as:
Django's main config file in `backend/base_app/settings/main.py`. This file is a standard Django one and defines many important aspects of the app such as:
- Which packages should be bundled with the app,
- What Middleware to use,
- Custom behaviors depending on if we are in testing, developing or production environment,
- Config to serve the frontend files from the backend,
- App specific settings,
- Database settings,
- Etc.
Another config file is loaded through the previous one: `backend/base_app/settings/app_settings.py`. It contains app specific settings such as the `CAS` configuration and how moderation is applied.
## Custom config files
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment