Skip to content

Commit

Permalink
🐛 [BUG] ApidaeTrekParser now fallbacks on trace filename extension if

Browse files Browse the repository at this point in the history

 no extension property
  • Loading branch information
marcantoinedupre committed Sep 2, 2024
1 parent ad5cca0 commit 642b30c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
5 changes: 5 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ CHANGELOG
- Fix swapped plural and singular translations for Annotation Categories (#4032)
- Filter out deleted services in API responses (#4284)

**Bug fixes**

- ApidaeTrekParser now fallbacks on trace filename extension if no extension property


2.109.1 (2024-08-22)
----------------------------

Expand Down
18 changes: 16 additions & 2 deletions geotrek/trekking/parsers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from pathlib import PurePath

import io
import json
import re
Expand Down Expand Up @@ -647,7 +649,7 @@ def filter_geom(self, src, val):
plan = self._find_first_plan_with_supported_file_extension(val, supported_extensions)
geom_file = self._fetch_geometry_file(plan)

ext = plan['traductionFichiers'][0]['extension']
ext = self._get_plan_extension(plan)
if ext == 'gpx':
return ApidaeTrekParser._get_geom_from_gpx(geom_file)
elif ext == 'kml':
Expand Down Expand Up @@ -902,14 +904,26 @@ def _find_first_plan_with_supported_file_extension(items, supported_extensions):
plans = [item for item in items if item['type'] == 'PLAN']
if not plans:
raise RowImportError('The trek from APIDAE has no attachment with the type "PLAN"')
supported_plans = [plan for plan in plans if plan['traductionFichiers'][0]['extension'] in supported_extensions]
supported_plans = [plan for plan in plans if
ApidaeTrekParser._get_plan_extension(plan) in supported_extensions]
if not supported_plans:
raise RowImportError(
"The trek from APIDAE has no attached \"PLAN\" in a supported format. "
f"Supported formats are : {', '.join(supported_extensions)}"
)
return supported_plans[0]

@staticmethod
def _get_plan_extension(plan):
info_fichier = plan['traductionFichiers'][0]
extension_prop = info_fichier.get('extension')
if extension_prop:
return extension_prop
url_suffix = PurePath(info_fichier['url']).suffix
if url_suffix:
return url_suffix.split('.')[1]
return None

Check warning on line 925 in geotrek/trekking/parsers.py

View check run for this annotation

Codecov / codecov/patch

geotrek/trekking/parsers.py#L925

Added line #L925 was not covered by tests

@staticmethod
def _get_geom_from_gpx(data):
"""Given GPX data as bytes it returns a geom."""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"numFound": 1,
"objetsTouristiques": [
{
"id": 123123,
"multimedias": [
{
"type": "PLAN",
"traductionFichiers": [
{
"url": "https://example.net/trace.kml"
}
]
}
],
"nom": {
"libelleFr": "Une belle randonnée de test avec un plan sans extension (déduite du filename)"
}
}
]
}
13 changes: 13 additions & 0 deletions geotrek/trekking/tests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,19 @@ def test_trek_not_imported_when_no_supported_format(self, mocked_get):
self.assertEqual(Trek.objects.count(), 0)
self.assertIn('has no attached "PLAN" in a supported format', output_stdout.getvalue())

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

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

self.assertEqual(Trek.objects.count(), 1)
trek = Trek.objects.all().first()
self.assertEqual(trek.geom.srid, 2154)
self.assertEqual(len(trek.geom.coords), 61)

@mock.patch('requests.get')
def test_trek_not_imported_when_no_plan_attached(self, mocked_get):
output_stdout = StringIO()
Expand Down

0 comments on commit 642b30c

Please sign in to comment.