config: update and slight improvements

* Rename Config → FlaskApplicationSettings so we know exactly what we're
  talking about
* Clarify that LocalConfig overrides both V5Config and Flask settings
* Only give defaults that are needed in LocalConfig and remove old
  settings that are no longer used
This commit is contained in:
Lephe 2022-05-05 19:31:26 +01:00
parent eb5ce1bd5c
commit a3ed633791
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
3 changed files with 30 additions and 19 deletions

View File

@ -4,13 +4,13 @@ from flask_migrate import Migrate
from flask_login import LoginManager
from flask_mail import Mail
from flask_wtf.csrf import CSRFProtect
from config import Config
from config import FlaskApplicationSettings
app = Flask(__name__)
app.config.from_object(Config)
app.config.from_object(FlaskApplicationSettings)
# Check security of secret
if Config.SECRET_KEY == "a-random-secret-key":
if FlaskApplicationSettings.SECRET_KEY == "a-random-secret-key":
raise Exception("Please use a strong secret key!")
db = SQLAlchemy(app)

View File

@ -6,11 +6,28 @@ try:
except ImportError:
print(" \033[92mWARNING: Local config not found\033[0m")
# The LocalConfig class serves a dual purpose and overrides settings in
# both V5Config and some fields of FlaskApplicationSettings. The first has
# defaults (DefaultConfig), but not the second, so we provide them here.
class LocalConfig():
pass
FLASK_DEBUG = False
DB_NAME = "pcv5"
SECRET_KEY = "a-random-secret-key"
#---
# Flask configuration
#---
class FlaskApplicationSettings(object):
"""
This object specifies the settings for the Flask application. All the keys
and values are predefined by Flask.
See: https://flask.palletsprojects.com/en/2.1.x/config/
"""
DEBUG = os.environ.get("FLASK_DEBUG") or LocalConfig.FLASK_DEBUG
class Config(object):
SECRET_KEY = os.environ.get('SECRET_KEY') or LocalConfig.SECRET_KEY
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \
'postgresql+psycopg2://' + os.environ.get('USER') + ':@/' \
@ -27,9 +44,11 @@ class Config(object):
# Do not attach cookies to cross-origin requests
SESSION_COOKIE_SAMESITE = "Lax"
#---
# v5 configuration
#---
class DefaultConfig(object):
"""Every value here can be overrided in the local_config.py class"""
# Domain
DOMAIN = "v5.planet-casio.com"
# Database name
@ -62,5 +81,9 @@ class DefaultConfig(object):
class V5Config(LocalConfig, DefaultConfig):
# Values put here cannot be overidden with local_config
"""
This object holds the settings for the v5 code. Each parameter has the
value specified in LocalConfig (if any), and defaults to the value set in
DefaultConfig.
"""
pass

View File

@ -1,12 +0,0 @@
# This is a sample file
# You can override every value available in the class DefaultConfig
class LocalConfig(object):
DB_NAME = "pcv5"
USE_LDAP = True
LDAP_PASSWORD = "openldap"
LDAP_ENV = "o=prod"
SECRET_KEY = "a-random-secret-key" # CHANGE THIS VALUE *NOW*
AVATARS_FOLDER = '/home/pc/data/avatars/'
ENABLE_GUEST_POST = True
SEND_MAILS = True