diff --git a/pontoon/base/aggregated_stats.py b/pontoon/base/aggregated_stats.py index b4e5284e9..de1c9424e 100644 --- a/pontoon/base/aggregated_stats.py +++ b/pontoon/base/aggregated_stats.py @@ -4,11 +4,18 @@ class AggregatedStats: - trans_res_query: object + aggregated_stats_query: object + """ + Must be set by the child class as a QuerySet of TranslatedResource objects. + + Should include any filters leaving out disabled or system projects. + """ @cached_property def _stats(self) -> dict[str, int]: - return self.trans_res_query.string_stats() + return self.aggregated_stats_query.string_stats( + count_disabled=True, count_system_projects=True + ) @property def total_strings(self) -> int: diff --git a/pontoon/base/models/locale.py b/pontoon/base/models/locale.py index d8fa5049a..73aaae2fe 100644 --- a/pontoon/base/models/locale.py +++ b/pontoon/base/models/locale.py @@ -82,10 +82,15 @@ def prefetch_project_locale(self, project): class Locale(models.Model, AggregatedStats): @property - def trans_res_query(self): + def aggregated_stats_query(self): from pontoon.base.models.translated_resource import TranslatedResource - return TranslatedResource.objects.filter(locale=self) + return TranslatedResource.objects.filter( + locale=self, + resource__project__disabled=False, + resource__project__system_project=False, + resource__project__visibility="public", + ) code = models.CharField(max_length=20, unique=True) diff --git a/pontoon/base/models/project.py b/pontoon/base/models/project.py index 79d046bb9..cffdbf53a 100644 --- a/pontoon/base/models/project.py +++ b/pontoon/base/models/project.py @@ -89,7 +89,7 @@ def prefetch_project_locale(self, locale): class Project(models.Model, AggregatedStats): @property - def trans_res_query(self): + def aggregated_stats_query(self): from pontoon.base.models.translated_resource import TranslatedResource return TranslatedResource.objects.filter(resource__project=self) diff --git a/pontoon/base/models/project_locale.py b/pontoon/base/models/project_locale.py index a6cae356b..36ceab48e 100644 --- a/pontoon/base/models/project_locale.py +++ b/pontoon/base/models/project_locale.py @@ -34,7 +34,7 @@ class ProjectLocale(models.Model, AggregatedStats): """Link between a project and a locale that is active for it.""" @property - def trans_res_query(self): + def aggregated_stats_query(self): from pontoon.base.models.translated_resource import TranslatedResource return TranslatedResource.objects.filter( diff --git a/pontoon/base/models/translated_resource.py b/pontoon/base/models/translated_resource.py index 59a9969c6..0f82cbc3c 100644 --- a/pontoon/base/models/translated_resource.py +++ b/pontoon/base/models/translated_resource.py @@ -3,12 +3,12 @@ from django.db import models from django.db.models import F, Q, Sum -from pontoon.base.models.entity import Entity -from pontoon.base.models.locale import Locale -from pontoon.base.models.project import Project -from pontoon.base.models.resource import Resource -from pontoon.base.models.translation import Translation -from pontoon.base.models.user import User +from .entity import Entity +from .locale import Locale +from .project import Project +from .resource import Resource +from .translation import Translation +from .user import User log = logging.getLogger(__name__) @@ -16,16 +16,17 @@ class TranslatedResourceQuerySet(models.QuerySet): def string_stats( - self, user: User | None = None, *, show_disabled: bool = False + self, + user: User | None = None, + *, + count_disabled: bool = False, + count_system_projects: bool = False, ) -> dict[str, int]: - query = ( - self - if show_disabled - else self.filter( - resource__project__disabled=False, - resource__project__system_project=False, - ) - ) + query = self + if not count_disabled: + query = query.filter(resource__project__disabled=False) + if not count_system_projects: + query = query.filter(resource__project__system_project=False) if user is None or not user.is_superuser: query = query.filter(resource__project__visibility="public") return query.aggregate( @@ -41,17 +42,14 @@ def query_stats(self, project: Project, paths: list[str], locale: Locale): """ Returns statistics for the given project, paths and locale. """ - query = self.filter(locale=locale, resource__project__disabled=False) + query = self.filter(locale=locale) if project.slug == "all-projects": - query = query.filter( - resource__project__system_project=False, - resource__project__visibility=Project.Visibility.PUBLIC, - ) + return query.string_stats() else: query = query.filter(resource__project=project) if paths: query = query.filter(resource__path__in=paths) - return query.string_stats(show_disabled=True) + return query.string_stats(count_system_projects=True) def calculate_stats(self): self = self.prefetch_related("resource__project", "locale") diff --git a/pontoon/base/views.py b/pontoon/base/views.py index 51472a4fd..27d682431 100755 --- a/pontoon/base/views.py +++ b/pontoon/base/views.py @@ -116,7 +116,7 @@ def locale_project_parts(request, locale, slug): locale = Locale.objects.get(code=locale) project = Project.objects.visible_for(request.user).get(slug=slug) tr = TranslatedResource.objects.filter( - resource__project=project, resource__entities__obsolete=False, locale=locale + locale=locale, resource__project=project ).distinct() details = list( tr.annotate( @@ -139,7 +139,7 @@ def locale_project_parts(request, locale, slug): "approved", ) ) - all_res_stats = tr.string_stats(request.user, show_disabled=True) + all_res_stats = tr.string_stats(request.user, count_system_projects=True) all_res_stats["title"] = "all-resources" details.append(all_res_stats) return JsonResponse(details, safe=False) diff --git a/pontoon/localizations/views.py b/pontoon/localizations/views.py index 549b15eb1..6861e8128 100644 --- a/pontoon/localizations/views.py +++ b/pontoon/localizations/views.py @@ -49,7 +49,7 @@ def localization(request, code, slug): { "locale": locale, "project": project, - "project_locale_stats": trans_res.string_stats(show_disabled=True), + "project_locale_stats": trans_res.string_stats(count_system_projects=True), "resource_count": trans_res.filter(resource__entities__obsolete=False) .distinct() .count(), diff --git a/pontoon/projects/templates/projects/widgets/project_list.html b/pontoon/projects/templates/projects/widgets/project_list.html index 16356e6ef..8ab9337c5 100644 --- a/pontoon/projects/templates/projects/widgets/project_list.html +++ b/pontoon/projects/templates/projects/widgets/project_list.html @@ -47,7 +47,7 @@

{% if request %} - {% if project.total_strings %} + {% if chart.total %} {{ project.avg_string_count|intcomma }} {% else %} Not synced yet diff --git a/pontoon/projects/views.py b/pontoon/projects/views.py index 0556322b8..522a469e9 100644 --- a/pontoon/projects/views.py +++ b/pontoon/projects/views.py @@ -63,7 +63,7 @@ def project(request, slug): request, "projects/project.html", { - "project_stats": project_tr.string_stats(show_disabled=True), + "project_stats": project_tr.string_stats(count_system_projects=True), "count": project_locales.count(), "project": project, "tags_count": (