Skip to content

Commit

Permalink
💫 [IMPR] Fix sublanguage usage (refs #3801)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chatewgne committed Jan 11, 2024
1 parent 4501b12 commit 029abe7
Show file tree
Hide file tree
Showing 38 changed files with 162 additions and 138 deletions.
1 change: 0 additions & 1 deletion conf/env.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ POSTGRES_PASSWORD="${POSTGRES_PASSWORD}"
POSTGRES_DB="${POSTGRES_DB}"
SRID="${SRID}"
LANGUAGES="${LANGUAGES}"
LANGUAGE_CODE="${LANGUAGE_CODE}"
TIME_ZONE="${TIME_ZONE}"
SERVER_NAME="${SERVER_NAME}"
RANDO_SERVER_NAME="${RANDO_SERVER_NAME}"
Expand Down
27 changes: 14 additions & 13 deletions geotrek/api/management/commands/sync_mobile.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import argparse
import logging
import filecmp
import logging
import os
import stat
from PIL import Image
import re
import shutil
import stat
import tempfile
from time import sleep
from zipfile import ZipFile
import cairosvg

import cairosvg
from django.conf import settings
from django.contrib.auth.models import AnonymousUser
from django.core.management.base import BaseCommand, CommandError
Expand All @@ -19,19 +18,21 @@
from django.test.client import RequestFactory
from django.utils import translation
from django.utils.translation import gettext as _
from geotrek.common.models import FileType # NOQA
from modeltranslation.utils import build_localized_fieldname
from PIL import Image

from geotrek.api.mobile.views.common import FlatPageViewSet, SettingsView
from geotrek.api.mobile.views.trekking import TrekViewSet
from geotrek.common import models as common_models
from geotrek.common.functions import GeometryType
from geotrek.common.helpers_sync import ZipTilesBuilder
from geotrek.common.models import FileType # NOQA
from geotrek.flatpages.models import FlatPage
from geotrek.tourism import models as tourism_models
from geotrek.trekking import models as trekking_models
from geotrek.api.mobile.views.trekking import TrekViewSet
from geotrek.api.mobile.views.common import FlatPageViewSet, SettingsView
from geotrek.common.helpers_sync import ZipTilesBuilder
from geotrek.tourism import urls # NOQA
# Register mapentity models
from geotrek.trekking import models as trekking_models
from geotrek.trekking import urls # NOQA
from geotrek.tourism import urls # NOQA


logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -237,7 +238,7 @@ def close_zip(self, zipfile, name):

def sync_flatpage(self, lang):
flatpages = FlatPage.objects.order_by('pk').filter(target__in=['mobile', 'all']).filter(
**{'published_{lang}'.format(lang=lang): True})
**{build_localized_fieldname('published', lang): True})
if self.portal:
flatpages = flatpages.filter(Q(portal__name__in=self.portal) | Q(portal=None))
self.sync_json(lang, FlatPageViewSet, 'flatpages',
Expand All @@ -249,7 +250,7 @@ def sync_flatpage(self, lang):
def sync_trekking(self, lang):
self.sync_geojson(lang, TrekViewSet, 'treks.geojson', type_view={'get': 'list'})
treks = trekking_models.Trek.objects.annotate(geom_type=GeometryType("geom")).filter(geom_type="LINESTRING").existing().order_by('pk')
treks = treks.filter(**{'published_{lang}'.format(lang=lang): True})
treks = treks.filter(**{build_localized_fieldname('published', lang): True})

if self.portal:
treks = treks.filter(Q(portal__name__in=self.portal) | Q(portal=None))
Expand Down
46 changes: 26 additions & 20 deletions geotrek/api/tests/test_mobile/test_sync_mobile.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,42 @@
import errno
from io import StringIO

import json
from landez.sources import DownloadError
from unittest import mock
import os
from PIL import Image
import shutil
from unittest import skipIf
import zipfile
from io import StringIO
from unittest import mock, skipIf

from django.conf import settings
from django.contrib.gis.geos import MultiLineString, LineString, Point
from django.contrib.gis.geos import LineString, MultiLineString, Point
from django.core import management
from django.core.management.base import CommandError
from django.db.models import Q
from django.http import HttpResponse, StreamingHttpResponse
from django.test import TestCase
from django.test.utils import override_settings
from django.utils import translation
from landez.sources import DownloadError
from modeltranslation.utils import build_localized_fieldname
from PIL import Image

from geotrek.common.tests.factories import RecordSourceFactory, TargetPortalFactory, AttachmentFactory
from geotrek.common.tests import TranslationResetMixin
from geotrek.common.utils.testdata import get_dummy_uploaded_image_svg, get_dummy_uploaded_image, get_dummy_uploaded_file
from geotrek.common.tests.factories import (AttachmentFactory,
RecordSourceFactory,
TargetPortalFactory)
from geotrek.common.utils.testdata import (get_dummy_uploaded_file,
get_dummy_uploaded_image,
get_dummy_uploaded_image_svg)
from geotrek.core.tests.factories import PathFactory
from geotrek.flatpages.tests.factories import FlatPageFactory
from geotrek.flatpages.models import FlatPage
from geotrek.trekking.models import Trek, OrderedTrekChild
from geotrek.trekking.tests.factories import TrekFactory, TrekWithPublishedPOIsFactory, PracticeFactory
from geotrek.tourism.tests.factories import (InformationDeskFactory, InformationDeskTypeFactory,
TouristicContentFactory, TouristicEventFactory)
from geotrek.flatpages.tests.factories import FlatPageFactory
from geotrek.tourism.models import TouristicEventType
from geotrek.tourism.tests.factories import (InformationDeskFactory,
InformationDeskTypeFactory,
TouristicContentFactory,
TouristicEventFactory)
from geotrek.trekking.models import OrderedTrekChild, Trek
from geotrek.trekking.tests.factories import (PracticeFactory, TrekFactory,
TrekWithPublishedPOIsFactory)


class VarTmpTestCase(TestCase):
Expand Down Expand Up @@ -251,7 +257,7 @@ def test_sync_flatpage(self):
with open(os.path.join(os.path.join(settings.TMP_DIR, 'sync_mobile', 'tmp_sync'), lang, 'flatpages.json'), 'r') as f:
flatpages = json.load(f)
self.assertEqual(len(flatpages),
FlatPage.objects.filter(**{'published_{}'.format(lang): True}).count())
FlatPage.objects.filter(**{build_localized_fieldname('published', lang): True}).count())
self.assertIn('en/flatpages.json', output.getvalue())

def test_sync_filtering_portal(self):
Expand Down Expand Up @@ -280,7 +286,7 @@ def test_sync_flatpage_lang(self):
with open(os.path.join(os.path.join(settings.TMP_DIR, 'sync_mobile', 'tmp_sync'), lang, 'flatpages.json'), 'r') as f:
flatpages = json.load(f)
self.assertEqual(len(flatpages),
FlatPage.objects.filter(**{'published_{}'.format(lang): True}).count())
FlatPage.objects.filter(**{build_localized_fieldname('published', lang): True}).count())
self.assertIn('en/flatpages.json', output.getvalue())

def test_sync_flatpage_content(self):
Expand All @@ -291,7 +297,7 @@ def test_sync_flatpage_content(self):
with open(os.path.join(os.path.join(settings.TMP_DIR, 'sync_mobile', 'tmp_sync'), lang, 'flatpages.json'), 'r') as f:
flatpages = json.load(f)
self.assertEqual(len(flatpages),
FlatPage.objects.filter(**{'published_{}'.format(lang): True}).count())
FlatPage.objects.filter(**{build_localized_fieldname('published', lang): True}).count())
self.assertIn('en/flatpages.json', output.getvalue())


Expand Down Expand Up @@ -400,7 +406,7 @@ def test_sync_treks(self):
with open(os.path.join(os.path.join(settings.TMP_DIR, 'sync_mobile', 'tmp_sync'), lang, 'treks.geojson'), 'r') as f:
trek_geojson = json.load(f)
self.assertEqual(len(trek_geojson['features']),
Trek.objects.filter(**{'published_{}'.format(lang): True}).count())
Trek.objects.filter(**{build_localized_fieldname('published', lang): True}).count())
self.assertIn('en/treks.geojson', output.getvalue())

def test_sync_treks_by_pk(self):
Expand All @@ -426,7 +432,7 @@ def test_sync_treks_with_portal(self):
with open(os.path.join(settings.TMP_DIR, 'sync_mobile', 'tmp_sync', lang, 'treks.geojson'), 'r') as f:
trek_geojson = json.load(f)
self.assertEqual(len(trek_geojson['features']),
Trek.objects.filter(**{'published_{}'.format(lang): True})
Trek.objects.filter(**{build_localized_fieldname('published', lang): True})
.filter(Q(portal__name__in=(self.portal_a,)) | Q(portal=None)).count())
with open(os.path.join(settings.TMP_DIR, 'sync_mobile', 'tmp_sync', 'en', str(self.trek_1.pk), 'touristic_contents.geojson'), 'r') as f:
tc_geojson = json.load(f)
Expand Down Expand Up @@ -563,7 +569,7 @@ def test_multilinestring(self):
with open(os.path.join(os.path.join(settings.TMP_DIR, 'sync_mobile', 'tmp_sync'), lang, 'treks.geojson'), 'r') as f:
trek_geojson = json.load(f)
self.assertEqual(len(trek_geojson['features']),
Trek.objects.filter(**{'published_{}'.format(lang): True}).count())
Trek.objects.filter(**{build_localized_fieldname('published', lang): True}).count())

def test_sync_treks_informationdesk_photo_missing(self):
os.remove(self.info_desk.photo.path)
Expand Down
9 changes: 5 additions & 4 deletions geotrek/api/v2/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from django_filters.widgets import CSVWidget
from rest_framework.filters import BaseFilterBackend
from rest_framework_gis.filters import DistanceToPointFilter, InBBOXFilter
from modeltranslation.utils import build_localized_fieldname

from geotrek.tourism.models import TouristicEventOrganizer, TouristicContent, TouristicContentType, TouristicEvent, \
TouristicEventPlace, TouristicEventType
Expand Down Expand Up @@ -141,13 +142,13 @@ def filter_queryset(self, request, queryset, view):
# no language specified. Check for all.
q = Q()
for lang in settings.MODELTRANSLATION_LANGUAGES:
field_name = 'published_{}'.format(lang.replace('-', '_'))
field_name = build_localized_fieldname('published', lang)
if field_name in associated_published_fields:
q |= Q(**{field_name: True})
qs = qs.filter(q)
else:
# one language is specified
field_name = 'published_{}'.format(language.replace('-', '_'))
field_name = build_localized_fieldname('published', language)
qs = qs.filter(**{field_name: True})

return qs
Expand Down Expand Up @@ -991,12 +992,12 @@ def filter_queryset_related_objects_published(self, queryset, request, related_n
language = request.GET.get('language')
if language:
# one language is specified
related_field_name = '{}__published_{}'.format(related_name, language.replace('-', '_'))
related_field_name = '{}__{}'.format(related_name, build_localized_fieldname('published', language))
q &= Q(**{related_field_name: True})
else:
# no language specified. Check for all.
for lang in settings.MODELTRANSLATION_LANGUAGES:
related_field_name = '{}__published_{}'.format(related_name, lang.replace('-', '_'))
related_field_name = '{}__{}'.format(related_name, build_localized_fieldname('published', lang))
q |= Q(**{related_field_name: True})
q &= optional_query
qs = qs.filter(q)
Expand Down
5 changes: 3 additions & 2 deletions geotrek/api/v2/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.conf import settings
from modeltranslation.utils import build_localized_fieldname


def get_translation_or_dict(model_field_name, serializer, instance):
Expand All @@ -12,13 +13,13 @@ def get_translation_or_dict(model_field_name, serializer, instance):
lang = serializer.context.get('request').GET.get('language', 'all') if serializer.context.get('request') else 'all'

if lang != 'all':
data = getattr(instance, '{}_{}'.format(model_field_name, lang.replace('-', '_')))
data = getattr(instance, build_localized_fieldname(model_field_name, lang))

else:
data = {}

for language in settings.MODELTRANSLATION_LANGUAGES:
data.update({language: getattr(instance, '{}_{}'.format(model_field_name, language.replace('-', '_')), )})
data.update({language: getattr(instance, build_localized_fieldname(model_field_name, language))})

return data

Expand Down
9 changes: 5 additions & 4 deletions geotrek/api/v2/views/trekking.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from rest_framework.decorators import action
from rest_framework.generics import get_object_or_404
from rest_framework.response import Response
from modeltranslation.utils import build_localized_fieldname

from geotrek.api.v2 import filters as api_filters, serializers as api_serializers, viewsets as api_viewsets
from geotrek.api.v2.decorators import cache_response_detail
Expand Down Expand Up @@ -63,15 +64,15 @@ def filter_published_lang_retrieve(self, request, queryset):
# no language specified. Check for all.
q = Q()
for lang in settings.MODELTRANSLATION_LANGUAGES:
field_name = 'published_{}'.format(lang)
field_name = build_localized_fieldname('published', lang)
if field_name in associated_published_fields:
field_name_parent = 'trek_parents__parent__published_{}'.format(lang)
field_name_parent = 'trek_parents__parent__{}'.format(build_localized_fieldname('published', lang))
q |= Q(**{field_name: True}) | Q(**{field_name_parent: True})
qs = qs.filter(q)
else:
# one language is specified
field_name = 'published_{}'.format(language)
field_name_parent = 'trek_parents__parent__published_{}'.format(language)
field_name = build_localized_fieldname('published', language)
field_name_parent = 'trek_parents__parent__{}'.format(build_localized_fieldname('published', language))
qs = qs.filter(Q(**{field_name: True}) | Q(**{field_name_parent: True}))
return qs.distinct()

Expand Down
9 changes: 5 additions & 4 deletions geotrek/common/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from django.utils.text import format_lazy
from django.utils.translation import gettext_lazy as _
from mapentity.forms import MapEntityForm, SubmitButton
from modeltranslation.utils import build_localized_fieldname

from geotrek.authent.models import (StructureOrNoneRelated, StructureRelated,
default_structure)
Expand Down Expand Up @@ -196,7 +197,7 @@ def check_structure(self, obj, structure, name):
@property
def any_published(self):
"""Check if form has published in at least one of the language"""
return any([self.cleaned_data.get(f'published_{language[0]}', False)
return any([self.cleaned_data.get(build_localized_fieldname('published', language[0]), False)
for language in settings.MAPENTITY_CONFIG['TRANSLATED_LANGUAGES']])

@property
Expand All @@ -205,7 +206,7 @@ def published_languages(self):
"""
languages = [language[0] for language in settings.MAPENTITY_CONFIG['TRANSLATED_LANGUAGES']]
if settings.PUBLISHED_BY_LANG:
return [language for language in languages if self.cleaned_data.get(f'published_{language}', None)]
return [language for language in languages if self.cleaned_data.get(build_localized_fieldname('published', language), None)]
else:
if self.any_published:
return languages
Expand Down Expand Up @@ -237,12 +238,12 @@ def _get_missing_completeness_fields(self, completeness_fields, msg):
if field_required in translated_fields:
if self.cleaned_data.get('review') and settings.COMPLETENESS_LEVEL == 'error_on_review':
# get field for first language only
field_required_lang = f"{field_required}_{settings.MAPENTITY_CONFIG['TRANSLATED_LANGUAGES'][0][0]}"
field_required_lang = build_localized_fieldname(field_required, settings.MAPENTITY_CONFIG['TRANSLATED_LANGUAGES'][0][0])
missing_fields.append(field_required_lang)
self.add_error(field_required_lang, msg)
else:
for language in self.published_languages:
field_required_lang = f'{field_required}_{language}'
field_required_lang = build_localized_fieldname(field_required, language)
if not self.cleaned_data.get(field_required_lang):
missing_fields.append(field_required_lang)
self.add_error(field_required_lang, msg)
Expand Down
2 changes: 1 addition & 1 deletion geotrek/common/management/commands/sync_rando.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ def handle(self, *args, **options):
for language in options['languages'].split(','):
if language not in settings.MODELTRANSLATION_LANGUAGES:
raise CommandError("Language {lang_n} doesn't exist. Select in these one : {langs}".
format(lang_n=language, langs=settings.MODELTRANSLATION_LANGUAGES))
format(lang_n=language, langs=list(settings.MODELTRANSLATION_LANGUAGES)))
self.languages = options['languages'].split(',')
else:
self.languages = settings.MODELTRANSLATION_LANGUAGES
Expand Down
Loading

0 comments on commit 029abe7

Please sign in to comment.