Make randomid deterministic to improve caching

This commit is contained in:
2026-05-12 08:27:11 +02:00
parent 140f3d2bd6
commit ebef0bba87
3 changed files with 24 additions and 22 deletions
+5 -3
View File
@@ -1,5 +1,4 @@
import random
import string
import hashlib
from django import template
@@ -8,4 +7,7 @@ register = template.Library()
@register.simple_tag
def randomid(seed: str = "") -> str:
return str(hash(seed + "".join(random.choices(string.ascii_lowercase, k=10))))
content_hash = hashlib.sha1(seed.encode()).hexdigest()
if seed:
return content_hash[:max(0, 10 - len(seed))] + seed
return content_hash[:10]