Skip to content

Commit

Permalink
Fix error handling in template tag (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos authored Oct 19, 2024
1 parent 3f5bd2f commit e2b081a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
5 changes: 3 additions & 2 deletions solo/templatetags/solo_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ def get_solo(model_path: str) -> SingletonModel:
"Received '{model_path}'."
).format(model_path=model_path)
)
model_class: type[SingletonModel] = apps.get_model(app_label, model_name)
if not model_class:
try:
model_class: type[SingletonModel] = apps.get_model(app_label, model_name)
except LookupError:
raise template.TemplateSyntaxError(
_("Could not get the model name '{model}' from the application named '{app}'").format(
model=model_name, app=app_label
Expand Down
22 changes: 21 additions & 1 deletion solo/tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.core.cache import caches
from django.core.files.uploadedfile import SimpleUploadedFile
from django.template import Context, Template
from django.template import Context, Template, TemplateSyntaxError
from django.test import TestCase
from django.test.utils import override_settings

Expand All @@ -16,6 +16,18 @@ def setUp(self):
"{{ site_config.site_name }}"
"{{ site_config.file.url }}"
)
self.template_invalid_app = Template(
"{% load solo_tags %}"
'{% get_solo "invalid_app.SiteConfiguration" as site_config %}'
"{{ site_config.site_name }}"
"{{ site_config.file.url }}"
)
self.template_invalid_model = Template(
"{% load solo_tags %}"
'{% get_solo "tests.InvalidModel" as site_config %}'
"{{ site_config.site_name }}"
"{{ site_config.file.url }}"
)
self.cache = caches["default"]
self.cache_key = SiteConfiguration.get_cache_key()
self.cache.clear()
Expand Down Expand Up @@ -96,6 +108,14 @@ def test_cache_prefix_overriding(self):
prefix = key.partition(":")[0]
self.assertEqual(prefix, "other")

def test_template_tag_invalid_app_name(self):
with self.assertRaises(TemplateSyntaxError):
self.template_invalid_app.render(Context())

def test_template_invalid_model_name(self):
with self.assertRaises(TemplateSyntaxError):
self.template_invalid_model.render(Context())


class SingletonWithExplicitIdTest(TestCase):
def setUp(self):
Expand Down

0 comments on commit e2b081a

Please sign in to comment.