Skip to content

Commit 61743c0

Browse files
authored
Fix missing stats for private projects (#4680)
1 parent c9e8e17 commit 61743c0

10 files changed

Lines changed: 447 additions & 14 deletions

File tree

‎pontoon/base/models/translated_resource.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from pontoon.base.models.project import Project
88
from pontoon.base.models.resource import Resource
99
from pontoon.base.models.translation import Translation
10-
from pontoon.base.models.user import User
1110

1211

1312
log = logging.getLogger(__name__)
@@ -16,17 +15,18 @@
1615
class TranslatedResourceQuerySet(models.QuerySet["TranslatedResource"]):
1716
def string_stats(
1817
self,
19-
user: User | None = None,
2018
*,
2119
count_disabled: bool = False,
2220
count_system_projects: bool = False,
21+
only_public: bool = False,
2322
) -> dict[str, int]:
23+
"""Return aggregate string counts, optionally only for public projects."""
2424
query = self
2525
if not count_disabled:
2626
query = query.filter(resource__project__disabled=False)
2727
if not count_system_projects:
2828
query = query.filter(resource__project__system_project=False)
29-
if user is None or not user.is_superuser:
29+
if only_public:
3030
query = query.filter(resource__project__visibility="public")
3131
return query.aggregate(
3232
total=Sum("total_strings", default=0),
@@ -43,7 +43,7 @@ def query_stats(self, project: Project, paths: list[str], locale: Locale):
4343
"""
4444
query = self.filter(locale=locale)
4545
if project.slug == "all-projects":
46-
return query.string_stats()
46+
return query.string_stats(only_public=True)
4747
else:
4848
query = query.filter(resource__project=project)
4949
if paths:

‎pontoon/base/tests/models/test_stats.py‎

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
import pytest
66

7-
from pontoon.base.models import TranslatedResource
8-
from pontoon.test.factories import TranslationFactory
7+
from pontoon.base.models import Project, TranslatedResource
8+
from pontoon.test.factories import TranslatedResourceFactory, TranslationFactory
99

1010

1111
def get_stats(translation):
@@ -184,3 +184,99 @@ def test_translation_with_warning(translation_a):
184184
"warnings": 0,
185185
"errors": 0,
186186
}
187+
188+
189+
@pytest.mark.django_db
190+
def test_string_stats_includes_private_projects(project_a, locale_a, resource_a):
191+
"""Private projects are included unless only_public is requested."""
192+
project_a.visibility = Project.Visibility.PRIVATE
193+
project_a.save()
194+
TranslatedResourceFactory.create(
195+
resource=resource_a, locale=locale_a, total_strings=2, approved_strings=1
196+
)
197+
198+
query = TranslatedResource.objects.filter(resource__project=project_a)
199+
assert query.string_stats()["total"] == 2
200+
assert query.string_stats()["approved"] == 1
201+
assert query.string_stats(only_public=True)["total"] == 0
202+
203+
204+
@pytest.mark.django_db
205+
def test_query_stats_includes_private_project(project_a, locale_a, resource_a):
206+
"""query_stats includes stats for a private project."""
207+
project_a.visibility = Project.Visibility.PRIVATE
208+
project_a.save()
209+
TranslatedResourceFactory.create(
210+
resource=resource_a, locale=locale_a, total_strings=2, approved_strings=1
211+
)
212+
213+
stats = TranslatedResource.objects.query_stats(project_a, [], locale_a)
214+
assert stats["total"] == 2
215+
assert stats["approved"] == 1
216+
217+
218+
@pytest.mark.django_db
219+
def test_query_stats_all_projects_excludes_private(
220+
project_a, project_b, locale_a, resource_a, resource_b
221+
):
222+
"""All-projects query_stats keeps excluding private projects."""
223+
project_b.visibility = Project.Visibility.PRIVATE
224+
project_b.save()
225+
TranslatedResourceFactory.create(
226+
resource=resource_a, locale=locale_a, total_strings=3, approved_strings=2
227+
)
228+
TranslatedResourceFactory.create(
229+
resource=resource_b, locale=locale_a, total_strings=4, approved_strings=1
230+
)
231+
232+
stats = TranslatedResource.objects.query_stats(
233+
Project(slug="all-projects"), [], locale_a
234+
)
235+
assert stats["total"] == 3
236+
assert stats["approved"] == 2
237+
238+
239+
@pytest.mark.django_db
240+
def test_aggregated_stats_includes_private_project(project_a, locale_a, resource_a):
241+
"""Project stats include strings from private projects."""
242+
project_a.visibility = Project.Visibility.PRIVATE
243+
project_a.save()
244+
TranslatedResourceFactory.create(
245+
resource=resource_a, locale=locale_a, total_strings=2, approved_strings=1
246+
)
247+
248+
assert project_a.total_strings == 2
249+
assert project_a.approved_strings == 1
250+
251+
252+
@pytest.mark.django_db
253+
def test_aggregated_stats_includes_private_project_locale(
254+
project_a, locale_a, project_locale_a, resource_a
255+
):
256+
"""ProjectLocale stats include strings from private projects."""
257+
project_a.visibility = Project.Visibility.PRIVATE
258+
project_a.save()
259+
TranslatedResourceFactory.create(
260+
resource=resource_a, locale=locale_a, total_strings=2, approved_strings=1
261+
)
262+
263+
assert project_locale_a.total_strings == 2
264+
assert project_locale_a.approved_strings == 1
265+
266+
267+
@pytest.mark.django_db
268+
def test_locale_aggregated_stats_excludes_private_projects(
269+
project_a, project_b, locale_a, resource_a, resource_b
270+
):
271+
"""Locale stats only aggregate public projects."""
272+
project_b.visibility = Project.Visibility.PRIVATE
273+
project_b.save()
274+
TranslatedResourceFactory.create(
275+
resource=resource_a, locale=locale_a, total_strings=3, approved_strings=2
276+
)
277+
TranslatedResourceFactory.create(
278+
resource=resource_b, locale=locale_a, total_strings=4, approved_strings=1
279+
)
280+
281+
assert locale_a.total_strings == 3
282+
assert locale_a.approved_strings == 2

‎pontoon/base/tests/views/test_ajax.py‎

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from django.http import Http404
88

9-
from pontoon.base.models import Resource
9+
from pontoon.base.models import Project, Resource
1010
from pontoon.base.views import (
1111
AjaxFormPostView,
1212
AjaxFormView,
@@ -676,3 +676,51 @@ def test_translate_view_endpoints_reach_system_projects(rf, user_a, view):
676676
request.user = user_a
677677

678678
assert view(request).status_code == 200
679+
680+
681+
@pytest.mark.django_db
682+
def test_locale_stats_exclude_private_projects(
683+
client, locale_a, project_a, project_b, resource_a, resource_b
684+
):
685+
"""Locale stats exclude private projects for regular users."""
686+
project_b.visibility = Project.Visibility.PRIVATE
687+
project_b.save()
688+
TranslatedResourceFactory.create(
689+
resource=resource_a, locale=locale_a, total_strings=2, approved_strings=1
690+
)
691+
TranslatedResourceFactory.create(
692+
resource=resource_b, locale=locale_a, total_strings=3, approved_strings=2
693+
)
694+
695+
response = client.get(
696+
f"/teams/{locale_a.code}/stats/", HTTP_X_REQUESTED_WITH="XMLHttpRequest"
697+
)
698+
699+
assert response.status_code == 200
700+
stats = response.json()[0]
701+
assert stats["total"] == 2
702+
assert stats["approved"] == 1
703+
704+
705+
@pytest.mark.django_db
706+
def test_locale_stats_include_private_projects_for_superuser(
707+
client_superuser, locale_a, project_a, project_b, resource_a, resource_b
708+
):
709+
"""Locale stats include private projects for superusers."""
710+
project_b.visibility = Project.Visibility.PRIVATE
711+
project_b.save()
712+
TranslatedResourceFactory.create(
713+
resource=resource_a, locale=locale_a, total_strings=2, approved_strings=1
714+
)
715+
TranslatedResourceFactory.create(
716+
resource=resource_b, locale=locale_a, total_strings=3, approved_strings=2
717+
)
718+
719+
response = client_superuser.get(
720+
f"/teams/{locale_a.code}/stats/", HTTP_X_REQUESTED_WITH="XMLHttpRequest"
721+
)
722+
723+
assert response.status_code == 200
724+
stats = response.json()[0]
725+
assert stats["total"] == 5
726+
assert stats["approved"] == 3

‎pontoon/base/views.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ def locale_projects(request, locale):
121121
def locale_stats(request, locale):
122122
"""Get locale stats used in All Resources part."""
123123
locale = get_object_or_404(Locale, code=locale)
124-
stats = TranslatedResource.objects.filter(locale=locale).string_stats(request.user)
124+
stats = TranslatedResource.objects.filter(locale=locale).string_stats(
125+
only_public=not request.user.is_superuser
126+
)
125127
stats["title"] = "all-resources"
126128
return JsonResponse([stats], safe=False)
127129

@@ -157,7 +159,7 @@ def locale_project_parts(request, locale, slug):
157159
"approved",
158160
)
159161
)
160-
all_res_stats = tr.string_stats(request.user, count_system_projects=True)
162+
all_res_stats = tr.string_stats(count_system_projects=True)
161163
all_res_stats["title"] = "all-resources"
162164
details.append(all_res_stats)
163165
return JsonResponse(details, safe=False)

‎pontoon/localizations/tests/test_views.py‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from django.shortcuts import render
66
from django.urls import reverse
77

8+
from pontoon.base.models import Project
89
from pontoon.test.factories import (
910
EntityFactory,
1011
ProjectLocaleFactory,
@@ -58,3 +59,83 @@ def test_ajax_resources(mock_render, client, project_a, locale_a):
5859
"errors": 0,
5960
"warnings": 0,
6061
}
62+
63+
64+
@pytest.mark.django_db
65+
def test_localization_view_private_stats(
66+
client_superuser, project_a, locale_a, project_locale_a, resource_a
67+
):
68+
"""Private project locale stats are visible to superusers."""
69+
project_a.visibility = Project.Visibility.PRIVATE
70+
project_a.save()
71+
TranslatedResourceFactory.create(
72+
resource=resource_a,
73+
locale=locale_a,
74+
total_strings=1,
75+
approved_strings=1,
76+
)
77+
78+
with patch("pontoon.localizations.views.render", wraps=render) as mock_render:
79+
response = client_superuser.get(
80+
reverse(
81+
"pontoon.localizations.localization",
82+
args=[locale_a.code, project_a.slug],
83+
)
84+
)
85+
86+
assert response.status_code == 200
87+
stats = mock_render.call_args[0][2]["project_locale_stats"]
88+
assert stats["total"] > 0
89+
assert stats["approved"] > 0
90+
91+
92+
@pytest.mark.django_db
93+
def test_localization_view_private_stats_anonymous(
94+
client, project_a, locale_a, project_locale_a, resource_a
95+
):
96+
"""Private project locale stats are hidden from anonymous users."""
97+
# The page is accessible while public; the 404 below is caused by visibility.
98+
response = client.get(
99+
reverse(
100+
"pontoon.localizations.localization",
101+
args=[locale_a.code, project_a.slug],
102+
)
103+
)
104+
assert response.status_code == 200
105+
106+
project_a.visibility = Project.Visibility.PRIVATE
107+
project_a.save()
108+
109+
response = client.get(
110+
reverse(
111+
"pontoon.localizations.localization",
112+
args=[locale_a.code, project_a.slug],
113+
)
114+
)
115+
assert response.status_code == 404
116+
117+
118+
@pytest.mark.django_db
119+
def test_localization_view_private_stats_non_superuser(
120+
member, project_a, locale_a, project_locale_a, resource_a
121+
):
122+
"""Private project locale stats are hidden from non-superusers."""
123+
# The page is accessible while public; the 404 below is caused by visibility.
124+
response = member.client.get(
125+
reverse(
126+
"pontoon.localizations.localization",
127+
args=[locale_a.code, project_a.slug],
128+
)
129+
)
130+
assert response.status_code == 200
131+
132+
project_a.visibility = Project.Visibility.PRIVATE
133+
project_a.save()
134+
135+
response = member.client.get(
136+
reverse(
137+
"pontoon.localizations.localization",
138+
args=[locale_a.code, project_a.slug],
139+
)
140+
)
141+
assert response.status_code == 404

‎pontoon/messaging/tests/test_send_notifications.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from django.template.loader import render_to_string
77
from django.utils import timezone
88

9+
from pontoon.base.models import Project, TranslatedResource
910
from pontoon.base.templatetags.helpers import full_url
1011
from pontoon.messaging.management.commands.send_deadline_notifications import (
1112
Command as DeadlineCommand,
@@ -202,6 +203,28 @@ def test_send_deadline_notifications_excludes_system_users(
202203
assert tm_user not in recipients
203204

204205

206+
@patch("pontoon.messaging.notifications.notify.send")
207+
@pytest.mark.django_db
208+
def test_send_deadline_notifications_include_private_project_stats(
209+
mock_notify, admin, project_a, locale_a, resource_a
210+
):
211+
"""Private project stats are counted for deadline notifications."""
212+
project_a.visibility = Project.Visibility.PRIVATE
213+
project_a.deadline = (timezone.now() + timedelta(days=7)).date()
214+
project_a.save()
215+
ProjectLocaleFactory.create(project=project_a, locale=locale_a)
216+
entity = EntityFactory.create(resource=resource_a)
217+
TranslationFactory.create(locale=locale_a, entity=entity, user=admin)
218+
TranslatedResource.objects.filter(resource=resource_a, locale=locale_a).update(
219+
total_strings=1, approved_strings=0
220+
)
221+
222+
DeadlineCommand().handle()
223+
224+
recipients = {call.kwargs["recipient"] for call in mock_notify.call_args_list}
225+
assert admin in recipients
226+
227+
205228
@patch("pontoon.messaging.notifications.notify.send")
206229
@pytest.mark.django_db
207230
def test_send_monthly_health_report_notifications_notifies_admins(

0 commit comments

Comments
 (0)