fix: implement safe retrieval of component classes from registry (#934)

This commit is contained in:
Oliver Haas 2025-02-01 11:08:35 +01:00 committed by GitHub
parent bafa9f7cc5
commit f01ee1fe8a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 24 additions and 30 deletions

View file

@ -1,8 +1,12 @@
import re
from typing import Any, Callable, List, Optional, Type, TypeVar
from hashlib import md5
from typing import TYPE_CHECKING, Any, Callable, List, Optional, Type, TypeVar
from django_components.util.nanoid import generate
if TYPE_CHECKING:
from django_components.component import Component
T = TypeVar("T")
@ -71,3 +75,9 @@ def get_last_index(lst: List, key: Callable[[Any], bool]) -> Optional[int]:
def is_nonempty_str(txt: Optional[str]) -> bool:
return txt is not None and bool(txt.strip())
def hash_comp_cls(comp_cls: Type["Component"]) -> str:
full_name = get_import_path(comp_cls)
comp_cls_hash = md5(full_name.encode()).hexdigest()[0:6]
return comp_cls.__name__ + "_" + comp_cls_hash