Skip to content

Commit

Permalink
Merge pull request mozilla#6282 from akatsoulas/normalize-locale
Browse files Browse the repository at this point in the history
Normalize locale in the API
  • Loading branch information
escattone authored Oct 9, 2024
2 parents ad827ac + 0247a42 commit 4cc50d8
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
10 changes: 5 additions & 5 deletions kitsune/gallery/api.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions kitsune/sumo/api_utils.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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


Expand Down
3 changes: 2 additions & 1 deletion kitsune/sumo/tests/test_api_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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):
Expand Down
6 changes: 4 additions & 2 deletions kitsune/wiki/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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

0 comments on commit 4cc50d8

Please sign in to comment.