diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2022-03-08 15:53:42 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2022-03-08 15:53:42 +0100 |
commit | d09fc817de5ba4692182208da153abd5268ecdb0 (patch) | |
tree | 84234d84769ec37079c53e6327ebce9ce68904a2 /ishtar_common | |
parent | 3683f2d298206fb5a7cc341ab7af653706af23cf (diff) | |
download | Ishtar-d09fc817de5ba4692182208da153abd5268ecdb0.tar.bz2 Ishtar-d09fc817de5ba4692182208da153abd5268ecdb0.zip |
Templates: improve splitpart filter
Diffstat (limited to 'ishtar_common')
-rw-r--r-- | ishtar_common/tests.py | 5 | ||||
-rw-r--r-- | ishtar_common/utils_secretary.py | 24 | ||||
-rw-r--r-- | ishtar_common/version.py | 4 |
3 files changed, 27 insertions, 6 deletions
diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py index 90af3ac93..8ef083c9f 100644 --- a/ishtar_common/tests.py +++ b/ishtar_common/tests.py @@ -3095,8 +3095,13 @@ class JinjaFilterTest(TestCase): self.assertEqual(utils_secretary.splitpart("1,2,3", 1), "2") self.assertEqual(utils_secretary.splitpart("1,2,3", 10), "") self.assertEqual(utils_secretary.splitpart("", 10), "") + # old use self.assertEqual(utils_secretary.splitpart("1;2;3", 2, ";"), "3") self.assertEqual(utils_secretary.splitpart("1;2;3;4", 1, ";", True), "2;3;4") + # new filter use + self.assertEqual(utils_secretary.splitpart("1;2;3", 0, 2, ";"), "1;2") + self.assertEqual(utils_secretary.splitpart("1;2;3", 2, 0, ";"), "3") + self.assertEqual(utils_secretary.splitpart("1;2;3;4", 1, -1, ";", "|"), "2|3") def test_human_date(self): self.assertEqual(utils_secretary.human_date_filter("NODATE"), "") diff --git a/ishtar_common/utils_secretary.py b/ishtar_common/utils_secretary.py index 062b43913..eea688ea1 100644 --- a/ishtar_common/utils_secretary.py +++ b/ishtar_common/utils_secretary.py @@ -71,15 +71,31 @@ def human_date_filter(value): return value.strftime(settings.DATE_FORMAT) -def splitpart(value, index, char=",", merge_end=False): - if not value or not index: +def splitpart(value, index, index_end=None, char=",", merge_character=None): + if index_end: + try: + index_end = int(index_end) + if not merge_character: # merge is assumed + merge_character = char + except ValueError: + # old filter use - manage compatibility + merge_character = char + char = index_end + index_end = None + if not value or (not index and index != 0): return "" + if merge_character is True: # old filter use + merge_character = char splited = value.split(char) if len(splited) <= index: return "" - if not merge_end: + if not merge_character: return splited[index] - return char.join(splited[index:]) + if index_end: + splited = splited[index:index_end] + else: + splited = splited[index:] + return merge_character.join(splited) class IshtarSecretaryRenderer(Renderer): diff --git a/ishtar_common/version.py b/ishtar_common/version.py index b2f930812..830404ea5 100644 --- a/ishtar_common/version.py +++ b/ishtar_common/version.py @@ -1,5 +1,5 @@ -# 3.1.49 -VERSION = (3, 1, 49) +# 3.1.50 +VERSION = (3, 1, 50) def get_version(): |