Skip to content

Commit

Permalink
🐛 [Bug fixes] fix language bug in several detail pages
Browse files Browse the repository at this point in the history
  • Loading branch information
njourdane committed Oct 23, 2023
1 parent 56db43f commit 698c287
Show file tree
Hide file tree
Showing 14 changed files with 179 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CHANGELOG

**Bug fixes**

- Fix language bug in several detail pages (#3577)
- Fix Aggregator fails when updating Tour steps order (#3793)
- Fix services list display error (refs ##3795)

Expand Down
11 changes: 11 additions & 0 deletions geotrek/diving/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ def test_pois_on_treks_not_public(self):
response = self.client.get(reverse('diving:dive_poi_geojson', kwargs={'lang': translation.get_language(), 'pk': dive.pk}))
self.assertEqual(response.status_code, 404)

def test_lang_in_detail_page(self):
dive = self.modelfactory.create(name_fr='une plongée', name_en='a dive')

response = self.client.get(dive.get_detail_url() + '?lang=fr')
self.assertContains(response, 'une plongée')
self.assertNotContains(response, 'a dive')

response = self.client.get(dive.get_detail_url() + '?lang=en')
self.assertContains(response, 'a dive')
self.assertNotContains(response, 'une plongée')


class DiveViewsLiveTests(CommonLiveTest):
model = Dive
Expand Down
11 changes: 11 additions & 0 deletions geotrek/infrastructure/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ def test_description_in_detail_page(self):
response = self.client.get(infra.get_detail_url())
self.assertContains(response, "<b>Beautiful !</b>")

def test_lang_in_detail_page(self):
infra = InfrastructureFactory.create(name_fr='une infra', name_en='an infra')

response = self.client.get(infra.get_detail_url() + '?lang=fr')
self.assertContains(response, 'une infra')
self.assertNotContains(response, 'an infra')

response = self.client.get(infra.get_detail_url() + '?lang=en')
self.assertContains(response, 'an infra')
self.assertNotContains(response, 'une infra')

def test_check_structure_or_none_related_are_visible(self):
infratype = InfrastructureTypeFactory.create(type=INFRASTRUCTURE_TYPES.BUILDING, structure=None)
response = self.client.get(self.model.get_add_url())
Expand Down
8 changes: 8 additions & 0 deletions geotrek/infrastructure/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.conf import settings
from django.utils import translation
from django.contrib.gis.db.models.functions import Transform
from mapentity.views import (MapEntityList, MapEntityFormat, MapEntityDetail, MapEntityDocument,
MapEntityCreate, MapEntityUpdate, MapEntityDelete)
Expand Down Expand Up @@ -37,6 +38,13 @@ class InfrastructureFormatList(MapEntityFormat, InfrastructureList):
class InfrastructureDetail(MapEntityDetail):
queryset = Infrastructure.objects.existing()

def dispatch(self, *args, **kwargs):
lang = self.request.GET.get('lang')
if lang:
translation.activate(lang)
self.request.LANGUAGE_CODE = lang
return super().dispatch(*args, **kwargs)

def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context['can_edit'] = self.get_object().same_structure(self.request.user)
Expand Down
24 changes: 24 additions & 0 deletions geotrek/outdoor/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ def test_init_form_with_parent_site(self):
selected = f"<option value=\"{parent.pk}\" selected> {parent.name}</option>"
self.assertContains(response, selected)

def test_lang_in_detail_page(self):
site = SiteFactory.create(name_fr='un site', name_en='a site')
self.client.force_login(SuperUserFactory())

response = self.client.get(site.get_detail_url() + '?lang=fr')
self.assertContains(response, 'un site')
self.assertNotContains(response, 'a site')

response = self.client.get(site.get_detail_url() + '?lang=en')
self.assertContains(response, 'a site')
self.assertNotContains(response, 'un site')


class CourseCustomViewTests(TestCase):
@mock.patch('mapentity.helpers.requests.get')
Expand Down Expand Up @@ -132,6 +144,18 @@ def test_serialize_ref_points(self):
data = "{'type': 'MultiPoint', 'coordinates': [[12.0, 12.0]]}"
self.assertEqual(str(response.json()[0]['points_reference']), data)

def test_lang_in_detail_page(self):
course = CourseFactory.create(name_fr='un parcours', name_en='a course', published=True)
self.client.force_login(SuperUserFactory())

response = self.client.get(course.get_detail_url() + '?lang=fr')
self.assertContains(response, 'un parcours')
self.assertNotContains(response, 'a course')

response = self.client.get(course.get_detail_url() + '?lang=en')
self.assertContains(response, 'a course')
self.assertNotContains(response, 'un parcours')


class SiteDeleteTest(TestCase):
@classmethod
Expand Down
15 changes: 15 additions & 0 deletions geotrek/outdoor/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.conf import settings
from django.utils import translation
from django.contrib.gis.db.models.functions import Transform
from django.db.models import Q, Prefetch
from geotrek.common.models import HDViewPoint
Expand Down Expand Up @@ -33,6 +34,13 @@ class SiteDetail(CompletenessMixin, MapEntityDetail):
queryset=HDViewPoint.objects.select_related('content_type', 'license'))
)

def dispatch(self, *args, **kwargs):
lang = self.request.GET.get('lang')
if lang:
translation.activate(lang)
self.request.LANGUAGE_CODE = lang
return super().dispatch(*args, **kwargs)

def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context['can_edit'] = self.get_object().same_structure(self.request.user)
Expand Down Expand Up @@ -156,6 +164,13 @@ class CourseList(CustomColumnsMixin, MapEntityList):
class CourseDetail(CompletenessMixin, MapEntityDetail):
queryset = Course.objects.prefetch_related('type').all()

def dispatch(self, *args, **kwargs):
lang = self.request.GET.get('lang')
if lang:
translation.activate(lang)
self.request.LANGUAGE_CODE = lang
return super().dispatch(*args, **kwargs)

def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context['can_edit'] = self.get_object().same_structure(self.request.user)
Expand Down
13 changes: 13 additions & 0 deletions geotrek/sensitivity/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.test import TestCase
from django.test.utils import override_settings
from django.urls import reverse
from mapentity.tests.factories import SuperUserFactory

from geotrek.authent.tests.factories import StructureFactory, UserProfileFactory
from geotrek.authent.tests.base import AuthentFixturesTest
Expand Down Expand Up @@ -160,6 +161,18 @@ def test_detail_sensitivearea_regulatory(self):
response = self.client.get(url, params)
self.assertIsNone(response.json()['species_id'])

def test_lang_in_detail_page(self):
sensitivearea = SensitiveAreaFactory.create(description_fr='une zone sensible', description_en='a sentive area')
self.client.force_login(SuperUserFactory())

response = self.client.get(sensitivearea.get_detail_url() + '?lang=fr')
self.assertContains(response, 'une zone sensible')
self.assertNotContains(response, 'a sentive area')

response = self.client.get(sensitivearea.get_detail_url() + '?lang=en')
self.assertContains(response, 'a sentive area')
self.assertNotContains(response, 'une zone sensible')

@override_settings(SENSITIVITY_OPENAIR_SPORT_PRACTICES=['Practice1', ])
def test_list_sensitivearea(self):
url = reverse('apiv2:sensitivearea-list')
Expand Down
8 changes: 8 additions & 0 deletions geotrek/sensitivity/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.db.models import F, Case, When
from django.http import Http404, HttpResponse
from django.shortcuts import get_object_or_404
from django.utils import translation
from django.utils.translation import ugettext_lazy as _
from django.views.generic import ListView
from django.views.generic.detail import BaseDetailView
Expand Down Expand Up @@ -53,6 +54,13 @@ class SensitiveAreaFormatList(MapEntityFormat, SensitiveAreaList):
class SensitiveAreaDetail(MapEntityDetail):
queryset = SensitiveArea.objects.existing()

def dispatch(self, *args, **kwargs):
lang = self.request.GET.get('lang')
if lang:
translation.activate(lang)
self.request.LANGUAGE_CODE = lang
return super().dispatch(*args, **kwargs)

def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context['can_edit'] = self.object.same_structure(self.request.user)
Expand Down
11 changes: 11 additions & 0 deletions geotrek/signage/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,17 @@ def test_content_in_detail_page(self):
self.assertContains(response, "<b>Beautiful !</b>")
self.assertContains(response, "(WGS 84 / Pseudo-Mercator)")

def test_lang_in_detail_page(self):
signage = SignageFactory.create(name_fr='une signalétique', name_en='a signage')

response = self.client.get(signage.get_detail_url() + '?lang=fr')
self.assertContains(response, 'une signalétique')
self.assertNotContains(response, 'a signage')

response = self.client.get(signage.get_detail_url() + '?lang=en')
self.assertContains(response, 'a signage')
self.assertNotContains(response, 'une signalétique')

def test_check_structure_or_none_related_are_visible(self):
signagetype = SignageTypeFactory.create(structure=None)
response = self.client.get(self.model.get_add_url())
Expand Down
8 changes: 8 additions & 0 deletions geotrek/signage/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.conf import settings
from django.contrib.gis.db.models.functions import Transform
from django.http import HttpResponse
from django.utils import translation
from django.utils.functional import classproperty
from mapentity.views import (MapEntityList, MapEntityFormat, MapEntityDetail,
MapEntityDocument, MapEntityCreate, MapEntityUpdate, MapEntityDelete)
Expand Down Expand Up @@ -50,6 +51,13 @@ class SignageFormatList(MapEntityFormat, SignageList):
class SignageDetail(MapEntityDetail):
queryset = Signage.objects.existing()

def dispatch(self, *args, **kwargs):
lang = self.request.GET.get('lang')
if lang:
translation.activate(lang)
self.request.LANGUAGE_CODE = lang
return super().dispatch(*args, **kwargs)

def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context['can_edit'] = self.get_object().same_structure(self.request.user)
Expand Down
25 changes: 25 additions & 0 deletions geotrek/tourism/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from django.urls import reverse
from embed_video.backends import detect_backend
from paperclip.models import random_suffix_regexp
from mapentity.tests.factories import SuperUserFactory

from geotrek.authent.tests.base import AuthentFixturesTest
from geotrek.authent.tests.factories import (StructureFactory, UserFactory,
Expand Down Expand Up @@ -360,6 +361,18 @@ def test_category(self):
"type2_label": self.content.type2_label,
"pictogram": os.path.join(settings.MEDIA_URL, self.category.pictogram.name)})

