Skip to content

Commit 62172bc

Browse files
Fix component discovery for nested and installed app directories (#94)
1 parent c7851e5 commit 62172bc

File tree

3 files changed

+461
-111
lines changed

3 files changed

+461
-111
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ and this project attempts to adhere to [Semantic Versioning](https://semver.org/
1818

1919
## [Unreleased]
2020

21+
### Fixed
22+
23+
- Fixed component discovery for nested directories and Django app templates.
24+
2125
## [0.8.0]
2226

2327
### Changed

src/django_bird/components.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
from typing import Any
77

88
from cachetools import LRUCache
9+
from django.apps import apps
910
from django.conf import settings
1011
from django.template.backends.django import Template as DjangoTemplate
12+
from django.template.engine import Engine
1113
from django.template.exceptions import TemplateDoesNotExist
1214
from django.template.loader import select_template
1315

@@ -51,8 +53,26 @@ class ComponentRegistry:
5153
def __init__(self, maxsize: int = 100):
5254
self._components: LRUCache[str, Component] = LRUCache(maxsize=maxsize)
5355

56+
def get_dirs(self):
57+
engine = Engine.get_default()
58+
dirs: list[str | Path] = list(engine.dirs)
59+
60+
for app_config in apps.get_app_configs():
61+
template_dir = Path(app_config.path) / "templates"
62+
if template_dir.is_dir():
63+
dirs.append(template_dir)
64+
65+
base_dir = getattr(settings, "BASE_DIR", None)
66+
67+
if base_dir is not None:
68+
root_template_dir = Path(base_dir) / "templates"
69+
if root_template_dir.is_dir():
70+
dirs.append(root_template_dir)
71+
72+
return dirs
73+
5474
def discover_components(self) -> None:
55-
template_dirs = [dir for config in settings.TEMPLATES for dir in config["DIRS"]]
75+
template_dirs = self.get_dirs()
5676

5777
component_dirs = [
5878
Path(template_dir) / component_dir
@@ -61,17 +81,15 @@ def discover_components(self) -> None:
6181
]
6282

6383
for component_dir in component_dirs:
84+
component_dir = Path(component_dir)
85+
6486
if not component_dir.is_dir():
6587
continue
6688

67-
for component_path in component_dir.iterdir():
68-
if component_path.is_dir():
69-
component_name = component_path.name
70-
elif component_path.suffix == ".html":
71-
component_name = component_path.stem
72-
else:
73-
continue
74-
89+
for component_path in component_dir.rglob("*.html"):
90+
component_name = str(
91+
component_path.relative_to(component_dir).with_suffix("")
92+
)
7593
try:
7694
self.get_component(component_name)
7795
except TemplateDoesNotExist:

0 commit comments

Comments
 (0)