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 files.__main__ import app
|
||||||
from .get import *
|
from .get import *
|
||||||
from os import listdir, environ
|
|
||||||
from .const import *
|
from .const import *
|
||||||
import time
|
|
||||||
from files.helpers.assetcache import assetcache_path
|
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")
|
@app.template_filter("post_embed")
|
||||||
def post_embed(id, v):
|
def post_embed(id, v):
|
||||||
p = get_post(id, v, graceful=True)
|
p = get_post(id, v, graceful=True)
|
||||||
|
@ -43,10 +59,12 @@ def timestamp(timestamp):
|
||||||
years = int(months / 12)
|
years = int(months / 12)
|
||||||
return f"{years}yr ago"
|
return f"{years}yr ago"
|
||||||
|
|
||||||
|
|
||||||
@app.template_filter("asset")
|
@app.template_filter("asset")
|
||||||
def template_asset(asset_path):
|
def template_asset(asset_path):
|
||||||
return assetcache_path(asset_path)
|
return assetcache_path(asset_path)
|
||||||
|
|
||||||
|
|
||||||
@app.context_processor
|
@app.context_processor
|
||||||
def inject_constants():
|
def inject_constants():
|
||||||
return {
|
return {
|
||||||
|
@ -68,6 +86,7 @@ def inject_constants():
|
||||||
"PERMS":PERMS,
|
"PERMS":PERMS,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def template_function(func):
|
def template_function(func):
|
||||||
assert(func.__name__ not in app.jinja_env.globals)
|
assert(func.__name__ not in app.jinja_env.globals)
|
||||||
app.jinja_env.globals[func.__name__] = func
|
app.jinja_env.globals[func.__name__] = func
|
||||||
|
|
|
@ -116,17 +116,21 @@
|
||||||
|
|
||||||
<h4>Recommended Posts And Communities</h4>
|
<h4>Recommended Posts And Communities</h4>
|
||||||
<ul>
|
<ul>
|
||||||
<li><a href="https://www.vault.themotte.org/">The Vault</a></li>
|
{{[
|
||||||
<li><a href="https://www.lesswrong.com/">Lesswrong</a></li>
|
'<li><a href="https://www.vault.themotte.org/">The Vault</a></li>',
|
||||||
<li><a href="https://astralcodexten.substack.com/">Astral Codex Ten</a></li>
|
'<li><a href="https://www.lesswrong.com/">Lesswrong</a></li>',
|
||||||
<li><a href="https://www.slatestarcodex.com/">Slate Star Codex</a></li>
|
'<li><a href="https://astralcodexten.substack.com/">Astral Codex Ten</a></li>',
|
||||||
<li><a href="https://www.reddit.com/r/FeMRADebates/">FeMRA Debates</a></li>
|
'<li><a href="https://www.slatestarcodex.com/">Slate Star Codex</a></li>',
|
||||||
|
'<li><a href="https://www.reddit.com/r/FeMRADebates/">FeMRA Debates</a></li>',
|
||||||
|
]|shuffle|join('\n')|safe}}
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<h4>Recommended Realtime Chats</h4>
|
<h4>Recommended Realtime Chats</h4>
|
||||||
<ul>
|
<ul>
|
||||||
<li><a href="https://discord.gg/Hkyq5eN">Astral Codex Ten Discord</a></li>
|
{{[
|
||||||
<li><a href="https://t.me/quokkas_den">Quokka's Den Telegram</a></li>
|
'<li><a href="https://discord.gg/Hkyq5eN">Astral Codex Ten Discord</a></li>',
|
||||||
|
'<li><a href="https://t.me/quokkas_den">Quokka\'s Den Telegram</a></li>',
|
||||||
|
]|shuffle|join('\n')|safe}}
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue