mirror of
https://github.com/django-components/django-components.git
synced 2025-08-11 09:48:00 +00:00
refactor: use component_id instead of Template as slot fill cache key
This commit is contained in:
parent
969f0bdc32
commit
1dd492314a
6 changed files with 398 additions and 75 deletions
|
@ -1,7 +1,9 @@
|
|||
import glob
|
||||
import random
|
||||
from pathlib import Path
|
||||
from typing import List, NamedTuple, Optional
|
||||
from typing import Callable, List, NamedTuple, Optional
|
||||
|
||||
from django.template.base import Node, NodeList
|
||||
from django.template.engine import Engine
|
||||
|
||||
from django_components.template_loader import Loader
|
||||
|
@ -35,3 +37,36 @@ def search(search_glob: Optional[str] = None, engine: Optional[Engine] = None) -
|
|||
component_filenames.append(Path(path))
|
||||
|
||||
return SearchResult(searched_dirs=dirs, matched_files=component_filenames)
|
||||
|
||||
|
||||
def walk_nodelist(nodes: NodeList, callback: Callable[[Node], None]) -> None:
|
||||
"""Recursively walk a NodeList, calling `callback` for each Node."""
|
||||
node_queue = [*nodes]
|
||||
while len(node_queue):
|
||||
node: Node = node_queue.pop()
|
||||
callback(node)
|
||||
node_queue.extend(get_node_children(node))
|
||||
|
||||
|
||||
def get_node_children(node: Node) -> NodeList:
|
||||
"""
|
||||
Get child Nodes from Node's nodelist atribute.
|
||||
|
||||
This function is taken from `get_nodes_by_type` method of `django.template.base.Node`.
|
||||
"""
|
||||
nodes = NodeList()
|
||||
for attr in node.child_nodelists:
|
||||
nodelist = getattr(node, attr, [])
|
||||
if nodelist:
|
||||
nodes.extend(nodelist)
|
||||
return nodes
|
||||
|
||||
|
||||
def gen_id(length: int = 5) -> str:
|
||||
# Generate random value
|
||||
# See https://stackoverflow.com/questions/2782229
|
||||
value = random.randrange(16**length)
|
||||
|
||||
# Signed hexadecimal (lowercase).
|
||||
# See https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting
|
||||
return f"{value:x}"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue