Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix apidae parser should not update every time #3408

Merged
merged 6 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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