Skip to content

Commit 46df5de

Browse files
committed
Set disable_collection_count and default_collection_count in alchemy's post_init
1 parent e51c676 commit 46df5de

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

flask_rest_jsonapi/data_layers/alchemy.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,37 @@ def __init__(self, kwargs):
5050
f"You must provide a model in data_layer_kwargs to use sqlalchemy data layer in {self.resource.__name__}"
5151
)
5252

53+
self.disable_collection_count: bool = False
54+
self.default_collection_count: int = -1
55+
56+
def post_init(self):
57+
"""
58+
Checking some props here
59+
:return:
60+
"""
61+
if self.resource is None:
62+
# if working outside the resource, it's not assigned here
63+
return
64+
65+
if not hasattr(self.resource, "disable_collection_count") or self.resource.disable_collection_count is False:
66+
return
67+
68+
params = self.resource.disable_collection_count
69+
70+
if isinstance(params, (bool, int)):
71+
self.disable_collection_count = bool(params)
72+
73+
if isinstance(params, (tuple, list)):
74+
try:
75+
self.disable_collection_count, self.default_collection_count = params
76+
except ValueError:
77+
raise ValueError(
78+
"Resource's attribute `disable_collection_count` "
79+
"has to be bool or list/tuple with exactly 2 values!\n"
80+
"For example `disable_collection_count = (True, 999)`"
81+
)
82+
# just ignoring other types, we don't know how to process them
83+
5384
def create_object(self, data, view_kwargs):
5485
"""Create an object through sqlalchemy
5586
@@ -153,6 +184,9 @@ def get_collection_count(self, query, qs, view_kwargs) -> int:
153184
:param view_kwargs: view kwargs
154185
:return:
155186
"""
187+
if self.disable_collection_count is True:
188+
return self.default_collection_count
189+
156190
return query.count()
157191

158192
def get_collection(self, qs, view_kwargs):

0 commit comments

Comments
 (0)