Randomize sidebar recommendation order (#483)
Fixes #483. Implement a Jinja template filter to shuffle a sequence (which surprisingly doesn't already exist) and shuffle the relevant lists in the sidebar.
This commit is contained in:
parent
50b098f740
commit
7a9a3f7c99
2 changed files with 32 additions and 9 deletions
|
@ -1,10 +1,26 @@
|
|||
from os import listdir, environ
|
||||
import random
|
||||
import time
|
||||
|
||||
from jinja2 import pass_context
|
||||
|
||||
from files.__main__ import app
|
||||
from .get import *
|
||||
from os import listdir, environ
|
||||
from .const import *
|
||||
import time
|
||||
from files.helpers.assetcache import assetcache_path
|
||||
|
||||
|
||||
@app.template_filter("shuffle")
|
||||
@pass_context
|
||||
def template_shuffle(ctx, l: list) -> list:
|
||||
# Uses @pass_context while ignoring `ctx` to prevent Jinja from
|
||||
# caching the result of the filter
|
||||
|
||||
# stdlib recommended idiom for shuffling out-of-place
|
||||
# as opposed to random.shuffle for in-place shuffling
|
||||
return random.sample(l, k=len(l))
|
||||
|
||||
|
||||
@app.template_filter("post_embed")
|
||||
def post_embed(id, v):
|
||||
p = get_post(id, v, graceful=True)
|
||||
|
@ -43,10 +59,12 @@ def timestamp(timestamp):
|
|||
years = int(months / 12)
|
||||
return f"{years}yr ago"
|
||||
|
||||
|
||||
@app.template_filter("asset")
|
||||
def template_asset(asset_path):
|
||||
return assetcache_path(asset_path)
|
||||
|
||||
|
||||
@app.context_processor
|
||||
def inject_constants():
|
||||
return {
|
||||
|
@ -68,6 +86,7 @@ def inject_constants():
|
|||
"PERMS":PERMS,
|
||||
}
|
||||
|
||||
|
||||
def template_function(func):
|
||||
assert(func.__name__ not in app.jinja_env.globals)
|
||||
app.jinja_env.globals[func.__name__] = func
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue