From 8707b0fcd7f68c9dac300fd93b3410c7edbc6523 Mon Sep 17 00:00:00 2001 From: kaiyou Date: Mon, 10 Dec 2018 15:30:53 +0100 Subject: [PATCH] Use a dictionary of db connection string templates --- core/admin/mailu/configuration.py | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/core/admin/mailu/configuration.py b/core/admin/mailu/configuration.py index 7ee892b2..ab6a7aac 100644 --- a/core/admin/mailu/configuration.py +++ b/core/admin/mailu/configuration.py @@ -13,10 +13,10 @@ DEFAULT_CONFIG = { 'DOMAIN_REGISTRATION': False, 'TEMPLATES_AUTO_RELOAD': True, # Database settings - 'DB_FLAVOR': 'sqlite', + 'DB_FLAVOR': None, 'DB_USER': 'mailu', - 'DB_PW': '', - 'DB_URL': 'database', + 'DB_PW': None, + 'DB_HOST': 'database', 'DB_NAME': 'mailu', 'SQLALCHEMY_DATABASE_URI': 'sqlite:////data/main.db', 'SQLALCHEMY_TRACK_MODIFICATIONS': False, @@ -64,31 +64,30 @@ class ConfigManager(dict): """ Naive configuration manager that uses environment only """ + DB_TEMPLATES = { + 'sqlite': 'sqlite:////{DB_HOST}', + 'postgresql': 'postgresql://{DB_USER}:{DB_PW}@{DB_HOST}/{DB_NAME}', + 'mysql': 'mysql://{DB_USER}:{DB_PW}@{DB_HOST}/{DB_NAME}' + } + def __init__(self): self.config = dict() self.parse_env() def init_app(self, app): self.config.update(app.config) - self.parse_env() - if self.config['DB_FLAVOR'] != 'sqlite': - self.setsql() - app.config = self - - def parse_env(self): + # get environment variables self.config.update({ key: os.environ.get(key, value) for key, value in DEFAULT_CONFIG.items() }) - if self.config['SQL_FLAVOR'] != 'sqlite' - self.setsql() + # automatically set the sqlalchemy string + if self.config['DB_FLAVOR']: + template = self.DB_TEMPLATES[self.config['DB_FLAVOR']] + self.config['SQLALCHEMY_DATABASE_URI'] = template.format(**self.config) + # update the app config itself app.config = self - def setsql(self) - if not self.config['DB_PW'] - self.config['DB_PW'] = self.config['SECRET_KEY'] - self.config['SQLALCHEMY_DATABASE_URI'] = '{driver}://{user}:{pw}@{url}/{db}'.format(driver=DB_FLAVOR,user=DB_USER,pw=DB_PW,url=DB_URL,db=DB_NAME) - def setdefault(self, key, value): if key not in self.config: self.config[key] = value