mirror of
https://github.com/django-components/django-components.git
synced 2025-08-09 08:48:00 +00:00
36 lines
1.3 KiB
Python
36 lines
1.3 KiB
Python
from typing import Any, Dict
|
|
|
|
from django.templatetags.static import static
|
|
from django.utils.html import format_html, html_safe
|
|
|
|
from django_components import Component, register
|
|
|
|
|
|
# Format as mentioned in https://github.com/django-components/django-components/issues/522#issuecomment-2173577094
|
|
@html_safe
|
|
class PathObj:
|
|
def __init__(self, static_path: str) -> None:
|
|
self.static_path = static_path
|
|
self.throw_on_calling_str = True
|
|
|
|
def __str__(self):
|
|
# This error will notify us when we've hit __str__ when we shouldn't have
|
|
if self.throw_on_calling_str:
|
|
raise RuntimeError("__str__ method of 'relative_file_pathobj_component' was triggered when not allow to")
|
|
|
|
if self.static_path.endswith(".js"):
|
|
return format_html('<script type="module" src="{}"></script>', static(self.static_path))
|
|
else:
|
|
return format_html('<link href="{}" rel="stylesheet">', static(self.static_path))
|
|
|
|
|
|
@register("relative_file_pathobj_component")
|
|
class RelativeFileWithPathObjComponent(Component):
|
|
template_file = "relative_file_pathobj.html"
|
|
|
|
class Media:
|
|
js = PathObj("relative_file_pathobj.js")
|
|
css = PathObj("relative_file_pathobj.css")
|
|
|
|
def get_context_data(self, variable, *args, **kwargs) -> Dict[str, Any]:
|
|
return {"variable": variable}
|