From 97d8a692127ca9a7ec08cd525a1014538f635c2c Mon Sep 17 00:00:00 2001 From: Konrad du Plessis Date: Thu, 23 Apr 2026 14:54:50 +0200 Subject: [PATCH] feat(adjustments): add |type_slug template filter for badge class naming --- core/templatetags/format_tags.py | 16 ++++++++++++++++ core/tests.py | 25 +++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/core/templatetags/format_tags.py b/core/templatetags/format_tags.py index 5c2ec9b..7723980 100644 --- a/core/templatetags/format_tags.py +++ b/core/templatetags/format_tags.py @@ -27,6 +27,22 @@ def money(value): 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: ``. + Returns '' for None / empty — no class generated, no crash. + """ + if not value: + return '' + return value.lower().replace(' ', '-') + + @register.filter def dictlookup(d, key): """Look up a dict value by a dynamic key. diff --git a/core/tests.py b/core/tests.py index bfa25c2..340d270 100644 --- a/core/tests.py +++ b/core/tests.py @@ -912,3 +912,28 @@ class InlineFiltersPairsContextTests(TestCase): project_ids = {p.id for p in resp.context['projects']} self.assertIn(out_project.id, project_ids, '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')