Skip to content

Commit 363fea7

Browse files
authored
fix: Group filter doesn't seem to work on OU list
Refs: IA-3996
2 parents c93b8dd + 1e802dc commit 363fea7

File tree

2 files changed

+219
-63
lines changed

2 files changed

+219
-63
lines changed

iaso/api/org_unit_search.py

+27-9
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ def build_org_units_queryset(queryset, params, profile):
6969
queryset = queryset.filter(source_ref__in=[])
7070
print("Failed parsing refs in search", search)
7171
else:
72-
queryset = queryset.filter(Q(name__icontains=search) | Q(aliases__contains=[search]))
72+
queryset = queryset.filter(
73+
Q(name__icontains=search) | Q(aliases__contains=[search])
74+
)
7375

7476
if group:
7577
if isinstance(group, str):
@@ -78,7 +80,7 @@ def build_org_units_queryset(queryset, params, profile):
7880
group_ids = [group]
7981
else:
8082
group_ids = group
81-
queryset = queryset.filter(groups__in=group_ids)
83+
queryset = queryset.filter(groups__in=group_ids).distinct("id")
8284

8385
if source:
8486
source = DataSource.objects.get(id=source)
@@ -103,7 +105,9 @@ def build_org_units_queryset(queryset, params, profile):
103105
queryset = queryset.filter(instance__created_at__lte=date_to)
104106

105107
if date_from is not None and date_to is not None:
106-
queryset = queryset.filter(instance__created_at__range=[date_from, date_to]).distinct("id")
108+
queryset = queryset.filter(
109+
instance__created_at__range=[date_from, date_to]
110+
).distinct("id")
107111

108112
if has_instances is not None:
109113
if has_instances == "true":
@@ -138,13 +142,17 @@ def build_org_units_queryset(queryset, params, profile):
138142
# We need a few things for empty location comparisons:
139143
# 1. An annotated queryset (geography fields exposed as geometries)
140144
queryset = queryset.annotate(location_as_geom=Cast("location", PointField(dim=3)))
141-
queryset = queryset.annotate(simplified_geom_as_geom=Cast("simplified_geom", MultiPolygonField()))
145+
queryset = queryset.annotate(
146+
simplified_geom_as_geom=Cast("simplified_geom", MultiPolygonField())
147+
)
142148
# 2. Empty features to compare to
143149
empty_point = GEOSGeometry("POINT EMPTY", srid=4326)
144150
empty_multipolygon = GEOSGeometry("MULTIPOLYGON EMPTY", srid=4326)
145151

146152
has_location = Q(location__isnull=False) & (~Q(location_as_geom=empty_point))
147-
has_simplified_geom = Q(simplified_geom__isnull=False) & (~Q(simplified_geom_as_geom=empty_multipolygon))
153+
has_simplified_geom = Q(simplified_geom__isnull=False) & (
154+
~Q(simplified_geom_as_geom=empty_multipolygon)
155+
)
148156

149157
if geography == "location":
150158
queryset = queryset.filter(has_location)
@@ -209,17 +217,23 @@ def build_org_units_queryset(queryset, params, profile):
209217
queryset = queryset.filter(sub_source=source_id)
210218

211219
if org_unit_type_category:
212-
queryset = queryset.filter(org_unit_type__category=org_unit_type_category.upper())
220+
queryset = queryset.filter(
221+
org_unit_type__category=org_unit_type_category.upper()
222+
)
213223

214224
if ignore_empty_names:
215225
queryset = queryset.filter(~Q(name=""))
216226

217227
if path_depth is not None:
218228
queryset = queryset.filter(path__depth=path_depth)
219229
if opening_date:
220-
queryset = queryset.filter(opening_date=datetime.strptime(opening_date, "%d-%m-%Y").date())
230+
queryset = queryset.filter(
231+
opening_date=datetime.strptime(opening_date, "%d-%m-%Y").date()
232+
)
221233
if closed_date:
222-
queryset = queryset.filter(closed_date=datetime.strptime(closed_date, "%d-%m-%Y").date())
234+
queryset = queryset.filter(
235+
closed_date=datetime.strptime(closed_date, "%d-%m-%Y").date()
236+
)
223237
if not direct_children:
224238
queryset = queryset.exclude(pk=org_unit_parent_id)
225239

@@ -238,7 +252,11 @@ def annotate_query(queryset, count_instances, count_per_form, forms):
238252
queryset = queryset.annotate(
239253
instances_count=Count(
240254
"instance",
241-
filter=(~Q(instance__file="") & ~Q(instance__device__test_device=True) & ~Q(instance__deleted=True)),
255+
filter=(
256+
~Q(instance__file="")
257+
& ~Q(instance__device__test_device=True)
258+
& ~Q(instance__deleted=True)
259+
),
242260
)
243261
)
244262

0 commit comments

Comments
 (0)