From b13d89a2b08229d5078a9530613290ba2aca9155 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Thu, 12 Aug 2021 19:08:42 -0700 Subject: [PATCH] Add backend to jinja context --- amanuensis/server/__init__.py | 37 +++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/amanuensis/server/__init__.py b/amanuensis/server/__init__.py index 0707744..5e845f8 100644 --- a/amanuensis/server/__init__.py +++ b/amanuensis/server/__init__.py @@ -4,11 +4,30 @@ import os from flask import Flask, g, url_for, redirect -from amanuensis.backend import lexiq, userq +from amanuensis.backend import lexiq, userq, memq from amanuensis.config import AmanuensisConfig, CommandLineConfig from amanuensis.db import DbContext +from amanuensis.parser import filesafe_title import amanuensis.server.auth as auth import amanuensis.server.home as home +import amanuensis.server.lexicon as lexicon + + +def date_format(dt: datetime, formatstr="%Y-%m-%d %H:%M:%S%z") -> str: + """Convert datetime to human-readable string""" + if dt is None: + return "never" + # Cast db time to UTC, then convert to local timezone + adjusted = dt.replace(tzinfo=timezone.utc).astimezone() + return adjusted.strftime(formatstr) + + +def article_link(title): + """Get the url for a lexicon by its title""" + return url_for( + 'lexicon.article', + name=g.lexicon.name, + title=filesafe_title(title)) def get_app( @@ -48,20 +67,12 @@ def get_app( app.teardown_appcontext(db_teardown) # Configure jinja options - app.jinja_options.update(trim_blocks=True, lstrip_blocks=True) - - def date_format(dt: datetime, formatstr="%Y-%m-%d %H:%M:%S%z") -> str: - if dt is None: - return "never" - # Cast db time to UTC, then convert to local timezone - adjusted = dt.replace(tzinfo=timezone.utc).astimezone() - return adjusted.strftime(formatstr) - - app.template_filter("date")(date_format) - def include_backend(): - return {"db": db, "lexiq": lexiq, "userq": userq} + return {"db": db, "lexiq": lexiq, "userq": userq, "memq": memq} + app.jinja_options.update(trim_blocks=True, lstrip_blocks=True) + app.template_filter("date")(date_format) + app.template_filter("articlelink")(article_link) app.context_processor(include_backend) # Set up Flask-Login