Skip to content

Commit 49c39b3

Browse files
committed
Render class name via custom template filter
``` In [4]: n.get_message().get_rendered_body() Out[4]: '\n\n\nConfig validation error in <code></code>.\nExpected a list, got type <code>int</code> (<code>422</code>).' ```
1 parent 3073702 commit 49c39b3

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

readthedocs/config/notifications.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@
249249
textwrap.dedent(
250250
"""
251251
Config validation error in <code>{{key}}</code>.
252-
Expected one of <code>[0, 1, true, false]</code>, got type <code>{{value.__class__}}</code> (<code>{{value}}</code>).
252+
Expected one of <code>[0, 1, true, false]</code>, got type <code>{{value|to_class_name}}</code> (<code>{{value}}</code>).
253253
Make sure the type of the value is not a string.
254254
"""
255255
).strip(),
@@ -263,7 +263,7 @@
263263
textwrap.dedent(
264264
"""
265265
Config validation error in <code>{{key}}</code>.
266-
Expected one of ({{choices}}), got type <code>{{value.__class__}}</code> (<code>{{value}}</code>).
266+
Expected one of ({{choices}}), got type <code>{{value|to_class_name}}</code> (<code>{{value}}</code>).
267267
Double check the type of the value.
268268
A string may be required (e.g. <code>"3.10"</code> insted of <code>3.10</code>)
269269
"""
@@ -278,7 +278,7 @@
278278
textwrap.dedent(
279279
"""
280280
Config validation error in <code>{{key}}</code>.
281-
Expected a dictionary, got type <code>{{value.__class__}}</code> (<code>{{value}}</code>).
281+
Expected a dictionary, got type <code>{{value|to_class_name}}</code> (<code>{{value}}</code>).
282282
"""
283283
).strip(),
284284
),
@@ -317,7 +317,7 @@
317317
textwrap.dedent(
318318
"""
319319
Config validation error in <code>{{key}}</code>.
320-
Expected a string, got type <code>{{value.__class__}}</code> (<code>{{value}}</code>).
320+
Expected a string, got type <code>{{value|to_class_name}}</code> (<code>{{value}}</code>).
321321
"""
322322
).strip(),
323323
),
@@ -330,7 +330,7 @@
330330
textwrap.dedent(
331331
"""
332332
Config validation error in <code>{{key}}</code>.
333-
Expected a list, got type <code>{{value.__class__}}</code> (<code>{{value}}</code>).
333+
Expected a list, got type <code>{{value|to_class_name}}</code> (<code>{{value}}</code>).
334334
"""
335335
).strip(),
336336
),

readthedocs/notifications/messages.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,21 @@ def get_display_icon_classes(self):
5858

5959
return " ".join(classes)
6060

61+
def _prepend_template_prefix(self, template):
62+
"""
63+
Prepend Django {% load %} template tag.
64+
65+
This is required to render the notifications with custom filters/tags.
66+
"""
67+
prefix = "{% load notifications_filters %}"
68+
return prefix + "\n\n\n" + template
69+
6170
def get_rendered_header(self):
62-
template = Template(self.header)
71+
template = Template(self._prepend_template_prefix(self.header))
6372
return template.render(context=Context(self.format_values))
6473

6574
def get_rendered_body(self):
66-
template = Template(self.body)
75+
template = Template(self._prepend_template_prefix(self.body))
6776
return template.render(context=Context(self.format_values))
6877

6978

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from django import template
2+
3+
register = template.Library()
4+
5+
6+
@register.filter
7+
def to_class_name(value):
8+
"""Output the name of the class for the given object."""
9+
return value.__class__.__name__

0 commit comments

Comments
 (0)