summaryrefslogtreecommitdiff
path: root/ishtar_common/models_common.py
diff options
context:
space:
mode:
Diffstat (limited to 'ishtar_common/models_common.py')
-rw-r--r--ishtar_common/models_common.py20
1 files changed, 18 insertions, 2 deletions
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
@@ -421,6 +423,13 @@ class GeneralType(Cached, models.Model):
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,
dct=None,
@@ -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(