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
- add static method to find url in description to help other parsers find it
  • Loading branch information
justinefricou committed Nov 18, 2024
1 parent e6096d7 commit 8fd0b01
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,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
14 changes: 13 additions & 1 deletion geotrek/trekking/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1569,7 +1569,11 @@ def end(self):
"""
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>'
formatted_url = f'<a data-schemarandonneeparser-url href={trek_url}>{trek_url}</a>'
if trek.description:
trek.description = f'{trek.description}\n\n{formatted_url}'
else:
trek.description = formatted_url
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)
Expand All @@ -1588,3 +1592,11 @@ def end(self):
otc.save()
order += 1
super().end()

@staticmethod
def find_url_in_description(trek):
pattern = r'<a data-schemarandonneeparser-url href=([^"]+)>[^<]*</a>'
match = re.search(pattern, trek.description)
if match:
return match.group(0)
return None
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
@@ -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 ] ]
}
}
]
}
19 changes: 18 additions & 1 deletion geotrek/trekking/tests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1861,7 +1861,7 @@ def test_create_basic_trek(self):
self.assertEqual(trek.geom.num_coords, 2)
self.assertEqual(trek.parking_location.srid, settings.SRID)
self.assertEqual(trek.parking_location.geom_type, 'Point')
self.assertEqual(trek.description, "Instructions 1\n\n<a href=https://test.com>https://test.com</a>")
self.assertEqual(trek.description, "Instructions 1\n\n<a data-schemarandonneeparser-url href=https://test.com>https://test.com</a>")

@mock.patch('geotrek.common.parsers.Parser.request_or_retry')
def test_parse_attachments(self, mocked_request_or_retry):
Expand Down Expand Up @@ -2035,3 +2035,20 @@ def test_no_url(self):
self.assertEqual(Trek.objects.count(), 1)
trek = Trek.objects.get()
self.assertEqual(trek.description, 'Instructions 1')

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 data-schemarandonneeparser-url href=https://test.com>https://test.com</a>")
trek2 = Trek.objects.get(eid="2")
self.assertEqual(trek2.description, "Instructions 2\n\n<a data-schemarandonneeparser-url href=https://test2.com>https://test2.com</a>")

def test_find_url_in_description(self):
self.call_import_command_with_file('find_url_in_description.geojson')
trek1 = Trek.objects.get(eid="1")
trek2 = Trek.objects.get(eid="2")
url1 = SchemaRandonneeParser.find_url_in_description(trek1)
self.assertEqual(url1, "<a data-schemarandonneeparser-url href=https://test.com>https://test.com</a>")
url2 = SchemaRandonneeParser.find_url_in_description(trek2)
self.assertIsNone(url2)

0 comments on commit 8fd0b01

Please sign in to comment.