diff options
Diffstat (limited to 'uploader/__init__.py')
| -rw-r--r-- | uploader/__init__.py | 49 |
1 files changed, 37 insertions, 12 deletions
diff --git a/uploader/__init__.py b/uploader/__init__.py index 8b49ad5..afaa78d 100644 --- a/uploader/__init__.py +++ b/uploader/__init__.py @@ -11,7 +11,7 @@ from cachelib import FileSystemCache from gn_libs import jobs as gnlibs_jobs -from flask_session import Session +from flask_session import Session# type: ignore[attr-defined] from uploader.oauth2.client import user_logged_in, authserver_authorise_uri @@ -22,6 +22,7 @@ from .files.views import files from .species import speciesbp from .publications import pubbp from .oauth2.views import oauth2 +from .flask_extensions import url_for from .expression_data import exprdatabp from .errors import register_error_handlers from .background_jobs import background_jobs_bp @@ -64,16 +65,34 @@ def setup_logging(app: Flask) -> Flask: "SERVER_SOFTWARE", "").split('/') return __log_gunicorn__(app) if bool(software) else __log_dev__(app) -def setup_modules_logging(app_logger): +def setup_modules_logging(app_logger, modules): """Setup module-level loggers to the same log-level as the application.""" loglevel = logging.getLevelName(app_logger.getEffectiveLevel()) - - def __setup__(logger_name): - _logger = logging.getLogger(logger_name) + for module in modules: + _logger = logging.getLogger(module) _logger.setLevel(loglevel) - __setup__("uploader.publications.models") - __setup__("uploader.publications.datatables") + +def __setup_scratch_directory__(app: Flask) -> Flask: + app.config["SCRATCH_DIRECTORY"] = Path( + app.config["SCRATCH_DIRECTORY"]).absolute() + return app + +def __setup_upload_directory__(app: Flask) -> Flask: + if app.config.get("UPLOADS_DIRECTORY", "").strip() == "": + app.config["UPLOADS_DIRECTORY"] = app.config[ + "SCRATCH_DIRECTORY"].joinpath("uploads") + else: + app.config["UPLOADS_DIRECTORY"] = Path( + app.config["UPLOADS_DIRECTORY"].strip()).absolute() + + return app + + +def update_unspecified_defaults(app: Flask) -> Flask: + """Setup the defaults for necessary configurations that do not have values + specified for them.""" + return __setup_upload_directory__(__setup_scratch_directory__(app)) def create_app(config: Optional[dict] = None): @@ -103,23 +122,29 @@ def create_app(config: Optional[dict] = None): # Silently ignore secrets if the file does not exist. app.config.from_pyfile(secretsfile) app.config.update(config) # Override everything with passed in config + update_unspecified_defaults(app) ### END: Application configuration app.config["SESSION_CACHELIB"] = FileSystemCache( - cache_dir=Path(app.config["SESSION_FILESYSTEM_CACHE_PATH"]).absolute(), + cache_dir=str(Path(app.config["SESSION_FILESYSTEM_CACHE_PATH"]).absolute()), threshold=int(app.config["SESSION_FILESYSTEM_CACHE_THRESHOLD"]), default_timeout=int(app.config["SESSION_FILESYSTEM_CACHE_TIMEOUT"])) setup_logging(app) - setup_modules_logging(app.logger) + setup_modules_logging( + app.logger, tuple(app.config.get("LOGGABLE_MODULES", []))) # setup jinja2 symbols - app.add_template_global(lambda : request.url, name="request_url") + app.add_template_global(user_logged_in) + app.add_template_global(url_for, name="url_for") app.add_template_global(authserver_authorise_uri) + app.add_template_global(lambda : request.url, name="request_url") app.add_template_global(lambda: app.config["GN2_SERVER_URL"], name="gn2server_uri") - app.add_template_global(user_logged_in) - app.add_template_global(lambda : session.user_details()["email"], name="user_email") + app.add_template_global(lambda : session.user_details()["email"], + name="user_email") + app.add_template_global(lambda: app.config["FEATURE_FLAGS_HTTP"], + name="http_feature_flags") Session(app) |
