Skip to content

Commit

Permalink
Uniform Checks for Locale, Project, and ProjectLocale in AJAX Views (m…
Browse files Browse the repository at this point in the history
  • Loading branch information
ayanaar authored Dec 20, 2023
1 parent 5edbcad commit 3a95b7b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pontoon/localizations/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
ResourceFactory,
TranslationFactory,
TranslatedResourceFactory,
ProjectLocaleFactory,
)


Expand All @@ -18,6 +19,7 @@
@patch.object(Locale, "parts_stats")
def test_latest_activity(mock_parts_stats, mock_render, client, project_a, locale_a):
"""Ensure that the latest_activity field is added to parts."""
ProjectLocaleFactory.create(locale=locale_a, project=project_a)
resource = ResourceFactory.create(project=project_a, path="has/stats.po")
resource2 = ResourceFactory.create(project=project_a, path="has/stats2.po")

Expand Down
12 changes: 12 additions & 0 deletions pontoon/localizations/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ def ajax_resources(request, code, slug):
slug=slug,
)

# Check if ProjectLocale exists
get_object_or_404(ProjectLocale, locale=locale, project=project)

# Amend the parts dict with latest activity info.
translatedresources_qs = TranslatedResource.objects.filter(
resource__project=project, locale=locale
Expand Down Expand Up @@ -150,6 +153,9 @@ def ajax_tags(request, code, slug):
locale = get_object_or_404(Locale, code=code)
project = get_object_or_404(Project.objects.visible_for(request.user), slug=slug)

# Check if ProjectLocale exists
get_object_or_404(ProjectLocale, locale=locale, project=project)

if not project.tags_enabled:
raise Http404

Expand All @@ -174,6 +180,8 @@ def ajax_insights(request, code, slug):
if not settings.ENABLE_INSIGHTS:
raise ImproperlyConfigured("ENABLE_INSIGHTS variable not set in settings.")

get_object_or_404(Locale, code=code)
get_object_or_404(Project.objects.visible_for(request.user), slug=slug)
pl = get_object_or_404(ProjectLocale, locale__code=code, project__slug=slug)
insights = get_insights(locale=pl.locale, project=pl.project)

Expand All @@ -188,6 +196,10 @@ class LocalizationContributorsView(ContributorsMixin, DetailView):
template_name = "localizations/includes/contributors.html"

def get_object(self):
get_object_or_404(Locale, code=self.kwargs["code"])
get_object_or_404(
Project.objects.visible_for(self.request.user), slug=self.kwargs["slug"]
)
return get_object_or_404(
ProjectLocale,
locale__code=self.kwargs["code"],
Expand Down

0 comments on commit 3a95b7b

Please sign in to comment.