def test_lang_in_detail_page(self):
touristic_content = TouristicContentFactory.create(name_fr='un contenu touristique', name_en='a touristic content')
self.client.force_login(SuperUserFactory())

response = self.client.get(touristic_content.get_detail_url() + '?lang=fr')
self.assertContains(response, 'un contenu touristique')
self.assertNotContains(response, 'a touristic content')

response = self.client.get(touristic_content.get_detail_url() + '?lang=en')
self.assertContains(response, 'a touristic content')
self.assertNotContains(response, 'un contenu touristique')


class TouristicEventAPITest(BasicJSONAPITest, TrekkingManagerTest):
factory = TouristicEventFactory
Expand Down Expand Up @@ -398,6 +411,18 @@ def test_category(self):
"type1_label": "Type",
"pictogram": "/static/tourism/touristicevent.svg"})

def test_lang_in_detail_page(self):
touristic_event = TouristicContentFactory.create(name_fr='un évenement touristique', name_en='a touristic event')
self.client.force_login(SuperUserFactory())

response = self.client.get(touristic_event.get_detail_url() + '?lang=fr')
self.assertContains(response, 'un évenement touristique')
self.assertNotContains(response, 'a touristic event')

response = self.client.get(touristic_event.get_detail_url() + '?lang=en')
self.assertContains(response, 'a touristic event')
self.assertNotContains(response, 'un évenement touristique')


