mirror of
https://github.com/chibicitiberiu/ytsm.git
synced 2024-02-24 05:43:31 +00:00
Improved settings.py to read the global settings from the .ini file.
This commit is contained in:
@ -11,23 +11,12 @@ https://docs.djangoproject.com/en/1.11/ref/settings/
|
||||
"""
|
||||
|
||||
import os
|
||||
import logging
|
||||
|
||||
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
|
||||
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.11/howto/deployment/checklist/
|
||||
|
||||
# SECURITY WARNING: keep the secret key used in production secret!
|
||||
SECRET_KEY = '^zv8@i2h!ko2lo=%ivq(9e#x=%q*i^^)6#4@(juzdx%&0c+9a0'
|
||||
|
||||
YOUTUBE_API_KEY = os.getenv('YTSM_YOUTUBE_API_KEY', 'AIzaSyBabzE4Bup77WexdLMa9rN9z-wJidEfNX8')
|
||||
|
||||
# SECURITY WARNING: don't run with debug turned on in production!
|
||||
DEBUG = True
|
||||
|
||||
#
|
||||
# Basic Django stuff
|
||||
#
|
||||
ALLOWED_HOSTS = ['*']
|
||||
|
||||
SESSION_COOKIE_AGE = 3600 * 30 # one month
|
||||
|
||||
# Application definition
|
||||
@ -76,24 +65,6 @@ TEMPLATES = [
|
||||
WSGI_APPLICATION = 'YtManager.wsgi.application'
|
||||
|
||||
|
||||
# Database
|
||||
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
|
||||
|
||||
DATABASES = {
|
||||
'default': {
|
||||
'ENGINE': os.getenv('YTSM_DATABASE_ENGINE', 'django.db.backends.sqlite3'),
|
||||
'NAME': os.getenv('YTSM_DATABASE_NAME', os.path.join(BASE_DIR, 'ytmanager.db')),
|
||||
'HOST': os.getenv('YTSM_DATABASE_HOST', None),
|
||||
'USER': os.getenv('YTSM_DATABASE_USERNAME', None),
|
||||
'PASSWORD': os.getenv('YTSM_DATABASE_PASSWORD', None),
|
||||
'PORT': os.getenv('YTSM_DATABASE_PORT', None)
|
||||
}
|
||||
}
|
||||
|
||||
if os.getenv('YTSM_DATABASE_URL', None):
|
||||
import dj_database_url
|
||||
DATABASES['default'] = dj_database_url.parse(os.environ['YTSM_DATABASE_URL'], conn_max_age=600)
|
||||
|
||||
# Password validation
|
||||
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators
|
||||
|
||||
@ -112,6 +83,9 @@ AUTH_PASSWORD_VALIDATORS = [
|
||||
},
|
||||
]
|
||||
|
||||
LOGIN_REDIRECT_URL = '/'
|
||||
LOGIN_URL = '/login'
|
||||
|
||||
|
||||
# Internationalization
|
||||
# https://docs.djangoproject.com/en/1.11/topics/i18n/
|
||||
@ -126,14 +100,115 @@ 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/'
|
||||
MEDIA_ROOT = 'data/media'
|
||||
|
||||
|
||||
# Misc Django stuff
|
||||
|
||||
CRISPY_TEMPLATE_PACK = 'bootstrap4'
|
||||
|
||||
LOGIN_REDIRECT_URL = '/'
|
||||
LOGIN_URL = '/login'
|
||||
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, ...)
|
||||
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
_DEFAULT_CONFIG_FILE = os.path.join(BASE_DIR, 'config/config.ini')
|
||||
_DEFAULT_LOG_FILE = os.path.join(BASE_DIR, 'data/log.log')
|
||||
_DEFAULT_MEDIA_ROOT = os.path.join(BASE_DIR, 'data/media')
|
||||
|
||||
DEFAULTS_FILE = os.path.join(BASE_DIR, 'config/defaults.ini')
|
||||
CONFIG_FILE = os.getenv('YTSM_CONFIG_FILE', _DEFAULT_CONFIG_FILE)
|
||||
|
||||
#
|
||||
# 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(BASE_DIR, 'ytmanager.db'),
|
||||
'HOST': None,
|
||||
'USER': None,
|
||||
'PASSWORD': None,
|
||||
'PORT': None,
|
||||
}
|
||||
|
||||
_SCHEDULER_SYNC_SCHEDULE = '5 * * * *'
|
||||
_DEFAULT_SCHEDULER_CONCURRENCY = 1
|
||||
|
||||
|
||||
#
|
||||
# Load globals from config.ini
|
||||
#
|
||||
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())
|
||||
cfg.read([DEFAULTS_FILE, CONFIG_FILE])
|
||||
|
||||
# Debug
|
||||
global DEBUG
|
||||
DEBUG = cfg.getboolean('global', 'Debug', fallback=_DEFAULT_DEBUG)
|
||||
|
||||
# Media root, which is where thumbnails are stored
|
||||
global MEDIA_ROOT
|
||||
MEDIA_ROOT = cfg.get('global', 'MediaRoot', 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 = cfg.get('global', 'SecretKey', fallback=_DEFAULT_SECRET_KEY)
|
||||
YOUTUBE_API_KEY = cfg.get('global', 'YoutubeApiKey', 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'))
|
||||
|
||||
else:
|
||||
DATABASES['default'] = {
|
||||
'ENGINE': cfg.get('global', 'DatabaseEngine', fallback=_DEFAULT_DATABASE['ENGINE']),
|
||||
'NAME': cfg.get('global', 'DatabaseName', fallback=_DEFAULT_DATABASE['NAME']),
|
||||
'HOST': cfg.get('global', 'DatabaseHost', fallback=_DEFAULT_DATABASE['HOST']),
|
||||
'USER': cfg.get('global', 'DatabaseUser', fallback=_DEFAULT_DATABASE['USER']),
|
||||
'PASSWORD': cfg.get('global', 'DatabasePassword', fallback=_DEFAULT_DATABASE['PASSWORD']),
|
||||
'PORT': cfg.get('global', 'DatabasePort', fallback=_DEFAULT_DATABASE['PORT']),
|
||||
}
|
||||
|
||||
# Log settings
|
||||
global LOG_LEVEL, LOG_FILE
|
||||
log_level_str = cfg.get('global', 'LogLevel', fallback='INFO')
|
||||
|
||||
try:
|
||||
LOG_LEVEL = getattr(logging, log_level_str)
|
||||
except AttributeError:
|
||||
print("Invalid log level " + LOG_LEVEL)
|
||||
LOG_LEVEL = logging.INFO
|
||||
|
||||
LOG_FILE = cfg.get('global', 'LogFile', fallback=_DEFAULT_LOG_FILE)
|
||||
|
||||
# Scheduler settings
|
||||
global SCHEDULER_SYNC_SCHEDULE, SCHEDULER_CONCURRENCY
|
||||
SCHEDULER_SYNC_SCHEDULE = cfg.get('global', 'SynchronizationSchedule', fallback=_SCHEDULER_SYNC_SCHEDULE)
|
||||
SCHEDULER_CONCURRENCY = cfg.getint('global', 'SchedulerConcurrency', fallback=_DEFAULT_SCHEDULER_CONCURRENCY)
|
||||
|
||||
|
||||
load_config_ini()
|
||||
|
@ -12,35 +12,26 @@ from django.contrib.auth.models import User
|
||||
from .models import UserSettings, Subscription
|
||||
from .utils.extended_interpolation_with_env import ExtendedInterpolatorWithEnv
|
||||
|
||||
_CONFIG_DIR = os.path.join(dj_settings.BASE_DIR, 'config')
|
||||
_LOG_FILE = 'log.log'
|
||||
_LOG_PATH = os.path.join(_CONFIG_DIR, _LOG_FILE)
|
||||
_LOG_FORMAT = '%(asctime)s|%(process)d|%(thread)d|%(name)s|%(filename)s|%(lineno)d|%(levelname)s|%(message)s'
|
||||
|
||||
|
||||
class AppSettings(ConfigParser):
|
||||
_DEFAULT_INTERPOLATION = ExtendedInterpolatorWithEnv()
|
||||
__DEFAULTS_FILE = 'defaults.ini'
|
||||
__SETTINGS_FILE = 'config.ini'
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(allow_no_value=True, *args, **kwargs)
|
||||
self.__defaults_path = os.path.join(_CONFIG_DIR, AppSettings.__DEFAULTS_FILE)
|
||||
self.__settings_path = os.path.join(_CONFIG_DIR, AppSettings.__SETTINGS_FILE)
|
||||
|
||||
def initialize(self):
|
||||
self.read([self.__defaults_path, self.__settings_path])
|
||||
self.read([dj_settings.DEFAULTS_FILE, dj_settings.CONFIG_FILE])
|
||||
|
||||
def save(self):
|
||||
if os.path.exists(self.__settings_path):
|
||||
if os.path.exists(dj_settings.CONFIG_FILE):
|
||||
# Create a backup
|
||||
copyfile(self.__settings_path, self.__settings_path + ".backup")
|
||||
copyfile(dj_settings.CONFIG_FILE, dj_settings.CONFIG_FILE + ".backup")
|
||||
else:
|
||||
# Ensure directory exists
|
||||
settings_dir = os.path.dirname(self.__settings_path)
|
||||
settings_dir = os.path.dirname(dj_settings.CONFIG_FILE)
|
||||
os.makedirs(settings_dir, exist_ok=True)
|
||||
|
||||
with open(self.__settings_path, 'w') as f:
|
||||
with open(dj_settings.CONFIG_FILE, 'w') as f:
|
||||
self.write(f)
|
||||
|
||||
def __get_combined_dict(self, vars: Optional[Any], sub: Optional[Subscription], user: Optional[User]) -> ChainMap:
|
||||
@ -112,12 +103,10 @@ def initialize_app_config():
|
||||
|
||||
|
||||
def __initialize_logger():
|
||||
log_level_str = settings.get('global', 'LogLevel', fallback='INFO')
|
||||
log_dir = os.path.dirname(dj_settings.LOG_FILE)
|
||||
os.makedirs(log_dir, exist_ok=True)
|
||||
|
||||
try:
|
||||
log_level = getattr(logging, log_level_str)
|
||||
logging.basicConfig(filename=_LOG_PATH, level=log_level, format=_LOG_FORMAT)
|
||||
|
||||
except AttributeError:
|
||||
logging.basicConfig(filename=_LOG_PATH, level=logging.INFO, format=_LOG_FORMAT)
|
||||
logging.warning('Invalid log level "%s" in config file.', log_level_str)
|
||||
logging.basicConfig(
|
||||
filename=dj_settings.LOG_FILE,
|
||||
level=dj_settings.LOG_LEVEL,
|
||||
format=dj_settings.LOG_FORMAT)
|
||||
|
Reference in New Issue
Block a user