From 25f8cc3a99bd3464d3c5799abca6ec8b7f28d9d2 Mon Sep 17 00:00:00 2001 From: Fred C <42280769+hypsug0@users.noreply.github.com> Date: Wed, 30 Aug 2023 10:46:13 +0200 Subject: [PATCH] fix #40 --- src/utils_flask_sqla/generic.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/utils_flask_sqla/generic.py b/src/utils_flask_sqla/generic.py index d98a1b3..b29985a 100644 --- a/src/utils_flask_sqla/generic.py +++ b/src/utils_flask_sqla/generic.py @@ -237,10 +237,14 @@ def build_query_order(self, query, parameters): return query.order_by(ordel_col) return query - def raw_query(self, process_filter=True): + def set_limit(self, q): + return q.limit(self.limit).offset(self.offset * self.limit) + + def raw_query(self, process_filter=True, with_limit=True): """ Renvoie la requete 'brute' (sans .all) - process_filter: application des filtres (et du sort) + - with_limit: application de la limite sur la query """ q = self.DB.session.query(self.view.tableDef) @@ -251,8 +255,8 @@ def raw_query(self, process_filter=True): unordered_q = self.build_query_filters(q, self.filters) q = self.build_query_order(unordered_q, self.filters) - if self.limit != -1: - q = q.limit(self.limit).offset(self.offset * self.limit) + if self.limit != -1 and with_limit: + q = self.set_limit(q) return q @@ -260,15 +264,15 @@ def query(self): """ Lance la requete et retourne l'objet sqlalchemy """ - q = self.raw_query(process_filter=False) + q = self.DB.session.query(self.view.tableDef) nb_result_without_filter = q.count() - q = self.raw_query() - nb_results = q.count() if self.filters else nb_result_without_filter + q = self.raw_query(process_filter=True, with_limit=False) + total_filtered = q.count() if self.filters else nb_result_without_filter - data = q.all() + data = self.set_limit(q).all() - return data, nb_result_without_filter, nb_results + return data, nb_result_without_filter, total_filtered def return_query(self): """