mirror of
https://github.com/retspen/webvirtcloud
synced 2026-11-08 11:16:47 +00:00
django-bootstrap-icons (last release July 2024) only read the SVG of
each {% bs_icon %} from static/icon_cache; for an icon not there it
fetched the SVG from a CDN while rendering the page, and it brought
defusedxml along. A 20-line template tag in webvirtcloud reads the
same files; the package, its CDN path and the BS_ICONS_CACHE setting
go. requests stays: websockify needs it.
24 lines
775 B
Python
24 lines
775 B
Python
"""{% bs_icon %}: a Bootstrap icon's SVG, inline, from static/icon_cache,
|
|
which holds each icon a template uses at the size, color and classes it
|
|
asks for (webvirtcloud/tests/test_icon_cache.py checks every template)."""
|
|
|
|
from functools import cache
|
|
from pathlib import Path
|
|
|
|
from django import template
|
|
from django.conf import settings
|
|
from django.utils.safestring import mark_safe
|
|
|
|
register = template.Library()
|
|
|
|
ICONS = Path(settings.BASE_DIR, "static", "icon_cache")
|
|
|
|
|
|
@register.simple_tag
|
|
def bs_icon(name, size=None, color=None, extra_classes=None):
|
|
return _svg(f"{name}_{size}_{color}_{extra_classes}.svg".replace(" ", "_"))
|
|
|
|
|
|
@cache # one file a literal of the templates
|
|
def _svg(file_name):
|
|
return mark_safe((ICONS / file_name).read_text(encoding="utf-8"))
|