Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load templates from template engines’ DIRS setting #145

Merged
merged 2 commits into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Added

- We now officially support Django 3.2, and tentatively Django 4.0
- Load templates from template engines’ [`DIRS`](https://docs.djangoproject.com/en/3.2/ref/settings/#dirs) as well as apps’ `templates` subdirectories.

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion docs/reference/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ PATTERN_LIBRARY = {
### `SECTIONS`

`SECTIONS` controls the groups of templates that appear in the navigation.
The keys are the group titles and the values are lists of template name prefixes that will be searched to populate the groups.
The keys are the group titles and the values are lists of template name prefixes that will be searched to populate the groups. The pattern library searches for templates both in [`DIRS`](https://docs.djangoproject.com/en/3.2/ref/settings/#dirs) directories for template engines, and in the `templates` subdirectory inside each installed application if using [`APP_DIRS`](https://docs.djangoproject.com/en/3.2/ref/settings/#app-dirs).

You can use this to create basic two-folder "includes and pages" hierarchies:

Expand Down
10 changes: 9 additions & 1 deletion pattern_library/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import re

from django.conf import settings
from django.template import TemplateDoesNotExist
from django.template.context import Context
from django.template.loader import get_template, render_to_string
Expand Down Expand Up @@ -66,9 +67,16 @@ def order_dict(dictionary, key_sort=None):
return dict(values)


def get_template_dirs():
template_dirs = [d for engines in settings.TEMPLATES for d in engines.get("DIRS", [])]
template_app_dirs = get_app_template_dirs('templates')
template_dirs += template_app_dirs
return template_dirs


def get_pattern_templates():
templates = base_dict()
template_dirs = get_app_template_dirs('templates')
template_dirs = get_template_dirs()

for lookup_dir in template_dirs:
for root, dirs, files in os.walk(lookup_dir, topdown=True):
Expand Down
50 changes: 48 additions & 2 deletions tests/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from django.test import SimpleTestCase
import os

from pattern_library.utils import get_template_ancestors
from django.conf import settings
from django.test import SimpleTestCase, override_settings

from pattern_library.utils import get_template_ancestors, get_template_dirs


class TestGetTemplateAncestors(SimpleTestCase):
Expand Down Expand Up @@ -33,3 +36,46 @@ def test_parent_template_from_variable(self):
'patterns/base.html',
],
)


class TestGetTemplateDirs(SimpleTestCase):
def get_relative_template_dirs(self):
"""Make paths relative with a predefined root so we can use them in assertions."""
base = os.path.dirname(settings.BASE_DIR)
dirs = get_template_dirs()
return ['/'.join(str(d).replace(base, 'dpl').split('/')[-4:-1]) for d in dirs]

@override_settings(TEMPLATES=[
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'APP_DIRS': True,
},
])
def test_get_template_dirs_app_dirs(self):
self.assertListEqual(self.get_relative_template_dirs(), [
'django/contrib/auth',
'dpl/pattern_library',
'dpl/tests',
])

@override_settings(TEMPLATES=[
{
'NAME': 'one',
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(settings.BASE_DIR, "test_one", "templates")],
'APP_DIRS': True,
},
{
'NAME': 'two',
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(settings.BASE_DIR, "test_two", "templates")],
},
])
def test_get_template_dirs_list_dirs(self):
self.assertListEqual(self.get_relative_template_dirs(), [
'dpl/tests/test_one',
'dpl/tests/test_two',
'django/contrib/auth',
'dpl/pattern_library',
'dpl/tests',
])