Skip to content

Commit e2b081a

Browse files
authored
Fix error handling in template tag (#138)
1 parent 3f5bd2f commit e2b081a

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

solo/templatetags/solo_tags.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ def get_solo(model_path: str) -> SingletonModel:
1919
"Received '{model_path}'."
2020
).format(model_path=model_path)
2121
)
22-
model_class: type[SingletonModel] = apps.get_model(app_label, model_name)
23-
if not model_class:
22+
try:
23+
model_class: type[SingletonModel] = apps.get_model(app_label, model_name)
24+
except LookupError:
2425
raise template.TemplateSyntaxError(
2526
_("Could not get the model name '{model}' from the application named '{app}'").format(
2627
model=model_name, app=app_label

solo/tests/tests.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from django.core.cache import caches
22
from django.core.files.uploadedfile import SimpleUploadedFile
3-
from django.template import Context, Template
3+
from django.template import Context, Template, TemplateSyntaxError
44
from django.test import TestCase
55
from django.test.utils import override_settings
66

@@ -16,6 +16,18 @@ def setUp(self):
1616
"{{ site_config.site_name }}"
1717
"{{ site_config.file.url }}"
1818
)
19+
self.template_invalid_app = Template(
20+
"{% load solo_tags %}"
21+
'{% get_solo "invalid_app.SiteConfiguration" as site_config %}'
22+
"{{ site_config.site_name }}"
23+
"{{ site_config.file.url }}"
24+
)
25+
self.template_invalid_model = Template(
26+
"{% load solo_tags %}"
27+
'{% get_solo "tests.InvalidModel" as site_config %}'
28+
"{{ site_config.site_name }}"
29+
"{{ site_config.file.url }}"
30+
)
1931
self.cache = caches["default"]
2032
self.cache_key = SiteConfiguration.get_cache_key()
2133
self.cache.clear()
@@ -96,6 +108,14 @@ def test_cache_prefix_overriding(self):
96108
prefix = key.partition(":")[0]
97109
self.assertEqual(prefix, "other")
98110

111+
def test_template_tag_invalid_app_name(self):
112+
with self.assertRaises(TemplateSyntaxError):
113+
self.template_invalid_app.render(Context())
114+
115+
def test_template_invalid_model_name(self):
116+
with self.assertRaises(TemplateSyntaxError):
117+
self.template_invalid_model.render(Context())
118+
99119

100120
class SingletonWithExplicitIdTest(TestCase):
101121
def setUp(self):

0 commit comments

Comments
 (0)