From bc5ae52edd6cf28ad47efdd46bbabda2e38072aa Mon Sep 17 00:00:00 2001 From: Christophe Gerard Date: Wed, 18 Dec 2024 11:35:06 +0100 Subject: [PATCH] fix setuper, show unit count on detail --- iaso/api/org_unit_types/serializers.py | 34 ++++++++++------------- iaso/api/org_unit_types/viewsets.py | 10 +++++++ setuper/create_submission_with_picture.py | 2 +- setuper/data_collection.py | 2 +- setuper/org_unit_pictures.py | 2 +- 5 files changed, 28 insertions(+), 22 deletions(-) diff --git a/iaso/api/org_unit_types/serializers.py b/iaso/api/org_unit_types/serializers.py index 2eb301630f..91e901fcac 100644 --- a/iaso/api/org_unit_types/serializers.py +++ b/iaso/api/org_unit_types/serializers.py @@ -80,15 +80,13 @@ class Meta: # Fixme make this directly in db ! def get_units_count(self, obj: OrgUnitType): - # Skip computation if the parameter is not present - if not self.context["request"].query_params.get("with_units_count"): - return None - - orgUnits = OrgUnit.objects.filter_for_user_and_app_id( - self.context["request"].user, self.context["request"].query_params.get("app_id") - ).filter(Q(validated=True) & Q(org_unit_type__id=obj.id)) - orgunits_count = orgUnits.count() - return orgunits_count + # Show count if it's a detail view OR if with_units_count parameter is present + if self.context.get('view_action') == 'retrieve' or self.context["request"].query_params.get("with_units_count"): + orgUnits = OrgUnit.objects.filter_for_user_and_app_id( + self.context["request"].user, self.context["request"].query_params.get("app_id") + ).filter(Q(validated=True) & Q(org_unit_type__id=obj.id)) + return orgUnits.count() + return None def get_sub_unit_types(self, obj: OrgUnitType): # Filter sub unit types to show only visible items for the current app id @@ -190,15 +188,13 @@ class Meta: # Fixme make this directly in db ! def get_units_count(self, obj: OrgUnitType): - # Skip computation if the parameter is not present - if not self.context["request"].query_params.get("with_units_count"): - return None - - orgUnits = OrgUnit.objects.filter_for_user_and_app_id( - self.context["request"].user, self.context["request"].query_params.get("app_id") - ).filter(Q(validation_status=OrgUnit.VALIDATION_VALID) & Q(org_unit_type__id=obj.id)) - orgunits_count = orgUnits.count() - return orgunits_count + # Show count if it's a detail view OR if with_units_count parameter is present + if self.context.get('view_action') == 'retrieve' or self.context["request"].query_params.get("with_units_count"): + orgUnits = OrgUnit.objects.filter_for_user_and_app_id( + self.context["request"].user, self.context["request"].query_params.get("app_id") + ).filter(Q(validation_status=OrgUnit.VALIDATION_VALID) & Q(org_unit_type__id=obj.id)) + return orgUnits.count() + return None def get_reference_forms(self, obj: OrgUnitType): return FormSerializer( @@ -261,7 +257,7 @@ def validate(self, data: typing.Mapping): def to_representation(self, instance): # Remove units_count from fields if not requested - if not self.context["request"].query_params.get("with_units_count"): + if not self.context.get('view_action') == 'retrieve' and not self.context["request"].query_params.get("with_units_count"): self.fields.pop("units_count", None) return super().to_representation(instance) diff --git a/iaso/api/org_unit_types/viewsets.py b/iaso/api/org_unit_types/viewsets.py index 0337fa47d6..30548b6757 100644 --- a/iaso/api/org_unit_types/viewsets.py +++ b/iaso/api/org_unit_types/viewsets.py @@ -45,6 +45,11 @@ def get_queryset(self): return queryset.order_by("depth").distinct().order_by(*orders) + def get_serializer_context(self): + context = super().get_serializer_context() + context['view_action'] = self.action + return context + class OrgUnitTypeViewSetV2(ModelViewSet): """Org unit types API @@ -101,3 +106,8 @@ def dropdown(self, request, *args): serializer = self.get_serializer(queryset, many=True) return Response(serializer.data) + + def get_serializer_context(self): + context = super().get_serializer_context() + context['view_action'] = self.action + return context diff --git a/setuper/create_submission_with_picture.py b/setuper/create_submission_with_picture.py index d43f7d5fc0..a64af4a39b 100644 --- a/setuper/create_submission_with_picture.py +++ b/setuper/create_submission_with_picture.py @@ -10,7 +10,7 @@ def define_health_facility_reference_form(iaso_client): - org_unit_types = iaso_client.get("/api/v2/orgunittypes/")["orgUnitTypes"] + org_unit_types = iaso_client.get("/api/v2/orgunittypes/?with_units_count=true")["orgUnitTypes"] health_facility_type = [out for out in org_unit_types if out["name"] == "Health facility/Formation sanitaire - HF"][ 0 ] diff --git a/setuper/data_collection.py b/setuper/data_collection.py index fc36dfb9d9..2453a3a54d 100644 --- a/setuper/data_collection.py +++ b/setuper/data_collection.py @@ -12,7 +12,7 @@ def setup_instances(account_name, iaso_client): print("-- Setting up a form") project_id = iaso_client.get("/api/projects/")["projects"][0]["id"] - org_unit_types = iaso_client.get("/api/v2/orgunittypes/")["orgUnitTypes"] + org_unit_types = iaso_client.get("/api/v2/orgunittypes/?with_units_count=true")["orgUnitTypes"] org_unit_type_ids = [ out["id"] for out in org_unit_types if out["name"] != "Health facility/Formation sanitaire - HF" ] diff --git a/setuper/org_unit_pictures.py b/setuper/org_unit_pictures.py index d9f90e2a6e..89299b34cb 100644 --- a/setuper/org_unit_pictures.py +++ b/setuper/org_unit_pictures.py @@ -2,7 +2,7 @@ def associate_favorite_picture(iaso_client): - org_unit_types = iaso_client.get("/api/v2/orgunittypes/")["orgUnitTypes"] + org_unit_types = iaso_client.get("/api/v2/orgunittypes/?with_units_count=true")["orgUnitTypes"] for org_unit_type in org_unit_types: orgunits = iaso_client.get(