feat: paths as objects + user-provided Media cls + handle static (#526)

Co-authored-by: Emil Stenström <emil@emilstenstrom.se>
This commit is contained in:
Juro Oravec 2024-06-21 19:36:53 +02:00 committed by GitHub
parent 1d0d960211
commit 3c5a7ad823
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 1106 additions and 146 deletions

View file

@ -0,0 +1,33 @@
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
# Format as mentioned in https://github.com/EmilStenstrom/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")
return format_html('<script type="module" src="{}"></script>', static(self.static_path))
@component.register("relative_file_pathobj_component")
class RelativeFileWithPathObjComponent(component.Component):
template_name = "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}