ytsm/app/YtManager/settings.py

279 lines
9.0 KiB
Python

"""
Django settings for YtManager project.
Generated by 'django-admin startproject' using Django 1.11.11.
For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/ref/settings/
"""
import os
import logging
from os.path import dirname as up
#
# Basic Django stuff
#
ALLOWED_HOSTS = ['*']
SESSION_COOKIE_AGE = 3600 * 30 # one month
# Application definition
INSTALLED_APPS = [
'YtManagerApp.apps.YtManagerAppConfig',
'crispy_forms',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
'dynamic_preferences',
'dynamic_preferences.users.apps.UserPreferencesConfig',
]
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',
]
ROOT_URLCONF = 'YtManager.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.template.context_processors.media',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'dynamic_preferences.processors.global_preferences',
],
},
},
]
WSGI_APPLICATION = 'YtManager.wsgi.application'
# Password validation
# https://docs.djangoproject.com/en/1.11/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',
},
]
LOGIN_REDIRECT_URL = '/'
LOGIN_URL = '/login'
# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
# Misc Django stuff
CRISPY_TEMPLATE_PACK = 'bootstrap4'
LOG_FORMAT = '%(asctime)s|%(process)d|%(thread)d|%(name)s|%(filename)s|%(lineno)d|%(levelname)s|%(message)s'
#
# Directories
#
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
PROJECT_ROOT = up(up(os.path.dirname(__file__))) # Project root
BASE_DIR = up(os.path.dirname(__file__)) # Base dir of the application
CONFIG_DIR = os.path.join(PROJECT_ROOT, "config")
DATA_DIR = os.path.join(PROJECT_ROOT, "data")
STATIC_ROOT = os.path.join(PROJECT_ROOT, "static")
_DEFAULT_CONFIG_FILE = os.path.join(CONFIG_DIR, 'config.ini')
_DEFAULT_LOG_FILE = os.path.join(DATA_DIR, 'log.log')
_DEFAULT_MEDIA_ROOT = os.path.join(DATA_DIR, 'media')
CONFIG_FILE = os.getenv('YTSM_CONFIG_FILE', _DEFAULT_CONFIG_FILE)
DATA_CONFIG_FILE = os.path.join(DATA_DIR, 'config.ini')
#
# Defaults
#
_DEFAULT_DEBUG = False
_DEFAULT_SECRET_KEY = '^zv8@i2h!ko2lo=%ivq(9e#x=%q*i^^)6#4@(juzdx%&0c+9a0'
_DEFAULT_YOUTUBE_API_KEY = 'AIzaSyBabzE4Bup77WexdLMa9rN9z-wJidEfNX8'
_DEFAULT_DATABASE = {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(DATA_DIR, 'ytmanager.db'),
'HOST': None,
'USER': None,
'PASSWORD': None,
'PORT': None,
}
_SCHEDULER_SYNC_SCHEDULE = '5 * * * *'
_DEFAULT_SCHEDULER_CONCURRENCY = 1
CONFIG_ERRORS = []
CONFIG_WARNINGS = []
#
# Load globals from config.ini
#
def get_global_opt(name, cfgparser, env_variable=None, fallback=None, boolean=False, integer=False):
"""
Reads a configuration option, in the following order:
1. environment variable
2. config parser
3. fallback
:param name:
:param env_variable:
:param fallback:
:param boolean:
:return:
"""
# Get from environment variable
if env_variable is not None:
value = os.getenv(env_variable)
if value is not None and boolean:
return value.lower() in ['true', 't', 'on', 'yes', 'y', '1']
elif value is not None and integer:
try:
return int(value)
except ValueError:
CONFIG_WARNINGS.append(f'Environment variable {env_variable}: value must be an integer value!')
elif value is not None:
return value
# Get from config parser
if boolean:
try:
return cfgparser.getboolean('global', name, fallback=fallback)
except ValueError:
CONFIG_WARNINGS.append(f'config.ini file: Value set for option global.{name} is not valid! '
f'Valid options: true, false, on, off.')
return fallback
if integer:
try:
return cfgparser.getint('global', name, fallback=fallback)
except ValueError:
CONFIG_WARNINGS.append(f'config.ini file: Value set for option global.{name} must be an integer number! ')
return fallback
return cfgparser.get('global', name, fallback=fallback)
def load_config_ini():
from configparser import ConfigParser
from YtManagerApp.utils.extended_interpolation_with_env import ExtendedInterpolatorWithEnv
import dj_database_url
cfg = ConfigParser(allow_no_value=True, interpolation=ExtendedInterpolatorWithEnv())
read_ok = cfg.read([DEFAULTS_FILE, CONFIG_FILE, DATA_CONFIG_FILE])
if DEFAULTS_FILE not in read_ok:
CONFIG_ERRORS.append(f'File {DEFAULTS_FILE} could not be read! Please make sure the file is in the right place,'
f' and it has read permissions.')
# Debug
global DEBUG
DEBUG = get_global_opt('Debug', cfg, env_variable='YTSM_DEBUG', fallback=_DEFAULT_DEBUG, boolean=True)
# Media root, which is where thumbnails are stored
global MEDIA_ROOT
MEDIA_ROOT = get_global_opt('MediaRoot', cfg, env_variable='YTSM_MEDIA_ROOT', fallback=_DEFAULT_MEDIA_ROOT)
# Keys - secret key, youtube API key
# SECURITY WARNING: keep the secret key used in production secret!
global SECRET_KEY, YOUTUBE_API_KEY
SECRET_KEY = get_global_opt('SecretKey', cfg, env_variable='YTSM_SECRET_KEY', fallback=_DEFAULT_SECRET_KEY)
YOUTUBE_API_KEY = get_global_opt('YoutubeApiKey', cfg, env_variable='YTSM_YTAPI_KEY', fallback=_DEFAULT_YOUTUBE_API_KEY)
# Database
global DATABASES
DATABASES = {
'default': _DEFAULT_DATABASE
}
if cfg.has_option('global', 'DatabaseURL'):
DATABASES['default'] = dj_database_url.parse(cfg.get('global', 'DatabaseURL'), conn_max_age=600)
else:
DATABASES['default'] = {
'ENGINE': get_global_opt('DatabaseEngine', cfg, env_variable='YTSM_DB_ENGINE', fallback=_DEFAULT_DATABASE['ENGINE']),
'NAME': get_global_opt('DatabaseName', cfg, env_variable='YTSM_DB_NAME', fallback=_DEFAULT_DATABASE['NAME']),
'HOST': get_global_opt('DatabaseHost', cfg, env_variable='YTSM_DB_HOST', fallback=_DEFAULT_DATABASE['HOST']),
'USER': get_global_opt('DatabaseUser', cfg, env_variable='YTSM_DB_USER', fallback=_DEFAULT_DATABASE['USER']),
'PASSWORD': get_global_opt('DatabasePassword', cfg, env_variable='YTSM_DB_PASSWORD', fallback=_DEFAULT_DATABASE['PASSWORD']),
'PORT': get_global_opt('DatabasePort', cfg, env_variable='YTSM_DB_PORT', fallback=_DEFAULT_DATABASE['PORT']),
}
# Log settings
global LOG_LEVEL, LOG_FILE
log_level_str = get_global_opt('LogLevel', cfg, env_variable='YTSM_LOG_LEVEL', fallback='INFO')
try:
LOG_LEVEL = getattr(logging, log_level_str)
except AttributeError:
CONFIG_WARNINGS.append(f'Invalid log level {log_level_str}. '
f'Valid options are: DEBUG, INFO, WARN, ERROR, CRITICAL.')
print("Invalid log level " + LOG_LEVEL)
LOG_LEVEL = logging.INFO
LOG_FILE = get_global_opt('LogFile', cfg, env_variable='YTSM_LOG_FILE', fallback=_DEFAULT_LOG_FILE)
# Scheduler settings
global SCHEDULER_SYNC_SCHEDULE, SCHEDULER_CONCURRENCY
SCHEDULER_SYNC_SCHEDULE = get_global_opt('SynchronizationSchedule', cfg, fallback=_SCHEDULER_SYNC_SCHEDULE)
SCHEDULER_CONCURRENCY = get_global_opt('SchedulerConcurrency', cfg, fallback=_DEFAULT_SCHEDULER_CONCURRENCY, integer=True)
load_config_ini()