mirror of
https://github.com/django-components/django-components.git
synced 2025-08-03 13:58:16 +00:00
parent
997ed52bb6
commit
dc9f1b46b2
10 changed files with 106 additions and 9 deletions
|
@ -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 {}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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"),
|
||||
]
|
||||
|
|
0
tests/components/__init__.py
Normal file
0
tests/components/__init__.py
Normal file
5
tests/components/multi_file/multi_file.html
Normal file
5
tests/components/multi_file/multi_file.html
Normal file
|
@ -0,0 +1,5 @@
|
|||
<form method="post">
|
||||
{% csrf_token %}
|
||||
<input type="text" name="variable" value="{{ variable }}">
|
||||
<input type="submit">
|
||||
</form>
|
20
tests/components/multi_file/multi_file.py
Normal file
20
tests/components/multi_file/multi_file.py
Normal 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}
|
26
tests/components/single_file.py
Normal file
26
tests/components/single_file.py
Normal 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
9
tests/components/urls.py
Normal 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"),
|
||||
]
|
30
tests/test_autodiscover.py
Normal file
30
tests/test_autodiscover.py
Normal 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"
|
||||
)
|
Loading…
Add table
Add a link
Reference in a new issue