Skip to content

Commit

Permalink
Refactor show_disabled option into count_disabled and count_system_pr…
Browse files Browse the repository at this point in the history
…ojects
  • Loading branch information
eemeli committed Jan 22, 2025
1 parent d9569c6 commit 3c5cd20
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 32 deletions.
11 changes: 9 additions & 2 deletions pontoon/base/aggregated_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 7 additions & 2 deletions pontoon/base/models/locale.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion pontoon/base/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pontoon/base/models/project_locale.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
40 changes: 19 additions & 21 deletions pontoon/base/models/translated_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,30 @@
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__)


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(
Expand All @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions pontoon/base/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pontoon/localizations/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ <h4>
</td>
{% if request %}
<td class="all-strings">
{% if project.total_strings %}
{% if chart.total %}
<span>{{ project.avg_string_count|intcomma }}</span>
{% else %}
<span class="not-ready">Not synced yet</span>
Expand Down
2 changes: 1 addition & 1 deletion pontoon/projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": (
Expand Down

0 comments on commit 3c5cd20

Please sign in to comment.