Merge pull request #1688 from umap-project/fix-oembed-i18n

fix: deal with i18n in oembed URLs
This commit is contained in:
Yohan Boniface 2024-03-13 18:36:14 +01:00 committed by GitHub
commit 8cc6d58752
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 2 deletions

View file

@ -7,6 +7,7 @@ from django.contrib.auth import get_user_model
from django.core import mail from django.core import mail
from django.core.signing import Signer from django.core.signing import Signer
from django.urls import reverse from django.urls import reverse
from django.utils import translation
from umap.models import DataLayer, Map, Star from umap.models import DataLayer, Map, Star
@ -815,6 +816,17 @@ def test_oembed_map(client, map, datalayer):
) )
def test_oembed_map_with_non_default_language(client, map, datalayer):
translation.activate("en")
path = map.get_absolute_url()
assert path.startswith("/en/")
path = path.replace("/en/", "/fr/")
url = f"{reverse('map_oembed')}?url=http://testserver{path}"
response = client.get(url)
assert response.status_code == 200
translation.activate("en")
def test_oembed_link(client, map, datalayer): def test_oembed_link(client, map, datalayer):
response = client.get(map.get_absolute_url()) response = client.get(map.get_absolute_url())
assert response.status_code == 200 assert response.status_code == 200

View file

@ -38,11 +38,11 @@ from django.http import (
from django.middleware.gzip import re_accepts_gzip from django.middleware.gzip import re_accepts_gzip
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from django.urls import resolve, reverse, reverse_lazy from django.urls import resolve, reverse, reverse_lazy
from django.utils import translation
from django.utils.encoding import smart_bytes from django.utils.encoding import smart_bytes
from django.utils.http import http_date from django.utils.http import http_date
from django.utils.timezone import make_aware from django.utils.timezone import make_aware
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from django.utils.translation import to_locale
from django.views.decorators.cache import cache_control from django.views.decorators.cache import cache_control
from django.views.decorators.http import require_GET from django.views.decorators.http import require_GET
from django.views.generic import DetailView, TemplateView, View from django.views.generic import DetailView, TemplateView, View
@ -536,7 +536,7 @@ class MapDetailMixin:
if hasattr(self.request, "LANGUAGE_CODE"): if hasattr(self.request, "LANGUAGE_CODE"):
lang = self.request.LANGUAGE_CODE lang = self.request.LANGUAGE_CODE
properties["lang"] = lang properties["lang"] = lang
locale = to_locale(lang) locale = translation.to_locale(lang)
properties["locale"] = locale properties["locale"] = locale
context["locale"] = locale context["locale"] = locale
geojson = self.get_geojson() geojson = self.get_geojson()
@ -699,6 +699,8 @@ class MapOEmbed(View):
raise Http404("Host not allowed.") raise Http404("Host not allowed.")
url_path = parsed_url.path url_path = parsed_url.path
lang = translation.get_language_from_path(url_path)
translation.activate(lang)
view, args, kwargs = resolve(url_path) view, args, kwargs = resolve(url_path)
if "slug" not in kwargs or "map_id" not in kwargs: if "slug" not in kwargs or "map_id" not in kwargs:
raise Http404("Invalid URL path.") raise Http404("Invalid URL path.")