2019-11-13 18:40:01 +01:00
|
|
|
import os
|
2021-02-20 16:59:31 +01:00
|
|
|
from typing import Dict, List, Tuple
|
2019-11-13 18:40:01 +01:00
|
|
|
|
2021-01-20 16:47:00 +01:00
|
|
|
from flask import Flask
|
|
|
|
|
2020-09-16 15:41:02 +02:00
|
|
|
from fittrackee import db
|
|
|
|
from fittrackee.users.models import User
|
2019-11-13 18:40:01 +01:00
|
|
|
|
|
|
|
from .models import AppConfig
|
|
|
|
|
|
|
|
MAX_FILE_SIZE = 1 * 1024 * 1024 # 1MB
|
|
|
|
|
|
|
|
|
2021-01-02 19:28:03 +01:00
|
|
|
def init_config() -> Tuple[bool, AppConfig]:
|
2019-11-13 18:40:01 +01:00
|
|
|
"""
|
|
|
|
init application configuration if not existing in database
|
|
|
|
|
2019-11-13 20:15:50 +01:00
|
|
|
Note: get some configuration values from env variables
|
|
|
|
(for FitTrackee versions prior to v0.3.0)
|
2019-11-13 18:40:01 +01:00
|
|
|
"""
|
|
|
|
existing_config = AppConfig.query.one_or_none()
|
2019-11-13 21:46:24 +01:00
|
|
|
nb_users = User.query.count()
|
2019-11-13 18:40:01 +01:00
|
|
|
if not existing_config:
|
|
|
|
config = AppConfig()
|
2019-11-13 21:46:24 +01:00
|
|
|
config.max_users = (
|
|
|
|
nb_users
|
2019-11-13 18:40:01 +01:00
|
|
|
if os.getenv('REACT_APP_ALLOW_REGISTRATION') == "false"
|
2019-11-13 21:46:24 +01:00
|
|
|
else 0
|
2019-11-13 18:40:01 +01:00
|
|
|
)
|
|
|
|
config.max_single_file_size = os.environ.get(
|
|
|
|
'REACT_APP_MAX_SINGLE_FILE_SIZE', MAX_FILE_SIZE
|
|
|
|
)
|
|
|
|
config.max_zip_file_size = os.environ.get(
|
|
|
|
'REACT_APP_MAX_ZIP_FILE_SIZE', MAX_FILE_SIZE * 10
|
|
|
|
)
|
|
|
|
db.session.add(config)
|
|
|
|
db.session.commit()
|
|
|
|
return True, config
|
|
|
|
return False, existing_config
|
|
|
|
|
|
|
|
|
2021-01-02 19:28:03 +01:00
|
|
|
def update_app_config_from_database(
|
|
|
|
current_app: Flask, db_config: AppConfig
|
|
|
|
) -> None:
|
2019-11-13 18:40:01 +01:00
|
|
|
current_app.config['gpx_limit_import'] = db_config.gpx_limit_import
|
|
|
|
current_app.config['max_single_file_size'] = db_config.max_single_file_size
|
|
|
|
current_app.config['MAX_CONTENT_LENGTH'] = db_config.max_zip_file_size
|
|
|
|
current_app.config['max_users'] = db_config.max_users
|
|
|
|
current_app.config[
|
|
|
|
'is_registration_enabled'
|
|
|
|
] = db_config.is_registration_enabled
|
2021-02-20 16:59:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
def verify_app_config(config_data: Dict) -> List:
|
|
|
|
"""
|
|
|
|
Verify if application config is valid.
|
|
|
|
|
|
|
|
If not, it returns not empty string
|
|
|
|
"""
|
|
|
|
ret = []
|
|
|
|
if (
|
|
|
|
'gpx_limit_import' in config_data
|
|
|
|
and config_data['gpx_limit_import'] <= 0
|
|
|
|
):
|
|
|
|
ret.append('Max. files in a zip archive must be greater than 0')
|
|
|
|
|
|
|
|
if (
|
|
|
|
'max_single_file_size' in config_data
|
|
|
|
and config_data['max_single_file_size'] <= 0
|
|
|
|
):
|
|
|
|
ret.append('Max. size of uploaded files must be greater than 0')
|
|
|
|
|
|
|
|
if (
|
|
|
|
'max_zip_file_size' in config_data
|
|
|
|
and config_data['max_zip_file_size'] <= 0
|
|
|
|
):
|
|
|
|
ret.append('Max. size of zip archive must be greater than 0')
|
|
|
|
return ret
|