Skip to content

Commit

Permalink
🐛 [BUG] Fix schema_randonnee parser url update issues (#4022)
Browse files Browse the repository at this point in the history
- fix url being duplicated in description after new import if description is null or was not updated
- description and url are now parsed in a filter_description method rather than in the end method
  • Loading branch information
justinefricou committed Nov 22, 2024
1 parent a763f53 commit 226a9d4
Show file tree
Hide file tree
Showing 10 changed files with 240 additions and 17 deletions.
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ CHANGELOG
**Bug fixes**

- Fix missing Dockerfile path on make build scripts
- Fix SchemaRandonneeParser url update when description is null or was not updated (#4022)

**Documentation**

Expand Down
29 changes: 15 additions & 14 deletions geotrek/trekking/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1338,7 +1338,7 @@ class SchemaRandonneeParser(AttachmentParserMixin, Parser):
'arrival': 'arrivee',
'duration': 'duree',
'difficulty': 'difficulte',
'description': 'instructions',
'description': ('instructions', 'url'),
'ambiance': 'presentation',
'description_teaser': 'presentation_courte',
'advice': 'recommandations',
Expand Down Expand Up @@ -1391,14 +1391,12 @@ class SchemaRandonneeParser(AttachmentParserMixin, Parser):
'attachments': 'medias',
'id_local': 'id_local',
'itineraire_parent': 'itineraire_parent',
'url': 'url',
}

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.related_treks_mapping = defaultdict(list)
self.id_local_to_pk_mapping = {}
self.trek_urls = {}
if self.url:
response = self.request_or_retry(self.url)
self.root = response.json()
Expand Down Expand Up @@ -1453,6 +1451,19 @@ def filter_parking_location(self, src, val):
except ValueError:
self.add_warning(_("Bad value for parking geometry: should be a Point"))

def filter_description(self, src, val):
instructions, url = val
if not instructions and not url:
return None
description = ""
if instructions:
description += instructions
if instructions and url:
description += "\n\n"
if url:
description += f'<a href={url}>{url}</a>'
return description

def filter_attachments(self, src, val):
"""Handles images only"""
if val is None:
Expand Down Expand Up @@ -1548,10 +1559,6 @@ def get_or_create_license(self, license_label):
self.add_warning(_("License '{val}' does not exist in Geotrek-Admin. Please add it").format(val=license_label))
return license

def save_url(self, src, val):
if val:
self.trek_urls[self.obj.pk] = val

def save_id_local(self, src, val):
if val:
self.id_local_to_pk_mapping[val] = self.obj.pk
Expand All @@ -1562,14 +1569,8 @@ def save_itineraire_parent(self, src, val):

def end(self):
"""
After all treks have been created:
- concatenate the `url` field into the description
- add children
After all treks have been created, add parent/children relationships
"""
for trek_pk, trek_url in self.trek_urls.items():
trek = Trek.objects.get(pk=trek_pk)
trek.description = f'{trek.description}\n\n<a href={trek_url}>{trek_url}</a>'
trek.save()
for parent_id_local, child_pks in self.related_treks_mapping.items():
parent_pk = self.id_local_to_pk_mapping.get(parent_id_local)
try:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"type": "FeatureCollection",
"name": "sql_statement",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{
"type": "Feature",
"properties": {
"id_local": "1",
"producteur": "Producer 1",
"nom_itineraire": "Trek 1",
"pratique": "pédestre",
"depart": "Departure 1",
"arrivee": "Arrival 1",
"instructions": "Instructions 1",
"url": "https://test.com"
},
"geometry": {
"type": "LineString",
"coordinates": [ [ 6.449592517966364, 44.733424655086957 ], [ 6.449539623508488, 44.733394939411369 ] ]
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"type": "FeatureCollection",
"name": "sql_statement",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{
"type": "Feature",
"properties": {
"id_local": "1",
"producteur": "Producer 1",
"nom_itineraire": "Trek 1",
"pratique": "pédestre",
"depart": "Departure 1",
"arrivee": "Arrival 1",
"instructions": "Instructions 1",
"url": null
},
"geometry": {
"type": "LineString",
"coordinates": [ [ 6.449592517966364, 44.733424655086957 ], [ 6.449539623508488, 44.733394939411369 ] ]
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"type": "FeatureCollection",
"name": "sql_statement",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{
"type": "Feature",
"properties": {
"id_local": "1",
"producteur": "Producer 1",
"url": "https://test.com",
"nom_itineraire": "Trek 1",
"pratique": "pédestre",
"depart": "Departure 1",
"arrivee": "Arrival 1",
"instructions": "Instructions 1"
},
"geometry": {
"type": "LineString",
"coordinates": [ [ 6.449592517966364, 44.733424655086957 ], [ 6.449539623508488, 44.733394939411369 ] ]
}
},
{
"type": "Feature",
"properties": {
"id_local": "2",
"producteur": "Producer 1",
"nom_itineraire": "Trek 1",
"pratique": "pédestre",
"depart": "Departure 1",
"arrivee": "Arrival 1",
"instructions": "Instructions 2"
},
"geometry": {
"type": "LineString",
"coordinates": [ [ 6.449592517966364, 44.733424655086957 ], [ 6.449539623508488, 44.733394939411369 ] ]
}
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"pratique": "pédestre",
"depart": "Departure 1",
"arrivee": "Arrival 1",
"instructions": "Instructions 1"
"instructions": null,
"url": null
},
"geometry": {
"type": "LineString",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"type": "FeatureCollection",
"name": "sql_statement",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{
"type": "Feature",
"properties": {
"id_local": "1",
"producteur": "Producer 1",
"url": "https://test.com",
"nom_itineraire": "Trek 1",
"pratique": "pédestre",
"depart": "Departure 1",
"arrivee": "Arrival 1",
"instructions": null
},
"geometry": {
"type": "LineString",
"coordinates": [ [ 6.449592517966364, 44.733424655086957 ], [ 6.449539623508488, 44.733394939411369 ] ]
}
},
{
"type": "Feature",
"properties": {
"id_local": "2",
"producteur": "Producer 1",
"url": "https://test2.com",
"nom_itineraire": "Trek 2",
"pratique": "pédestre",
"depart": "Departure 1",
"arrivee": "Arrival 1",
"instructions": "Instructions 2"
},
"geometry": {
"type": "LineString",
"coordinates": [ [ 6.449592517966364, 44.733424655086957 ], [ 6.449539623508488, 44.733394939411369 ] ]
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"type": "FeatureCollection",
"name": "sql_statement",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{
"type": "Feature",
"properties": {
"id_local": "1",
"producteur": "Producer 1",
"url": "https://test.com",
"nom_itineraire": "Trek 1",
"pratique": "pédestre",
"depart": "Departure 1",
"arrivee": "Arrival 1",
"instructions": "Instructions 1"
},
"geometry": {
"type": "LineString",
"coordinates": [ [ 6.449592517966364, 44.733424655086957 ], [ 6.449539623508488, 44.733394939411369 ] ]
}
},
{
"type": "Feature",
"properties": {
"id_local": "2",
"producteur": "Producer 1",
"url": "https://test1.com",
"nom_itineraire": "Trek 2",
"pratique": "pédestre",
"depart": "Departure 1",
"arrivee": "Arrival 1",
"instructions": "Instructions 2"
},
"geometry": {
"type": "LineString",
"coordinates": [ [ 6.449592517966364, 44.733424655086957 ], [ 6.449539623508488, 44.733394939411369 ] ]
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"type": "FeatureCollection",
"name": "sql_statement",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{
"type": "Feature",
"properties": {
"id_local": "1",
"producteur": "Producer 1",
"nom_itineraire": "Trek 1",
"pratique": "pédestre",
"depart": "Departure 1",
"arrivee": "Arrival 1",
"instructions": null,
"url": "https://test.com"
},
"geometry": {
"type": "LineString",
"coordinates": [ [ 6.449592517966364, 44.733424655086957 ], [ 6.449539623508488, 44.733394939411369 ] ]
}
}
]
}
30 changes: 28 additions & 2 deletions geotrek/trekking/tests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2030,8 +2030,34 @@ def test_incorrect_parking_location(self):
self.assertIsNone(trek.parking_location)
self.assertIn("Bad value for parking geometry: should be a Point", output.getvalue())

def test_no_url(self):
self.call_import_command_with_file('no_url.geojson')
def test_description_and_url(self):
self.call_import_command_with_file('description_and_url.geojson')
self.assertEqual(Trek.objects.count(), 1)
trek = Trek.objects.get()
self.assertEqual(trek.description, 'Instructions 1\n\n<a href=https://test.com>https://test.com</a>')

def test_description_no_url(self):
self.call_import_command_with_file('description_no_url.geojson')
self.assertEqual(Trek.objects.count(), 1)
trek = Trek.objects.get()
self.assertEqual(trek.description, 'Instructions 1')

def test_url_no_description(self):
self.call_import_command_with_file('url_no_description.geojson')
self.assertEqual(Trek.objects.count(), 1)
trek = Trek.objects.get()
self.assertEqual(trek.description, '<a href=https://test.com>https://test.com</a>')

def test_no_description_no_url(self):
self.call_import_command_with_file('no_description_no_url.geojson')
self.assertEqual(Trek.objects.count(), 1)
trek = Trek.objects.get()
self.assertEqual(trek.description, '')

def test_update_url(self):
self.call_import_command_with_file('update_url_before.geojson')
self.call_import_command_with_file('update_url_after.geojson')
trek1 = Trek.objects.get(eid="1")
self.assertEqual(trek1.description, "<a href=https://test.com>https://test.com</a>")
trek2 = Trek.objects.get(eid="2")
self.assertEqual(trek2.description, "Instructions 2\n\n<a href=https://test2.com>https://test2.com</a>")

0 comments on commit 226a9d4

Please sign in to comment.