Refactor the initial setting bootstrap code
Signed-off-by: Roberto Rosario <roberto.rosario@mayan-edms.com>
This commit is contained in:
+26
-100
@@ -1,14 +1,3 @@
|
||||
"""
|
||||
Django settings for mayan10 project.
|
||||
|
||||
Generated by 'django-admin startproject' using Django 1.10.4.
|
||||
|
||||
For more information on this file, see
|
||||
https://docs.djangoproject.com/en/1.10/topics/settings/
|
||||
|
||||
For the full list of settings and their values, see
|
||||
https://docs.djangoproject.com/en/1.10/ref/settings/
|
||||
"""
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import os
|
||||
@@ -17,15 +6,9 @@ import sys
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from mayan.apps.smart_settings.literals import BOOTSTRAP_SETTING_LIST
|
||||
from mayan.apps.smart_settings.utils import (
|
||||
get_environment_setting, get_environment_variables, read_configuration_file
|
||||
)
|
||||
from mayan.apps.smart_settings.utils import SettingNamespaceSingleton
|
||||
|
||||
from .literals import (
|
||||
CONFIGURATION_FILENAME, CONFIGURATION_LAST_GOOD_FILENAME,
|
||||
DEFAULT_SECRET_KEY, SECRET_KEY_FILENAME, SYSTEM_DIR
|
||||
)
|
||||
from .literals import DEFAULT_SECRET_KEY, SECRET_KEY_FILENAME, SYSTEM_DIR
|
||||
|
||||
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
|
||||
|
||||
@@ -34,9 +17,17 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
# Quick-start development settings - unsuitable for production
|
||||
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/
|
||||
|
||||
MEDIA_ROOT = get_environment_setting(
|
||||
name='MEDIA_ROOT', fallback_default=os.path.join(BASE_DIR, 'media')
|
||||
)
|
||||
setting_namespace = SettingNamespaceSingleton(global_symbol_table=globals())
|
||||
if 'revertsettings' in sys.argv:
|
||||
setting_namespace.update_globals(only_critical=True)
|
||||
DATABASES = {
|
||||
'default': {
|
||||
'ENGINE': 'django.db.backends.sqlite3',
|
||||
'NAME': os.path.join(MEDIA_ROOT, 'db.sqlite3'), # NOQA: F821
|
||||
}
|
||||
}
|
||||
else:
|
||||
setting_namespace.update_globals()
|
||||
|
||||
# SECURITY WARNING: keep the secret key used in production secret!
|
||||
environment_secret_key = os.environ.get('MAYAN_SECRET_KEY')
|
||||
@@ -44,15 +35,12 @@ if environment_secret_key:
|
||||
SECRET_KEY = environment_secret_key
|
||||
else:
|
||||
try:
|
||||
with open(os.path.join(MEDIA_ROOT, SYSTEM_DIR, SECRET_KEY_FILENAME)) as file_object:
|
||||
with open(os.path.join(MEDIA_ROOT, SYSTEM_DIR, SECRET_KEY_FILENAME)) as file_object: # NOQA: F821
|
||||
SECRET_KEY = file_object.read().strip()
|
||||
except IOError:
|
||||
SECRET_KEY = DEFAULT_SECRET_KEY
|
||||
|
||||
# SECURITY WARNING: don't run with debug turned on in production!
|
||||
DEBUG = get_environment_setting(name='DEBUG')
|
||||
|
||||
ALLOWED_HOSTS = get_environment_setting(name='ALLOWED_HOSTS')
|
||||
|
||||
# Application definition
|
||||
|
||||
@@ -243,7 +231,7 @@ LANGUAGES = (
|
||||
SITE_ID = 1
|
||||
|
||||
STATIC_ROOT = os.environ.get(
|
||||
'MAYAN_STATIC_ROOT', os.path.join(MEDIA_ROOT, 'static')
|
||||
'MAYAN_STATIC_ROOT', os.path.join(MEDIA_ROOT, 'static') # NOQA: F821
|
||||
)
|
||||
|
||||
STATICFILES_FINDERS = (
|
||||
@@ -255,13 +243,6 @@ STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
|
||||
|
||||
TEST_RUNNER = 'mayan.apps.common.tests.runner.MayanTestRunner'
|
||||
|
||||
# --------- Django -------------------
|
||||
|
||||
LOGIN_URL = get_environment_setting(name='LOGIN_URL')
|
||||
LOGIN_REDIRECT_URL = get_environment_setting(name='LOGIN_REDIRECT_URL')
|
||||
LOGOUT_REDIRECT_URL = get_environment_setting(name='LOGOUT_REDIRECT_URL')
|
||||
INTERNAL_IPS = get_environment_setting(name='INTERNAL_IPS')
|
||||
|
||||
# ---------- Django REST framework -----------
|
||||
|
||||
REST_FRAMEWORK = {
|
||||
@@ -283,12 +264,6 @@ PAGINATION_SETTINGS = {
|
||||
|
||||
# ----------- Celery ----------
|
||||
|
||||
CELERY_BROKER_URL = get_environment_setting(name='CELERY_BROKER_URL')
|
||||
CELERY_RESULT_BACKEND = get_environment_setting(name='CELERY_RESULT_BACKEND')
|
||||
CELERY_TASK_ALWAYS_EAGER = get_environment_setting(
|
||||
name='CELERY_TASK_ALWAYS_EAGER'
|
||||
)
|
||||
|
||||
CELERY_ACCEPT_CONTENT = ('json',)
|
||||
CELERY_BEAT_SCHEDULE = {}
|
||||
CELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers.DatabaseScheduler'
|
||||
@@ -329,54 +304,6 @@ SWAGGER_SETTINGS = {
|
||||
AJAX_REDIRECT_CODE = 278
|
||||
|
||||
BASE_INSTALLED_APPS = INSTALLED_APPS
|
||||
COMMON_EXTRA_APPS = ()
|
||||
COMMON_DISABLED_APPS = ()
|
||||
|
||||
CONFIGURATION_FILEPATH = get_environment_setting(
|
||||
name='CONFIGURATION_FILEPATH', fallback_default=os.path.join(
|
||||
MEDIA_ROOT, CONFIGURATION_FILENAME
|
||||
)
|
||||
)
|
||||
|
||||
CONFIGURATION_LAST_GOOD_FILEPATH = get_environment_setting(
|
||||
name='CONFIGURATION_LAST_GOOD_FILEPATH', fallback_default=os.path.join(
|
||||
MEDIA_ROOT, CONFIGURATION_LAST_GOOD_FILENAME
|
||||
)
|
||||
)
|
||||
|
||||
if 'revertsettings' not in sys.argv:
|
||||
configuration_result = read_configuration_file(CONFIGURATION_FILEPATH)
|
||||
environment_result = get_environment_variables()
|
||||
|
||||
for setting in BOOTSTRAP_SETTING_LIST:
|
||||
if setting['name'] in configuration_result:
|
||||
globals().update(
|
||||
{setting['name']: configuration_result[setting['name']]}
|
||||
)
|
||||
elif setting['name'] in environment_result:
|
||||
globals().update(
|
||||
{setting['name']: environment_result[setting['name']]}
|
||||
)
|
||||
else:
|
||||
# Apply the default only if it has not yet been defined previously
|
||||
# in this module.
|
||||
if setting['name'] not in globals():
|
||||
globals().update(
|
||||
{
|
||||
setting['name']: get_environment_setting(
|
||||
name=setting['name']
|
||||
)
|
||||
}
|
||||
)
|
||||
else:
|
||||
# Safe fallback to allow the revertsettings command to complete.
|
||||
DATABASES = {
|
||||
'default': {
|
||||
'ENGINE': 'django.db.backends.sqlite3',
|
||||
'NAME': os.path.join(MEDIA_ROOT, 'db.sqlite3'),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for app in INSTALLED_APPS:
|
||||
if 'mayan.apps.{}'.format(app) in BASE_INSTALLED_APPS:
|
||||
@@ -385,32 +312,31 @@ for app in INSTALLED_APPS:
|
||||
'in https://docs.mayan-edms.com/releases/3.2.html#backward-incompatible-changes'
|
||||
)
|
||||
|
||||
|
||||
for APP in (COMMON_EXTRA_APPS or ()):
|
||||
for APP in (COMMON_EXTRA_APPS or ()): # NOQA: F821
|
||||
INSTALLED_APPS = INSTALLED_APPS + (APP,)
|
||||
|
||||
|
||||
INSTALLED_APPS = [
|
||||
APP for APP in INSTALLED_APPS if APP not in (COMMON_DISABLED_APPS or ())
|
||||
APP for APP in INSTALLED_APPS if APP not in (COMMON_DISABLED_APPS or ()) # NOQA: F821
|
||||
]
|
||||
|
||||
if not DATABASES:
|
||||
if DATABASE_ENGINE:
|
||||
if DATABASE_ENGINE: # NOQA: F821
|
||||
DATABASES = {
|
||||
'default': {
|
||||
'ENGINE': DATABASE_ENGINE,
|
||||
'NAME': DATABASE_NAME,
|
||||
'USER': DATABASE_USER,
|
||||
'PASSWORD': DATABASE_PASSWORD,
|
||||
'HOST': DATABASE_HOST,
|
||||
'PORT': DATABASE_PORT,
|
||||
'CONN_MAX_AGE': DATABASE_CONN_MAX_AGE
|
||||
'ENGINE': DATABASE_ENGINE, # NOQA: F821
|
||||
'NAME': DATABASE_NAME, # NOQA: F821
|
||||
'USER': DATABASE_USER, # NOQA: F821
|
||||
'PASSWORD': DATABASE_PASSWORD, # NOQA: F821
|
||||
'HOST': DATABASE_HOST, # NOQA: F821
|
||||
'PORT': DATABASE_PORT, # NOQA: F821
|
||||
'CONN_MAX_AGE': DATABASE_CONN_MAX_AGE # NOQA: F821
|
||||
}
|
||||
}
|
||||
else:
|
||||
DATABASES = {
|
||||
'default': {
|
||||
'ENGINE': 'django.db.backends.sqlite3',
|
||||
'NAME': os.path.join(MEDIA_ROOT, 'db.sqlite3'),
|
||||
'NAME': os.path.join(MEDIA_ROOT, 'db.sqlite3'), # NOQA: F821
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user