From 986d6c2c372df1bf1962533e4a24080da4d79c8a Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Wed, 1 Apr 2026 12:37:31 +0200 Subject: ✨ general management of filter on types - filter qualification types for actors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ishtar_common/models_common.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'ishtar_common/models_common.py') diff --git a/ishtar_common/models_common.py b/ishtar_common/models_common.py index ba27b3bbc..117981e79 100644 --- a/ishtar_common/models_common.py +++ b/ishtar_common/models_common.py @@ -403,6 +403,7 @@ class GeneralType(Cached, models.Model): initial=None, force=False, full_hierarchy=False, + limit=None ): if not dct: dct = {} @@ -412,7 +413,8 @@ class GeneralType(Cached, models.Model): if not instances and empty_first and not default: types = [("", "--")] types += cls._pre_get_types( - dct, instances, exclude, default, force, get_full_hierarchy=full_hierarchy + dct, instances, exclude, default, force, get_full_hierarchy=full_hierarchy, + limit=limit ) if not initial: return types @@ -420,6 +422,13 @@ class GeneralType(Cached, models.Model): types += new_vals return types + @classmethod + def _set_limit(cls, dct, limit): + """ + Translate limit set in forms to a query filter + """ + raise NotImplementedError(f"_set_limit method must be set for {cls}") + @classmethod def _pre_get_types( cls, @@ -429,6 +438,7 @@ class GeneralType(Cached, models.Model): default=None, force=False, get_full_hierarchy=False, + limit=None ): if not dct: dct = {} @@ -439,11 +449,15 @@ class GeneralType(Cached, models.Model): if not instances: keys = ["__get_types"] keys += ["{}".format(ex) for ex in exclude] + ["{}".format(default)] + if limit: + keys.append("lim" + ";".join(limit)) keys += ["{}-{}".format(str(k), dct[k]) for k in dct] cache_key, value = get_cache(cls, keys) if value and not force: return value base_dct = dct.copy() + if limit: + cls._set_limit(base_dct, limit) if hasattr(cls, "parent"): if not cache_key: return cls._get_parent_types( @@ -467,7 +481,9 @@ class GeneralType(Cached, models.Model): return vals if not cache_key: - return cls._get_types(base_dct, instances, exclude=exclude, default=default) + return cls._get_types( + base_dct, instances, exclude=exclude, default=default + ) vals = [ v for v in cls._get_types( -- cgit v1.2.3