added some jinja filters

This commit is contained in:
Daniel Tsvetkov 2020-04-25 16:04:13 +02:00
parent 5e0bd4c128
commit b7623e46dd
2 changed files with 18 additions and 5 deletions

View File

@ -14,7 +14,7 @@ from flask_security import RoleMixin, UserMixin
from config import SQLALCHEMY_DATABASE_URI, MAKEDIRS, DATABASE_FILE from config import SQLALCHEMY_DATABASE_URI, MAKEDIRS, DATABASE_FILE
from sqlalchemy.orm.collections import InstrumentedList from sqlalchemy.orm.collections import InstrumentedList
from sqlalchemy_utils import Choice from sqlalchemy_utils import Choice
from tww.tww import solve_query from tww.tww import solve_query, resolve_timezone, dt_tz_translation
db = SQLAlchemy() db = SQLAlchemy()
@ -172,7 +172,7 @@ def register_filters(app):
return Markup(result) return Markup(result)
@app.template_filter('format_dt') @app.template_filter('format_dt')
def format_datetime(dt, formatting="%A, %d %b %Y"): def format_datetime(dt, formatting="%a, %d %b %Y"):
""" """
Formats the datetime string provided in value into whatever format you want that is supported by python strftime Formats the datetime string provided in value into whatever format you want that is supported by python strftime
http://strftime.org/ http://strftime.org/
@ -184,6 +184,17 @@ def register_filters(app):
dt = solve_query(dt) dt = solve_query(dt)
return dt.strftime(formatting) return dt.strftime(formatting)
@app.template_filter('to_tz')
def to_tz(dt, human_tz="utc", formatting='%H:%M', include_offset=True):
if type(dt) is str:
dt = solve_query(dt)
tz = resolve_timezone(human_tz)
dt = dt_tz_translation(dt, to_tz_offset=tz.get('tz_offset'))
base = dt.strftime(formatting)
if not include_offset:
return base
return "{} ({})".format(base, tz.get('tz_offset'))
def init_db(app): def init_db(app):
app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI

View File

@ -14,13 +14,14 @@ def list_view(model_view, template):
return inner return inner
def get_view(model_view, template, template_ctx): def get_view(model_view, template, template_ctx_func):
def inner(uuid): def inner(uuid):
model = model_view.model model = model_view.model
instance = model.query.filter_by(uuid=uuid).first() instance = model.query.filter_by(uuid=uuid).first()
if not instance: if not instance:
flash("No {}:{}".format(model_view.model_name, uuid)) flash("No {}:{}".format(model_view.model_name, uuid))
return redirect(request.referrer or url_for('home')) return redirect(request.referrer or url_for('home'))
template_ctx = template_ctx_func(instance) if template_ctx_func else {}
return render_template(template, instance=instance, **template_ctx) return render_template(template, instance=instance, **template_ctx)
return inner return inner
@ -113,11 +114,12 @@ class ModelView(object):
'list_{}'.format(self.model_name), 'list_{}'.format(self.model_name),
list_view(self, list_template)) list_view(self, list_template))
def register_get(self, retrieve_template, template_ctx=None): def register_get(self, retrieve_template, template_ctx_func=None):
url = '/{}/<uuid>'.format(self.model_name_pl) url = '/{}/<uuid>'.format(self.model_name_pl)
self.app.add_url_rule(url, self.app.add_url_rule(url,
'get_{}'.format(self.model_name), 'get_{}'.format(self.model_name),
get_view(self, retrieve_template, template_ctx)) get_view(self, retrieve_template, template_ctx_func))
def register_update(self, update_template): def register_update(self, update_template):
url = '/{}/<uuid>/edit'.format(self.model_name_pl) url = '/{}/<uuid>/edit'.format(self.model_name_pl)