Reorganize repos
This commit is contained in:
87
app/filters.py
Normal file
87
app/filters.py
Normal file
@ -0,0 +1,87 @@
|
||||
import humanize
|
||||
import datetime
|
||||
from flask import current_app
|
||||
from zoneinfo import ZoneInfo
|
||||
import logging
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def htime(timestamp):
|
||||
if timestamp:
|
||||
dt = datetime.datetime.fromtimestamp(timestamp)
|
||||
return humanize.naturaltime(dt)
|
||||
|
||||
|
||||
def htime_dt(datetime):
|
||||
if datetime:
|
||||
try:
|
||||
return humanize.naturaltime(datetime)
|
||||
except ValueError:
|
||||
return "Never"
|
||||
return "Never"
|
||||
|
||||
|
||||
def hdate(timestamp):
|
||||
if timestamp:
|
||||
dt = datetime.datetime.fromtimestamp(timestamp)
|
||||
return humanize.naturaldate(dt)
|
||||
|
||||
|
||||
def hdate_dt(datetime):
|
||||
if datetime:
|
||||
try:
|
||||
return humanize.naturaldate(datetime)
|
||||
except ValueError:
|
||||
return "Never"
|
||||
return "Never"
|
||||
|
||||
|
||||
def fmt_timestamp(timestamp):
|
||||
with current_app.app_context():
|
||||
tz = ZoneInfo(current_app.config['APP_TZ'])
|
||||
if timestamp:
|
||||
local_ts = datetime.datetime.fromtimestamp(timestamp, tz)
|
||||
return "%s %s" % (local_ts.strftime('%Y-%m-%d %H:%M:%S'),
|
||||
local_ts.tzname())
|
||||
|
||||
|
||||
def fmt_datetime(datetime):
|
||||
with current_app.app_context():
|
||||
tz = ZoneInfo(current_app.config['APP_TZ'])
|
||||
if datetime:
|
||||
try:
|
||||
local_ts = datetime.fromtimestamp(datetime.timestamp(), tz)
|
||||
except OverflowError:
|
||||
return "Never"
|
||||
return "%s %s" % (local_ts.strftime('%Y-%m-%d %H:%M:%S'),
|
||||
local_ts.tzname())
|
||||
|
||||
|
||||
def fancyBool(bool_):
|
||||
if bool_:
|
||||
return '<span class="fancyTrue">✔</span>'
|
||||
else:
|
||||
return '<span class="fancyFalse">✘</span>'
|
||||
|
||||
|
||||
def fancyOnline(online):
|
||||
return ["offline", "online"][online]
|
||||
|
||||
|
||||
# A list of all of the template_filter functions to add in `init_app`
|
||||
FILTERS = [hdate,
|
||||
hdate_dt,
|
||||
htime,
|
||||
htime_dt,
|
||||
fmt_timestamp,
|
||||
fmt_datetime,
|
||||
fancyBool,
|
||||
fancyOnline]
|
||||
|
||||
|
||||
def init_app(app):
|
||||
"""Register the template filters with an app instance"""
|
||||
log.debug("filters init")
|
||||
for func in FILTERS:
|
||||
app.jinja_env.filters[func.__name__] = func
|
Reference in New Issue
Block a user