[damned-lies] [api] Refs #257 - Add API for module/branch with all language stats



commit 4e7e6f4b94a2e347927a8506f51bb9301b83332c
Author: Claude Paroz <claude 2xlibre net>
Date:   Wed Sep 8 22:35:44 2021 +0200

    [api] Refs #257 - Add API for module/branch with all language stats

 api/tests.py | 22 ++++++++++++++++++++++
 api/urls.py  |  5 +++++
 api/views.py | 27 ++++++++++++++++++++++++++-
 3 files changed, 53 insertions(+), 1 deletion(-)
---
diff --git a/api/tests.py b/api/tests.py
index 6d4f6e94..b9d215e9 100644
--- a/api/tests.py
+++ b/api/tests.py
@@ -40,6 +40,28 @@ class APITests(TestCase):
             {'description': 'UI Translations', 'name': 'po', 'dtype': 'ui', 'layout': 'po/{lang}.po'}
         )
 
+    def test_module_branch(self):
+        response = self.client.get(reverse('api-module-branch', args=['gnome-hello', 'master']))
+        result = response.json()
+        self.assertEqual(
+            result, {
+                'module': 'gnome-hello',
+                'branch': 'master',
+                'domains': [{
+                    'name': 'po',
+                    'statistics': {
+                        'fr': {'fuzzy': 0, 'trans': 47, 'untrans': 0},
+                        'it': {'fuzzy': 10, 'trans': 30, 'untrans': 7}
+                    }}, {
+                    'name': 'help',
+                    'statistics': {
+                        'fr': {'fuzzy': 0, 'trans': 20, 'untrans': 0},
+                        'it': {'fuzzy': 0, 'trans': 20, 'untrans': 0}
+                    }}
+                ],
+            }
+        )
+
     def test_teams(self):
         response = self.client.get(reverse('api-teams'))
         result = response.json()
diff --git a/api/urls.py b/api/urls.py
index b5190d96..d71bfafd 100644
--- a/api/urls.py
+++ b/api/urls.py
@@ -11,6 +11,11 @@ urlpatterns = [
     path('', views.APIHomeView.as_view(), name='api-home'),
     path('modules/', views.ModulesView.as_view(), name='api-modules'),
     path('modules/<name:module_name>', views.ModuleView.as_view(), name='api-module'),
+    path(
+        'modules/<name:module_name>/branch/<name:branch_name>',
+        views.ModuleBranchView.as_view(),
+        name='api-module-branch',
+    ),
     path('teams/', views.TeamsView.as_view(), name='api-teams'),
     path('teams/<locale:team_name>', views.TeamView.as_view(), name='api-team'),
     path('languages/', views.LanguagesView.as_view(), name='api-languages'),
diff --git a/api/views.py b/api/views.py
index 04ee54a6..3e9a8943 100644
--- a/api/views.py
+++ b/api/views.py
@@ -12,7 +12,7 @@ from django.views.generic import View
 
 from common.utils import check_gitlab_request
 from languages.models import Language
-from stats.models import Branch, Module, Release
+from stats.models import Branch, Module, Release, Statistics
 from teams.models import Team
 from vertimus.models import ActionRT, ActionUT
 from vertimus.views import get_vertimus_state
@@ -85,6 +85,31 @@ class ModuleView(SerializeObjectView):
         return data
 
 
+class ModuleBranchView(SerializeObjectView):
+    def get_object(self):
+        return get_object_or_404(
+            Branch.objects.select_related('module'),
+            module__name=self.kwargs['module_name'], name=self.kwargs['branch_name']
+        )
+
+    def serialize_obj(self, branch, **kwargs):
+        data = {
+            'module': branch.module.name,
+            'branch': branch.name,
+            'domains': [],
+        }
+        for dom_name in branch.get_domains().keys():
+            stats = {}
+            for stat in Statistics.objects.filter(branch=branch, domain__name=dom_name, 
language__isnull=False):
+                stats[stat.language.locale] = {
+                    'trans': stat.translated(),
+                    'fuzzy': stat.fuzzy(),
+                    'untrans': stat.untranslated(),
+                }
+            data['domains'].append({'name': dom_name, 'statistics': stats})
+        return data
+
+
 class TeamsView(SerializeListView):
     fields = ['name', 'description']
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]