Fix issue #368 and add tests (#369)

This commit is contained in:
Dylan Castillo 2024-01-27 23:10:03 +01:00 committed by GitHub
parent 997ed52bb6
commit dc9f1b46b2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 106 additions and 9 deletions

View file

@ -1,4 +1,5 @@
import difflib
import inspect
from collections import ChainMap
from typing import Any, ClassVar, Dict, Iterable, Optional, Set, Tuple, Union
@ -87,10 +88,8 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
self.outer_context: Context = outer_context or Context()
self.fill_content = fill_content
@classmethod
@property
def class_hash(cls):
return hash(str(cls.__module__) + str(cls.__name__))
def __init_subclass__(cls, **kwargs):
cls.class_hash = hash(inspect.getfile(cls) + cls.__name__)
def get_context_data(self, *args, **kwargs) -> Dict[str, Any]:
return {}

View file

@ -12,17 +12,17 @@ from django.template.utils import get_app_template_dirs
class Loader(FilesystemLoader):
def get_dirs(self):
component_dir = "components"
directories = list(get_app_template_dirs(component_dir))
directories = set(get_app_template_dirs(component_dir))
if settings.SETTINGS_MODULE:
settings_path = Path(*settings.SETTINGS_MODULE.split("."))
path = (settings_path / ".." / component_dir).resolve()
if path.is_dir():
directories.append(path)
directories.add(path)
path = (settings_path / ".." / ".." / component_dir).resolve()
if path.is_dir():
directories.append(path)
directories.add(path)
return directories
return list(directories)

View file

@ -14,6 +14,12 @@ class Calendar(component.Component):
"date": date,
}
def get(self, request, *args, **kwargs):
context = {
"date": request.GET.get("date", ""),
}
return self.render_to_response(context)
class Media:
css = "calendar/calendar.css"
js = "calendar/calendar.js"

View file

@ -1,6 +1,8 @@
from components.calendar.calendar import Calendar
from components.greeting import Greeting
from django.urls import path
from greeting import Greeting
urlpatterns = [
path("greeting/", Greeting.as_view(), name="greeting"),
path("calendar/", Calendar.as_view(), name="calendar"),
]

View file

View file

@ -0,0 +1,5 @@
<form method="post">
{% csrf_token %}
<input type="text" name="variable" value="{{ variable }}">
<input type="submit">
</form>

View file

@ -0,0 +1,20 @@
from typing import Any, Dict
from django.http import HttpResponse
from django_components import component
@component.register("multi_file_component")
class MultFileComponent(component.Component):
template_name = "multi_file/multi_file.html"
def post(self, request, *args, **kwargs) -> HttpResponse:
variable = request.POST.get("variable")
return self.render_to_response({"variable": variable})
def get(self, request, *args, **kwargs) -> HttpResponse:
return self.render_to_response({"variable": "GET"})
def get_context_data(self, variable, *args, **kwargs) -> Dict[str, Any]:
return {"variable": variable}

View file

@ -0,0 +1,26 @@
from typing import Any, Dict
from django.http import HttpResponse
from django_components import component
@component.register("single_file_component")
class SingleFileComponent(component.Component):
template = """
<form method="post">
{% csrf_token %}
<input type="text" name="variable" value="{{ variable }}">
<input type="submit">
</form>
"""
def post(self, request, *args, **kwargs) -> HttpResponse:
variable = request.POST.get("variable")
return self.render_to_response({"variable": variable})
def get(self, request, *args, **kwargs) -> HttpResponse:
return self.render_to_response({"variable": "GET"})
def get_context_data(self, variable, *args, **kwargs) -> Dict[str, Any]:
return {"variable": variable}

9
tests/components/urls.py Normal file
View file

@ -0,0 +1,9 @@
from django.urls import path
from tests.components.multi_file.multi_file import MultFileComponent
from tests.components.single_file import SingleFileComponent
urlpatterns = [
path("single/", SingleFileComponent.as_view(), name="single"),
path("multi/", MultFileComponent.as_view(), name="multi"),
]

View file

@ -0,0 +1,30 @@
from django.urls import include, path
# isort: off
from .django_test_setup import * # noqa
from .testutils import Django30CompatibleSimpleTestCase as SimpleTestCase
# isort: on
from django_components import autodiscover, component
urlpatterns = [
path("", include("tests.components.urls")),
]
class TestAutodiscover(SimpleTestCase):
def setUp(self):
settings.SETTINGS_MODULE = "tests.test_autodiscover" # noqa
def tearDown(self) -> None:
del settings.SETTINGS_MODULE # noqa
def test_autodiscover_with_components_as_views(self):
try:
autodiscover()
except component.AlreadyRegistered:
self.fail(
"Autodiscover should not raise AlreadyRegistered exception"
)