class TouristicEventViewsSameStructureTests(AuthentFixturesTest):
@classmethod
Expand Down
15 changes: 15 additions & 0 deletions geotrek/tourism/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.db.models import Q, Sum
from django.http import Http404
from django.shortcuts import get_object_or_404
from django.utils import translation
from django.utils.translation import gettext as _
from django.views.generic import DetailView
from django_filters.rest_framework import DjangoFilterBackend
Expand Down Expand Up @@ -65,6 +66,13 @@ class TouristicContentFormatList(MapEntityFormat, TouristicContentList):
class TouristicContentDetail(CompletenessMixin, MapEntityDetail):
queryset = TouristicContent.objects.existing()

def dispatch(self, *args, **kwargs):
lang = self.request.GET.get('lang')
if lang:
translation.activate(lang)
self.request.LANGUAGE_CODE = lang
return super().dispatch(*args, **kwargs)

def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context['can_edit'] = self.get_object().same_structure(self.request.user)
Expand Down Expand Up @@ -217,6 +225,13 @@ def get_queryset(self):
class TouristicEventDetail(CompletenessMixin, MapEntityDetail):
queryset = TouristicEvent.objects.existing().select_related('place', 'cancellation_reason').prefetch_related('participants')

def dispatch(self, *args, **kwargs):
lang = self.request.GET.get('lang')
if lang:
translation.activate(lang)
self.request.LANGUAGE_CODE = lang
return super().dispatch(*args, **kwargs)

