From 0247a42278e3e37439f4ba3bef8c550f62af3062 Mon Sep 17 00:00:00 2001 From: Tasos Katsoulas Date: Wed, 9 Oct 2024 18:46:56 +0300 Subject: [PATCH] Normalize locale in the API --- kitsune/gallery/api.py | 10 +++++----- kitsune/sumo/api_utils.py | 8 ++++---- kitsune/sumo/tests/test_api_utils.py | 3 ++- kitsune/wiki/api.py | 6 ++++-- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/kitsune/gallery/api.py b/kitsune/gallery/api.py index 9ef77472add..2751ac30ebd 100644 --- a/kitsune/gallery/api.py +++ b/kitsune/gallery/api.py @@ -1,15 +1,15 @@ -from django.db.models import Q from django.contrib.auth.models import User - +from django.db.models import Q from rest_framework import generics, serializers from kitsune.gallery.models import Image from kitsune.sumo.api_utils import ( - LocaleNegotiationMixin, - InequalityFilterBackend, DateTimeUTCField, ImageUrlField, + InequalityFilterBackend, + LocaleNegotiationMixin, ) +from kitsune.sumo.i18n import normalize_language class ImageShortSerializer(serializers.ModelSerializer): @@ -50,7 +50,7 @@ def get_queryset(self): # locale may come from the Accept-language header, but it can be # overridden via the query string. - locale = self.get_locale() + locale = normalize_language(self.get_locale()) locale = self.request.query_params.get("locale", locale) if locale is not None: queryset = queryset.filter(locale=locale) diff --git a/kitsune/sumo/api_utils.py b/kitsune/sumo/api_utils.py index 0bb0c348189..e9addd7a1a6 100644 --- a/kitsune/sumo/api_utils.py +++ b/kitsune/sumo/api_utils.py @@ -1,18 +1,18 @@ from zoneinfo import ZoneInfo from django import forms -from django.contrib.contenttypes.models import ContentType from django.contrib.auth.models import User +from django.contrib.contenttypes.models import ContentType from django.db.models import F from django.http import HttpResponse from django.utils import translation from django.utils.translation import pgettext - from rest_framework import fields, filters, permissions, serializers -from rest_framework.authentication import SessionAuthentication, CSRFCheck +from rest_framework.authentication import CSRFCheck, SessionAuthentication from rest_framework.exceptions import APIException, AuthenticationFailed from rest_framework.renderers import JSONRenderer as DRFJSONRenderer +from kitsune.sumo.i18n import normalize_language from kitsune.users.models import Profile @@ -44,7 +44,7 @@ def get_locale(self): def get_serializer_context(self): context = super(LocaleNegotiationMixin, self).get_serializer_context() - context["locale"] = self.get_locale() + context["locale"] = normalize_language(self.get_locale()) return context diff --git a/kitsune/sumo/tests/test_api_utils.py b/kitsune/sumo/tests/test_api_utils.py index 6e93bb02884..b838b295d6b 100644 --- a/kitsune/sumo/tests/test_api_utils.py +++ b/kitsune/sumo/tests/test_api_utils.py @@ -7,6 +7,7 @@ from rest_framework import fields, serializers from kitsune.sumo import api_utils +from kitsune.sumo.i18n import normalize_language from kitsune.sumo.tests import TestCase @@ -22,7 +23,7 @@ def test_it_works(self): negotiater = api_utils.LocaleNegotiationMixin() request = factory.get("/", HTTP_ACCEPT_LANGUAGE="es,en-US") negotiater.request = request - self.assertEqual(negotiater.get_locale(), "es") + self.assertEqual(normalize_language(negotiater.get_locale()), "es") class TestInequalityFilterBackend(TestCase): diff --git a/kitsune/wiki/api.py b/kitsune/wiki/api.py index a64b7773a24..415f0269c19 100644 --- a/kitsune/wiki/api.py +++ b/kitsune/wiki/api.py @@ -5,6 +5,7 @@ from kitsune.products.models import Product, Topic from kitsune.sumo.api_utils import GenericAPIException, LocaleNegotiationMixin +from kitsune.sumo.i18n import normalize_language from kitsune.wiki.config import REDIRECT_HTML from kitsune.wiki.models import Document @@ -40,7 +41,7 @@ def get_queryset(self): self.request.user, category__in=settings.IA_DEFAULT_CATEGORIES ) - locale = self.get_locale() + locale = normalize_language(self.get_locale()) product = self.request.query_params.get("product") topic = self.request.query_params.get("topic") is_template = bool(self.request.query_params.get("is_template", False)) @@ -82,7 +83,8 @@ class DocumentDetail(LocaleNegotiationMixin, generics.RetrieveAPIView): serializer_class = DocumentDetailSerializer def get_object(self): - queryset = Document.objects.visible(self.request.user, locale=self.get_locale()) + locale = normalize_language(self.get_locale()) + queryset = Document.objects.visible(self.request.user, locale=locale) obj = get_object_or_404(queryset, **self.kwargs) self.check_object_permissions(self.request, obj) return obj