Repository: WeblateOrg/weblate

Issue:
No /statistics REST API for categories
### Describe the problem

The UI is displaying a nice view to show how much a language is translated for a given category:
/projects/:project/:category/#languages

However the REST API does not provide such route.

### Describe the solution you'd like

I guess we will need a new route GET /categories/:category_id/statistics/

### Describe alternatives you've considered

I am currently web scrapping from the UI.

### Screenshots

_No response_

### Additional context

_No response_


Discussion:
This issue seems to be a good fit for newbie contributors. You are welcome to contribute to Weblate! Don't hesitate to ask any questions you would have while implementing this.

You can learn about how to get started in our [contributors documentation](https://docs.weblate.org/en/latest/contributing/index.html).
I'd like to work on this good first issue.

From my initial observation of the code base, I see that the `views.py` provides several components with the statistics endpoint exposed.  Are these statistics you mention the same generic statistics as the other components have?
Yes, the missing but is `statistics` endpoint on `CategoryViewSet`.

Resolving patch:
diff --git a/weblate/api/serializers.py b/weblate/api/serializers.py
--- a/weblate/api/serializers.py
+++ b/weblate/api/serializers.py
@@ -1168,15 +1168,21 @@ class CategorySerializer(RemovableSerializer):
         queryset=Category.objects.none(),
         required=False,
     )
+    statistics_url = serializers.HyperlinkedIdentityField(
+        view_name="api:category-statistics",
+        lookup_field="pk",
+    )
 
     class Meta:
         model = Category
         fields = (
+            "id",
             "name",
             "slug",
             "project",
             "category",
             "url",
+            "statistics_url",
         )
         extra_kwargs = {"url": {"view_name": "api:category-detail"}}
 
diff --git a/weblate/api/views.py b/weblate/api/views.py
--- a/weblate/api/views.py
+++ b/weblate/api/views.py
@@ -1701,6 +1701,14 @@ def perform_update(self, serializer):
         serializer.instance.acting_user = self.request.user
         serializer.save()
 
+    @action(detail=True, methods=["get"])
+    def statistics(self, request, **kwargs):
+        obj = self.get_object()
+
+        serializer = StatisticsSerializer(obj, context={"request": request})
+
+        return Response(serializer.data)
+
 
 class Metrics(APIView):
     """Metrics view for monitoring."""


Test patch:
diff --git a/weblate/api/tests.py b/weblate/api/tests.py
--- a/weblate/api/tests.py
+++ b/weblate/api/tests.py
@@ -4128,6 +4128,14 @@ def test_component(self):
         for translation in response.data["results"]:
             self.do_request(translation["url"])
 
+    def test_statistics(self):
+        # Create a category to get the statistics from
+        response = self.create_category()
+        category_kwargs = {"pk": response.data["id"]}
+        # Use the default category kwargs to get the statistics
+        request = self.do_request("api:category-statistics", category_kwargs)
+        self.assertEqual(request.data["total"], 0)
+
 
 class LabelAPITest(APIBaseTest):
     def test_get_label(self):