Skip to content

Commit

Permalink
fix: setuper using units_count
Browse files Browse the repository at this point in the history
  • Loading branch information
beygorghor authored Dec 18, 2024
2 parents 8e14c0e + 962eda4 commit 630d8cc
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 22 deletions.
40 changes: 21 additions & 19 deletions iaso/api/org_unit_types/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ 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
Expand Down Expand Up @@ -190,15 +190,15 @@ 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(
Expand Down Expand Up @@ -261,7 +261,9 @@ 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)

Expand Down
10 changes: 10 additions & 0 deletions iaso/api/org_unit_types/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion setuper/create_submission_with_picture.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
Expand Down
2 changes: 1 addition & 1 deletion setuper/data_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
Expand Down
2 changes: 1 addition & 1 deletion setuper/org_unit_pictures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 630d8cc

Please sign in to comment.