Restructure of settings.py

This commit is contained in:
Corban-Lee Jones 2024-01-18 12:16:07 +00:00
parent 067bc744d7
commit e5ffbc9005

View File

@ -5,33 +5,22 @@ from pathlib import Path
from django.utils import timezone
now = timezone.now()
env = environ.Env(
# set casting, default value
DEBUG=(bool, True)
)
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
# BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# BASE_DIR is the root of the project, all paths should be constructed from it using pathlib
BASE_DIR = Path(__file__).parent.parent
CORE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print(BASE_DIR, CORE_DIR)
# Take environment variables from .env file
# Create an environment and read variables from .env file
env = environ.Env(DEBUG=(bool, True))
environ.Env.read_env(BASE_DIR / ".env")
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env('SECRET_KEY', default='S#perS3crEt_007')
# SECURITY WARNING: This is sensitive data, keep secure!
SECRET_KEY = env('SECRET_KEY', default="unsecure-default-secret-key")
# SECURITY WARNING: don't run with debug turned on in production!
# SECURITY WARNING: Must be 'False' in production!
DEBUG = env('DEBUG')
# Assets Management
ASSETS_ROOT = os.getenv('ASSETS_ROOT', '/static/assets')
# load production server from .env
ALLOWED_HOSTS = ['localhost', 'localhost:85', '127.0.0.1', '192.168.0.19', env('SERVER', default='127.0.0.1') ]
CSRF_TRUSTED_ORIGINS = ['http://localhost:85', 'http://127.0.0.1', 'https://' + env('SERVER', default='127.0.0.1') ]
# Hosts and Origins that the server host must be within.
ALLOWED_HOSTS = ["localhost", "127.0.0.1", env("HOST", default="127.0.0.1")]
CSRF_TRUSTED_ORIGINS = ["http://localhost", "http://127.0.0.1", "https://" + env("HOST", default="127.0.0.1")]
# Application definition
@ -62,14 +51,18 @@ MIDDLEWARE = [
]
ROOT_URLCONF = 'core.urls'
LOGIN_REDIRECT_URL = "home" # Route defined in home/urls.py
LOGOUT_REDIRECT_URL = "home" # Route defined in home/urls.py
TEMPLATE_DIR = os.path.join(CORE_DIR, "apps/templates") # ROOT dir for templates
APPEND_SLASH = True
LOGIN_URL = "/login/"
LOGIN_REDIRECT_URL = "/"
LOGOUT_REDIRECT_URL = "/"
AUTH_USER_MODEL = "authentication.User"
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_DIR],
'DIRS': [BASE_DIR / "apps/templates"],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
@ -77,7 +70,6 @@ TEMPLATES = [
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'apps.context_processors.cfg_assets_root',
],
},
},
@ -85,8 +77,9 @@ TEMPLATES = [
WSGI_APPLICATION = 'core.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases
if os.environ.get('DB_ENGINE') and os.environ.get('DB_ENGINE') == "mysql":
DATABASES = {
@ -108,7 +101,7 @@ else:
}
# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
@ -125,7 +118,8 @@ AUTH_PASSWORD_VALIDATORS = [
},
]
# Loggiong
# Logging
# https://docs.djangoproject.com/en/5.0/topics/logging/
LOGGING_DIR = BASE_DIR / "logs"
LOGGING_DIR.mkdir(exist_ok=True)
@ -137,7 +131,7 @@ LOGGING = {
'file': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'filename': LOGGING_DIR / f'{now}.log',
'filename': LOGGING_DIR / f'{timezone.now()}.log',
"formatter": "verbose",
},
'console': {
@ -181,8 +175,9 @@ LOGGING = {
}
}
# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/
# https://docs.djangoproject.com/en/5.0/topics/i18n/
LANGUAGE_CODE = 'en-gb'
@ -190,29 +185,26 @@ TIME_ZONE = 'Europe/London'
USE_I18N = True
USE_L10N = True
USE_TZ = True
#############################################################
# SRC: https://devcenter.heroku.com/articles/django-assets
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
STATIC_ROOT = os.path.join(CORE_DIR, 'staticfiles')
# https://docs.djangoproject.com/en/5.0/howto/static-files/
STATIC_ROOT = BASE_DIR / 'staticfiles'
STATIC_URL = '/static/'
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
os.path.join(CORE_DIR, 'apps/static'),
BASE_DIR / 'apps/static',
)
# Media Files
MEDIA_ROOT = os.path.join(CORE_DIR, 'media')
MEDIA_ROOT = BASE_DIR / 'media'
MEDIA_URL = '/media/'
#############################################################
#############################################################
# Django Rest Framework
# https://www.django-rest-framework.org/
REST_FRAMEWORK = {
'DEFAULT_THROTTLE_CLASSES': [
@ -224,13 +216,3 @@ REST_FRAMEWORK = {
'user': '1000/day'
}
}
#############################################################
#############################################################
# If route isnt found, try again with appended slash
APPEND_SLASH = True
AUTH_USER_MODEL = "authentication.User"
LOGIN_URL = "/login/"