diff --git a/hat/webpack.dev.js b/hat/webpack.dev.js index 65c2183dd8..cd1f57d215 100644 --- a/hat/webpack.dev.js +++ b/hat/webpack.dev.js @@ -183,13 +183,28 @@ module.exports = { }, host: '0.0.0.0', port: 3000, - + hot: true, devMiddleware: { writeToDisk: true, }, + static: { + directory: path.join(__dirname, 'assets'), + publicPath: '/', + }, + client: { + overlay: true, + progress: true, + }, + watchFiles: { + paths: ['src/**/*', 'assets/**/*'], + options: { + usePolling: true, + }, + }, }, plugins: [ + new webpack.HotModuleReplacementPlugin(), new CleanWebpackPlugin(), new webpack.NormalModuleReplacementPlugin( /^__intl\/messages\/en$/, diff --git a/iaso/api/org_unit_types/serializers.py b/iaso/api/org_unit_types/serializers.py index 2eb301630f..3f30e5f2a7 100644 --- a/iaso/api/org_unit_types/serializers.py +++ b/iaso/api/org_unit_types/serializers.py @@ -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 @@ -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( @@ -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) diff --git a/iaso/api/org_unit_types/viewsets.py b/iaso/api/org_unit_types/viewsets.py index 0337fa47d6..be24ae987f 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(