def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context['can_edit'] = self.get_object().same_structure(self.request.user)
Expand Down
22 changes: 22 additions & 0 deletions geotrek/trekking/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,17 @@ def test_pois_on_treks_not_public_anonymous(self):
reverse('trekking:trek_poi_geojson', kwargs={'lang': translation.get_language(), 'pk': trek.pk}))
self.assertEqual(response.status_code, 404)

def test_lang_in_detail_page(self):
poi = POIFactory.create(name_fr='un POI', name_en='a POI')

response = self.client.get(poi.get_detail_url() + '?lang=fr')
self.assertContains(response, 'un POI')
self.assertNotContains(response, 'a POI')

response = self.client.get(poi.get_detail_url() + '?lang=en')
self.assertContains(response, 'a POI')
self.assertNotContains(response, 'un POI')


class TrekViewsTest(GeotrekAPITestCase, CommonTest):
model = Trek
Expand Down Expand Up @@ -484,6 +495,17 @@ def test_detail_lother_language(self):
form = self.get_form(response)
self.assertEqual(form.data['parking_location'], bad_data['parking_location'])

def test_lang_in_detail_page(self):
trek = TrekFactory.create(name_fr='un itinéraire', name_en='a trek')

response = self.client.get(trek.get_detail_url() + '?lang=fr')
self.assertContains(response, 'un itinéraire')
self.assertNotContains(response, 'a trek')

response = self.client.get(trek.get_detail_url() + '?lang=en')
self.assertContains(response, 'a trek')
self.assertNotContains(response, 'un itinéraire')

def test_list_in_csv(self):
if self.model is None:
return # Abstract test should not run
Expand Down
7 changes: 7 additions & 0 deletions geotrek/trekking/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,13 @@ class POIDetail(CompletenessMixin, MapEntityDetail):
queryset=HDViewPoint.objects.select_related('content_type', 'license'))
)

def dispatch(self, *args, **kwargs):
lang = self.request.GET.get('lang')
if lang:
translation.activate(lang)
self.request.LANGUAGE_CODE = lang
return super().dispatch(*args, **kwargs)

def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context['can_edit'] = self.get_object().same_structure(self.request.user)
Expand Down

0 comments on commit 698c287

Please sign in to comment.