rDrama/files/helpers/jinja2.py
TLSM fcb51c09d3
Implement assetcache module with asset hashing.
Provides a module `assetcache` to support automatic cachebusting of
static web assets, rather than the current approach of incrementing
a version number.

On module load, it walks the file/assets/ dir, computes a CRC32 of
each asset, and provides those hashes via `assetcache_get(path)` at
runtime. The primary consumer of hashes at present is the new `asset`
filter for Jinja2, which accepts a relative asset path and returns the
full path, with cache busting, suitable for final use in the template.

Ex: `{{ 'css/main.css' | asset }}`
2022-08-30 16:00:24 -04:00

81 lines
1.7 KiB
Python

from files.__main__ import app
from .get import *
from os import listdir, environ
from .const import *
import time
from files.helpers.assetcache import assetcache_get
@app.template_filter("post_embed")
def post_embed(id, v):
try: id = int(id)
except: return None
p = get_post(id, v, graceful=True)
if p: return render_template("submission_listing.html", listing=[p], v=v)
return ''
@app.template_filter("timestamp")
def timestamp(timestamp):
age = int(time.time()) - timestamp
if age < 60:
return "just now"
elif age < 3600:
minutes = int(age / 60)
return f"{minutes}m ago"
elif age < 86400:
hours = int(age / 3600)
return f"{hours}hr ago"
elif age < 2678400:
days = int(age / 86400)
return f"{days}d ago"
now = time.gmtime()
ctd = time.gmtime(timestamp)
months = now.tm_mon - ctd.tm_mon + 12 * (now.tm_year - ctd.tm_year)
if now.tm_mday < ctd.tm_mday:
months -= 1
if months < 12:
return f"{months}mo ago"
else:
years = int(months / 12)
return f"{years}yr ago"
@app.template_filter("asset")
def template_asset(asset_path):
outpath = '/assets/' + asset_path
cachehash = assetcache_get(asset_path)
if cachehash:
outpath += '?v=' + cachehash
return outpath
@app.context_processor
def inject_constants():
return {
"environ":environ,
"SITE":SITE,
"SITE_ID":SITE_ID,
"SITE_TITLE":SITE_TITLE,
"SITE_FULL":SITE_FULL,
"AUTOJANNY_ID":AUTOJANNY_ID,
"NOTIFICATIONS_ID":NOTIFICATIONS_ID,
"PUSHER_ID":PUSHER_ID,
"CC":CC,
"CC_TITLE":CC_TITLE,
"listdir":listdir,
"MOOSE_ID":MOOSE_ID,
"AEVANN_ID":AEVANN_ID,
"PIZZASHILL_ID":PIZZASHILL_ID,
"config":app.config.get,
"DEFAULT_COLOR":DEFAULT_COLOR,
"COLORS":COLORS,
"ADMINISTRATORS":ADMINISTRATORS
}