mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-29 19:35:20 +02:00
feat(automation): add custom template filters
This commit is contained in:
parent
b4e5bf95a4
commit
08e94ac5ca
1 changed files with 29 additions and 0 deletions
29
surfsense_backend/app/automations/templating/filters.py
Normal file
29
surfsense_backend/app/automations/templating/filters.py
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
"""Custom Jinja filters registered into the sandboxed environment."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
|
||||
def filter_date(value: Any, fmt: str = "%Y-%m-%d") -> str:
|
||||
"""Format a datetime-like value with ``strftime``. Strings pass through."""
|
||||
if value is None:
|
||||
return ""
|
||||
if isinstance(value, str):
|
||||
return value
|
||||
if hasattr(value, "strftime"):
|
||||
return value.strftime(fmt)
|
||||
raise ValueError(f"date filter requires datetime-like, got {type(value).__name__}")
|
||||
|
||||
|
||||
_SLUG_NONALNUM = re.compile(r"[^a-z0-9]+")
|
||||
_SLUG_DASHES = re.compile(r"-+")
|
||||
|
||||
|
||||
def filter_slugify(value: Any) -> str:
|
||||
"""Lowercase, replace non-alphanumerics with hyphens, collapse and trim."""
|
||||
s = str(value).lower()
|
||||
s = _SLUG_NONALNUM.sub("-", s)
|
||||
s = _SLUG_DASHES.sub("-", s)
|
||||
return s.strip("-")
|
||||
Loading…
Add table
Add a link
Reference in a new issue