Skip to content

Commit 1ca1ed1

Browse files
committed
fix(spp_demo): optimize _get_leaf_areas to query only leaf areas
Replace the Python-level filter over all areas with a direct ORM query using the domain [('child_ids', '=', False)], which lets the database find leaf areas in a single SQL query instead of loading all records into memory.
1 parent a9c2e5e commit 1ca1ed1

1 file changed

Lines changed: 6 additions & 12 deletions

File tree

spp_demo/models/demo_data_generator.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,7 @@ def generate_demo_data(self):
168168
self.members_range_from = self._default_members_range_from()
169169
self.members_range_to = self._default_members_range_to()
170170
raise ValidationError(
171-
"Members per Group (From) cannot be greater than Members per Group (To)."
172-
" Resetting to default values."
171+
"Members per Group (From) cannot be greater than Members per Group (To). Resetting to default values."
173172
)
174173

175174
self.state = "in_progress"
@@ -371,18 +370,13 @@ def _get_leaf_areas(self):
371370
recordset: spp.area records at the deepest available level
372371
"""
373372
Area = self.env["spp.area"]
374-
all_areas = Area.search([])
375-
if not all_areas:
376-
return Area.browse()
377373

378-
# Find the maximum area_level (deepest in hierarchy)
379-
max_level = max(all_areas.mapped("area_level"))
374+
# Query only leaf areas directly: areas that have no children.
375+
# This is a single SQL query rather than loading all areas into memory.
376+
leaf_areas = Area.search([("child_ids", "=", False)])
380377

381-
# Get areas at the deepest level
382-
leaf_areas = all_areas.filtered(lambda a: a.area_level == max_level)
383-
384-
# Fallback: if no areas at max level, use all areas
385-
return leaf_areas if leaf_areas else all_areas
378+
# Fallback: if no leaf areas found, return all areas
379+
return leaf_areas if leaf_areas else Area.search([])
386380

387381
def get_group_vals(self, fake):
388382
registration_date = self.get_random_date(

0 commit comments

Comments
 (0)