Repository: digitalfabrik/integreat-cms

Issue:
Fix translation coverage
The translation coverage is not working due to the changed language model a while ago...
Error: `'Language' object has no attribute 'name'`


Discussion:

Resolving patch:
diff --git a/backend/cms/models/pages/page_translation.py b/backend/cms/models/pages/page_translation.py
--- a/backend/cms/models/pages/page_translation.py
+++ b/backend/cms/models/pages/page_translation.py
@@ -120,7 +120,7 @@ def is_outdated(self):
             return True
         self_revision = self.latest_major_public_revision
         source_revision = source_translation.latest_major_public_revision
-        # If on of the translations has no major public revision, it cannot be outdated
+        # If one of the translations has no major public revision, it cannot be outdated
         if not self_revision or not source_revision:
             return False
         return self_revision.last_updated < source_revision.last_updated
@@ -134,9 +134,21 @@ def combined_text(self):
             return self.page.get_mirrored_text(self.language.code) + self.text
         return self.text + self.page.get_mirrored_text(self.language.code)
 
+    @classmethod
+    def get_translations(cls, region, language):
+        return cls.objects.filter(page__region=region, language=language).distinct('page')
+
+    @classmethod
+    def get_outdated_translations(cls, region, language):
+        return [t for t in cls.objects.filter(page__region=region, language=language).distinct('page') if t.is_outdated]
+
+    @classmethod
+    def get_up_to_date_translations(cls, region, language):
+        return [t for t in cls.objects.filter(page__region=region, language=language).distinct('page') if not t.is_outdated]
+
     def __str__(self):
         if self.id:
-            return '(id: {}, lang: {}, slug: {})'.format(self.id, self.language.code, self.slug)
+            return '(id: {}, page_id: {}, lang: {}, version: {}, slug: {})'.format(self.id, self.page.id, self.language.code, self.version, self.slug)
         return super(PageTranslation, self).__str__()
 
     class Meta:
diff --git a/backend/cms/views/analytics/translation_coverage_view.py b/backend/cms/views/analytics/translation_coverage_view.py
--- a/backend/cms/views/analytics/translation_coverage_view.py
+++ b/backend/cms/views/analytics/translation_coverage_view.py
@@ -4,7 +4,7 @@
 from django.views.generic import TemplateView
 from django.shortcuts import render
 
-from ...models import Language, Page, PageTranslation
+from ...models import PageTranslation, Region
 from ...decorators import region_permission_required
 
 
@@ -12,30 +12,31 @@
 @method_decorator(region_permission_required, name='dispatch')
 class TranslationCoverageView(TemplateView):
     """
-    Class to create the statistic page, that can be found via -> "Statistiken"
+    Class to create the translation coverage statistic
     """
     template_name = 'analytics/translation_coverage.html'
     base_context = {'current_menu_item': 'translation_coverage'}
 
     def get(self, request, *args, **kwargs):
 
-        languages = Language.objects.all()
+        region = Region.get_current_region(request)
+        num_pages = region.pages.count()
+        languages = []
 
-        languages = list(map(lambda x: [x.code, x.name, x.id], languages))
-
-        all_pages = Page.objects.count()
-
-        # pylint: disable=consider-using-enumerate
-        for i in range(len(languages)):
-            translated_count = PageTranslation.objects.filter(language_id=languages[i][2]).count()
-            languages[i].append(translated_count)
-            languages[i].append(all_pages - languages[i][3])
+        for language in region.languages:
+            page_translations = PageTranslation.get_translations(region, language)
+            languages.append({
+                'translated_name': language.translated_name,
+                'num_page_translations_up_to_date': len([t for t in page_translations if not t.is_outdated]),
+                'num_page_translations_outdated': len([t for t in page_translations if t.is_outdated]),
+                'num_page_translations_missing': num_pages - page_translations.count()
+            })
 
         return render(
             request,
             self.template_name,
             {
                 **self.base_context,
-                'coverages': languages
+                'languages': languages
             }
         )


Test patch: