Skip to content

Commit

Permalink
Merge 4353021 into 9ad3e59
Browse files Browse the repository at this point in the history
  • Loading branch information
LePetitTim authored Jan 17, 2023
2 parents 9ad3e59 + 4353021 commit 868b95e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 15 deletions.
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ In preparation for HD Views developments (PR #3298)
- Modify site's geometry before saving to avoid edition and export of shapefiles (#3399)
- Fix API V2 cache key with X-Forwarded-Proto header (#3404)
- Check pictogram exist on categories during generation of pdfs
- Fix ApidaeParsers does not update every time


2.94.0 (2022-12-12)
Expand Down
30 changes: 19 additions & 11 deletions geotrek/common/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ def parse_real_field(self, dst, src, val):
def parse_translation_field(self, dst, src, val):
"""Specific treatment for translated fields
TODO: check self.default_language to get default values
TODO: handle flow with a field for each language (ex: APIDAE)
TODO: compare each translated fields with source fields :
this only compares old 'name' with new 'name' but it should compare
- old 'name_en' with new 'name_en',
Expand All @@ -252,21 +251,30 @@ def parse_translation_field(self, dst, src, val):
"""
val = val or ""
modified = False

old_values = {}
# We keep every old values for each langs to get tracability during filter or apply filter
for lang in settings.MODELTRANSLATION_LANGUAGES:
dst_field_lang = '{field}_{lang}'.format(field=dst, lang=lang)
old_values[lang] = getattr(self.obj, dst_field_lang)
# If during filter, the traduction of the field has been changed
# we can still check if this value has been changed
if hasattr(self, 'filter_{0}'.format(dst)):
val = getattr(self, 'filter_{0}'.format(dst))(src, val)
val_default_language = getattr(self, 'filter_{0}'.format(dst))(src, val)
else:
val = self.apply_filter(dst, src, val)
val_default_language = self.apply_filter(dst, src, val)

for lang in settings.MODELTRANSLATION_LANGUAGES:
dst_field_lang = '{field}_{lang}'.format(field=dst, lang=lang)
old = getattr(self.obj, dst_field_lang)
new_value = getattr(self.obj, dst_field_lang)
old_value = old_values[lang]
# Field not translated, use same val for all translated
val = val or ""

if old != val:
# Set dst_field_lang only if empty
if not old:
self.set_value(dst_field_lang, src, val)
val_default_language = val_default_language or ""
# Set val_default_language only if new empty
if not new_value:
# If there is no new value check if old value is different form the default value
# If this is the same, it means old value was empty and was fill with default value in previous import
if old_value != val_default_language:
self.set_value(dst_field_lang, src, val_default_language)
modified = True
return modified

Expand Down
6 changes: 4 additions & 2 deletions geotrek/trekking/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,8 @@ def _find_gpx_plan_in_multimedia_items(items):
plans = list(filter(lambda item: item['type'] == 'PLAN', items))
if len(plans) > 1:
raise RowImportError("APIDAE Trek has more than one map defined")
if len(plans) == 0:
raise RowImportError("APIDAE Trek has no map defined")
return plans[0]

@staticmethod
Expand Down Expand Up @@ -1007,9 +1009,9 @@ def _make_duration(duration_in_minutes=None, duration_in_days=None):
"""Returns the duration in hours. The method expects one argument or the other, not both. If both arguments have
non-zero values the method only considers `duration_in_minutes` and discards `duration_in_days`."""
if duration_in_minutes:
return (Decimal(duration_in_minutes) / Decimal(60)).quantize(Decimal('.01'))
return float((Decimal(duration_in_minutes) / Decimal(60)).quantize(Decimal('.01')))
elif duration_in_days:
return duration_in_days * 24
return float(duration_in_days * 24)
else:
return None

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"numFound": 1,
"objetsTouristiques": [
{
"id": 123123,
"multimedias": [],
"nom": {
"libelleFr": "Une belle randonnée de test sans plan..."
}
}
]
}
14 changes: 12 additions & 2 deletions geotrek/trekking/tests/test_parsers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from datetime import date
from decimal import Decimal
import json
import os
from copy import copy
Expand Down Expand Up @@ -818,6 +817,17 @@ def test_trek_not_imported_when_no_gpx_file(self, mocked_get):
self.assertEqual(Trek.objects.count(), 0)
self.assertIn('pas au format GPX', output_stdout.getvalue())

@mock.patch('requests.get')
def test_trek_not_imported_when_no_plan(self, mocked_get):
output_stdout = StringIO()
mocked_get.side_effect = self.make_dummy_get('trek_no_plan_error.json')

call_command('import', 'geotrek.trekking.tests.test_parsers.TestApidaeTrekParser', verbosity=2,
stdout=output_stdout)

self.assertEqual(Trek.objects.count(), 0)
self.assertIn('APIDAE Trek has no map defined', output_stdout.getvalue())

@mock.patch('requests.get')
def test_trek_linked_entities_are_imported(self, mocked_get):
mocked_get.side_effect = self.make_dummy_get('a_trek.json')
Expand Down Expand Up @@ -1191,7 +1201,7 @@ def test_giving_both_duration_arguments_only_duration_in_minutes_is_considered(s
self.assertAlmostEqual(ApidaeTrekParser._make_duration(duration_in_minutes=90, duration_in_days=0.5), 1.5)

def test_it_rounds_output_to_two_decimal_places(self):
self.assertEqual(Decimal(ApidaeTrekParser._make_duration(duration_in_minutes=20)), Decimal('0.33'))
self.assertEqual(ApidaeTrekParser._make_duration(duration_in_minutes=20), 0.33)


class TestApidaePOIParser(ApidaePOIParser):
Expand Down

0 comments on commit 868b95e

Please sign in to comment.