From 23873f7a5203bbfe9742839903411a3b0db8bfc4 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Wed, 22 Apr 2020 22:18:28 -0700 Subject: [PATCH] Add new lexicon model to request context --- amanuensis/models/factory.py | 2 +- amanuensis/server/__init__.py | 4 +++- amanuensis/server/helpers.py | 7 +++++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/amanuensis/models/factory.py b/amanuensis/models/factory.py index d658a01..2400886 100644 --- a/amanuensis/models/factory.py +++ b/amanuensis/models/factory.py @@ -42,5 +42,5 @@ class ModelFactory(): raise ValueError(f'Invalid index entry: {lid}') else: lid = identifier - lexicon = LexiconModel(self.root, lid) + lexicon = LexiconModel(self.root, lid) return lexicon diff --git a/amanuensis/server/__init__.py b/amanuensis/server/__init__.py index 1df4a2a..a124a38 100644 --- a/amanuensis/server/__init__.py +++ b/amanuensis/server/__init__.py @@ -3,12 +3,13 @@ import os from flask import Flask, render_template from flask_login import LoginManager -from amanuensis.config import get +from amanuensis.config import get, root from amanuensis.server.auth import get_bp as get_auth_bp from amanuensis.server.home import get_bp as get_home_bp from amanuensis.server.helpers import register_custom_filters from amanuensis.server.lexicon import get_bp as get_lex_bp from amanuensis.user import AnonymousUserModel +from amanuensis.models import ModelFactory # Flask app init static_root = os.path.abspath(get("static_root")) @@ -17,6 +18,7 @@ app = Flask( template_folder="../templates", static_folder=static_root) app.secret_key = bytes.fromhex(get('secret_key')) +app.config['model_factory'] = ModelFactory(root) app.jinja_options['trim_blocks'] = True app.jinja_options['lstrip_blocks'] = True register_custom_filters(app) diff --git a/amanuensis/server/helpers.py b/amanuensis/server/helpers.py index c7522b3..3270af4 100644 --- a/amanuensis/server/helpers.py +++ b/amanuensis/server/helpers.py @@ -3,14 +3,14 @@ from datetime import datetime from functools import wraps # Third party imports -from flask import g, flash, redirect, url_for +from flask import g, flash, redirect, url_for, current_app from flask_login import current_user # Module imports from amanuensis.lexicon import LexiconModel from amanuensis.parser import filesafe_title from amanuensis.user import UserModel - +from amanuensis.models import ModelFactory def register_custom_filters(app): """Adds custom filters to the Flask app""" @@ -42,6 +42,9 @@ def lexicon_param(route): if g.lexicon is None: flash("Couldn't find a lexicon with the name '{}'".format(name)) return redirect(url_for("home.home")) + # TODO transition to new model + model_factory: ModelFactory = current_app.config['model_factory'] + g.lexicon_ = model_factory.lexicon(name) return route(**kwargs) return with_lexicon