Skip to content

Commit c34f7bd

Browse files
committed
Update the hook system to use Python 3 and Django 5 patterns instead of legacy patterns.
1 parent a8383ea commit c34f7bd

4 files changed

Lines changed: 15 additions & 21 deletions

File tree

docs/references/template-hooks.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Creating a hook listener in a `third_party_app`:
3333

3434
# Example 1
3535
def css_resources(context, *args, **kwargs):
36-
return mark_safe(u'<link rel="stylesheet" href="%s/app_hook/styles.css">' % settings.STATIC_URL)
36+
return mark_safe('<link rel="stylesheet" href="%s/app_hook/styles.css">' % settings.STATIC_URL)
3737

3838

3939
# Example 2
@@ -52,7 +52,8 @@ Creating a hook listener in a `third_party_app`:
5252
# If you are doing this a lot, make sure to keep your templates in memory (google: django.template.loaders.cached.Loader)
5353
return render_to_string(
5454
template_name='templates/app_hook/head_resources.html',
55-
context_instance=context
55+
context=dict(context),
56+
request=context.get('request'),
5657
)
5758

5859

@@ -61,8 +62,8 @@ Creating a hook listener in a `third_party_app`:
6162
articles = Article.objects.all()
6263
return render_to_string(
6364
template_name='templates/app_hook/my_articles.html',
64-
dictionary={'articles': articles, },
65-
context_instance=context
65+
context={'articles': articles},
66+
request=context.get('request'),
6667
)
6768

6869
Registering a hook listener in a `third_party_app`:
@@ -78,7 +79,7 @@ Registering a hook listener in a `third_party_app`:
7879
verbose_name = 'My App'
7980

8081
def ready(self):
81-
from hooks.templatehook import hook
82+
from hypha.core.templatehook import hook
8283
from third_party_app.template_hooks import css_resources
8384

8485
hook.register("within_head", css_resources)

hypha/core/templatehook.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
# Adds the ability to have hooks injected into templates, via the `hooks_tags.hook` template tag.
22
#
33
# Originally from https://github.com/nitely/django-hooks but after culling everything except
4-
# the template hook. See /docs/templatehook.rst for more information
4+
# the template hook. See docs/references/template-hooks.md for more information
55
#
66
# The reason this was forked, rather than used as a module, is that the last commit from the
77
# other project was made in 2015, and can no longer be imported into a modern django project.
88
# The template hook continues to work correctly, but the other hooks most likely do not, and
99
# so were removed.
1010

1111

12-
class TemplateHook(object):
12+
class TemplateHook:
1313
"""
1414
A hook for templates. This can be used directly or\
1515
through the :py:class:`Hook` dispatcher
16-
17-
:param list providing_args: A list of the arguments\
18-
this hook can pass along in a :py:func:`.__call__`
1916
"""
2017

21-
def __init__(self, providing_args=None):
22-
self.providing_args = providing_args or []
18+
def __init__(self):
2319
self._registry = []
2420

2521
def __call__(self, *args, **kwargs):
@@ -38,7 +34,8 @@ def register(self, func):
3834
3935
:param callable func: A function reference used as a callback
4036
"""
41-
assert callable(func), "Callback func must be a callable"
37+
if not callable(func):
38+
raise TypeError("Callback func must be a callable")
4239

4340
self._registry.append(func)
4441

@@ -58,10 +55,10 @@ def unregister_all(self):
5855
"""
5956
Remove all callbacks
6057
"""
61-
del self._registry[:]
58+
self._registry.clear()
6259

6360

64-
class Hook(object):
61+
class Hook:
6562
"""
6663
Dynamic dispatcher (proxy) for :py:class:`TemplateHook`
6764
"""

hypha/core/templatetags/hooks_tags.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def template_hook_collect(module, hook_name, *args, **kwargs):
3636
Example::
3737
3838
import myhooks
39-
from hooks.templatetags import template_hook_collect
39+
from hypha.core.templatetags.hooks_tags import template_hook_collect
4040
4141
@register.simple_tag(takes_context=True)
4242
def hook(context, name, *args, **kwargs):

hypha/core/tests/test_templatehook.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,9 @@ def test_unregister_all_clears_registry(self):
4343
self.assertEqual(self.hook(), [])
4444

4545
def test_register_non_callable_raises(self):
46-
with self.assertRaises(AssertionError):
46+
with self.assertRaises(TypeError):
4747
self.hook.register("not_callable")
4848

49-
def test_providing_args_stored(self):
50-
hook = TemplateHook(providing_args=["foo", "bar"])
51-
self.assertEqual(hook.providing_args, ["foo", "bar"])
52-
5349

5450
class TestHook(SimpleTestCase):
5551
def setUp(self):

0 commit comments

Comments
 (0)