-
-
Notifications
You must be signed in to change notification settings - Fork 32.9k
Fixed #25791 -- Implement autoreload behaviour for cached template loader #12928
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from django.dispatch import receiver | ||
from django.template import engines | ||
from django.template.backends.django import DjangoTemplates | ||
from django.utils.autoreload import ( | ||
autoreload_started, file_changed, is_django_path, | ||
) | ||
|
||
|
||
def get_template_directories(): | ||
# Iterate through each template backend and find | ||
# any template_loader that has a 'get_dirs' method. | ||
# Collect the directories, filtering out Django templates. | ||
items = set() | ||
for backend in engines.all(): | ||
if not isinstance(backend, DjangoTemplates): | ||
continue | ||
|
||
items.update(backend.engine.dirs) | ||
|
||
for loader in backend.engine.template_loaders: | ||
if not hasattr(loader, 'get_dirs'): | ||
continue | ||
items.update( | ||
directory | ||
for directory in loader.get_dirs() | ||
if not is_django_path(directory) | ||
) | ||
return items | ||
|
||
|
||
def reset_loaders(): | ||
for backend in engines.all(): | ||
if not isinstance(backend, DjangoTemplates): | ||
continue | ||
for loader in backend.engine.template_loaders: | ||
loader.reset() | ||
|
||
|
||
@receiver(autoreload_started, dispatch_uid='template_loaders_watch_changes') | ||
def watch_for_template_changes(sender, **kwargs): | ||
for directory in get_template_directories(): | ||
sender.watch_dir(directory, '**/*') | ||
|
||
|
||
@receiver(file_changed, dispatch_uid='template_loaders_file_changed') | ||
def template_changed(sender, file_path, **kwargs): | ||
for template_dir in get_template_directories(): | ||
if template_dir in file_path.parents: | ||
reset_loaders() | ||
return True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
from pathlib import Path | ||
from unittest import mock | ||
|
||
from django.template import autoreload | ||
from django.test import SimpleTestCase, override_settings | ||
from django.test.utils import require_jinja2 | ||
|
||
ROOT = Path(__file__).parent.absolute() | ||
EXTRA_TEMPLATES_DIR = ROOT / "templates_extra" | ||
|
||
|
||
@override_settings( | ||
INSTALLED_APPS=['template_tests'], | ||
TEMPLATES=[{ | ||
'BACKEND': 'django.template.backends.dummy.TemplateStrings', | ||
'APP_DIRS': True, | ||
}, { | ||
'BACKEND': 'django.template.backends.django.DjangoTemplates', | ||
'DIRS': [EXTRA_TEMPLATES_DIR], | ||
'OPTIONS': { | ||
'context_processors': [ | ||
'django.template.context_processors.request', | ||
], | ||
'loaders': [ | ||
'django.template.loaders.filesystem.Loader', | ||
'django.template.loaders.app_directories.Loader', | ||
] | ||
}, | ||
}]) | ||
class TemplateReloadTests(SimpleTestCase): | ||
@mock.patch('django.template.autoreload.reset_loaders') | ||
def test_template_changed(self, mock_reset): | ||
template_path = Path(__file__).parent / 'templates' / 'index.html' | ||
self.assertTrue(autoreload.template_changed(None, template_path)) | ||
mock_reset.assert_called_once() | ||
|
||
@mock.patch('django.template.autoreload.reset_loaders') | ||
def test_non_template_changed(self, mock_reset): | ||
self.assertIsNone(autoreload.template_changed(None, Path(__file__))) | ||
mock_reset.assert_not_called() | ||
|
||
def test_watch_for_template_changes(self): | ||
mock_reloader = mock.MagicMock() | ||
autoreload.watch_for_template_changes(mock_reloader) | ||
self.assertSequenceEqual( | ||
sorted(mock_reloader.watch_dir.call_args_list), | ||
[ | ||
mock.call(ROOT / 'templates', '**/*'), | ||
mock.call(ROOT / 'templates_extra', '**/*') | ||
] | ||
) | ||
|
||
def test_get_template_directories(self): | ||
self.assertSetEqual( | ||
autoreload.get_template_directories(), | ||
{ | ||
ROOT / 'templates_extra', | ||
ROOT / 'templates', | ||
} | ||
) | ||
|
||
@mock.patch('django.template.loaders.base.Loader.reset') | ||
def test_reset_all_loaders(self, mock_reset): | ||
autoreload.reset_loaders() | ||
self.assertEqual(mock_reset.call_count, 2) | ||
|
||
|
||
@require_jinja2 | ||
@override_settings(INSTALLED_APPS=['template_tests']) | ||
class Jinja2TemplateReloadTests(SimpleTestCase): | ||
def test_watch_for_template_changes(self): | ||
mock_reloader = mock.MagicMock() | ||
autoreload.watch_for_template_changes(mock_reloader) | ||
self.assertSequenceEqual( | ||
sorted(mock_reloader.watch_dir.call_args_list), | ||
[ | ||
mock.call(ROOT / 'templates', '**/*'), | ||
] | ||
) | ||
|
||
def test_get_template_directories(self): | ||
self.assertSetEqual( | ||
autoreload.get_template_directories(), | ||
{ | ||
ROOT / 'templates', | ||
} | ||
) | ||
|
||
@mock.patch('django.template.loaders.base.Loader.reset') | ||
def test_reset_all_loaders(self, mock_reset): | ||
autoreload.reset_loaders() | ||
self.assertEqual(mock_reset.call_count, 0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is definitely not the best way to invoke this module, but I really don't know how else to do it. This file is a kind of entrypoint to
django.template
submodules, so we don't want to pollute it if we can avoid it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So question, can the autoreloader take care of these registrations itself? (Haven't looked into it but...)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Part of the improvements I made to the autoreloader was to decouple it from registrations like this. The only one we have right now, prior to this, is the translation cache clearing. We used to have a function in the autoreload module that would handle the clearing of the cache, which has since been replaced with a signal based registration. I figured this was way better, as you just need to register a signal in any module and just have it work.
However I didn't consider modules where the module scope is effectively the API and adding any imports to it might cause issues down the line. I'd really like to avoid adding anything to the autoreloader specifically for this, I'd honestly prefer to do:
but I'm not sure how much you'd hate that 😂
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On consideration, I'd take the first bit of that...
(I'm reviewing now. I'll do it.)