feat(adjustments): add |type_slug template filter for badge class naming
This commit is contained in:
parent
cf82215511
commit
97d8a69212
@ -27,6 +27,22 @@ def money(value):
|
|||||||
return formatted.replace(",", " ")
|
return formatted.replace(",", " ")
|
||||||
|
|
||||||
|
|
||||||
|
# === type_slug filter ===
|
||||||
|
# Converts adjustment-type labels like "Advance Payment" to
|
||||||
|
# CSS-class-friendly slugs like "advance-payment". Used by the Adjustments
|
||||||
|
# tab to pick the right colour badge class per row.
|
||||||
|
@register.filter
|
||||||
|
def type_slug(value):
|
||||||
|
"""Return a hyphen-separated lowercase version of `value`.
|
||||||
|
|
||||||
|
Used in templates: `<span class="badge-type-{{ adj.type|type_slug }}">`.
|
||||||
|
Returns '' for None / empty — no class generated, no crash.
|
||||||
|
"""
|
||||||
|
if not value:
|
||||||
|
return ''
|
||||||
|
return value.lower().replace(' ', '-')
|
||||||
|
|
||||||
|
|
||||||
@register.filter
|
@register.filter
|
||||||
def dictlookup(d, key):
|
def dictlookup(d, key):
|
||||||
"""Look up a dict value by a dynamic key.
|
"""Look up a dict value by a dynamic key.
|
||||||
|
|||||||
@ -912,3 +912,28 @@ class InlineFiltersPairsContextTests(TestCase):
|
|||||||
project_ids = {p.id for p in resp.context['projects']}
|
project_ids = {p.id for p in resp.context['projects']}
|
||||||
self.assertIn(out_project.id, project_ids,
|
self.assertIn(out_project.id, project_ids,
|
||||||
'URL-selected project must survive the date-scope filter')
|
'URL-selected project must survive the date-scope filter')
|
||||||
|
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# === TESTS FOR |type_slug FILTER ===
|
||||||
|
# Used by Adjustments tab to build CSS class names from type labels.
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
|
||||||
|
class TypeSlugFilterTests(TestCase):
|
||||||
|
"""format_tags.type_slug converts adjustment-type labels to slugs."""
|
||||||
|
|
||||||
|
def test_spaces_become_hyphens_and_lowercased(self):
|
||||||
|
from core.templatetags.format_tags import type_slug
|
||||||
|
self.assertEqual(type_slug('Advance Payment'), 'advance-payment')
|
||||||
|
self.assertEqual(type_slug('New Loan'), 'new-loan')
|
||||||
|
self.assertEqual(type_slug('Bonus'), 'bonus')
|
||||||
|
|
||||||
|
def test_empty_or_none_returns_empty_string(self):
|
||||||
|
from core.templatetags.format_tags import type_slug
|
||||||
|
self.assertEqual(type_slug(''), '')
|
||||||
|
self.assertEqual(type_slug(None), '')
|
||||||
|
|
||||||
|
def test_idempotent_on_already_slugged(self):
|
||||||
|
from core.templatetags.format_tags import type_slug
|
||||||
|
self.assertEqual(type_slug('bonus'), 'bonus')
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user