Repository: GeotrekCE/Geotrek-admin

Issue:
Add POIs on trek GPX
- name
- description



Discussion:

Resolving patch:
diff --git a/geotrek/trekking/serializers.py b/geotrek/trekking/serializers.py
new file mode 100644
--- /dev/null
+++ b/geotrek/trekking/serializers.py
@@ -0,0 +1,15 @@
+import gpxpy
+
+from mapentity.serializers import GPXSerializer
+
+
+class TrekGPXSerializer(GPXSerializer):
+    def end_object(self, trek):
+        super(TrekGPXSerializer, self).end_object(trek)
+        for poi in trek.pois.all():
+            wpt = gpxpy.gpx.GPXWaypoint(latitude=poi.geom.y,
+                                        longitude=poi.geom.x,
+                                        elevation=poi.geom.z)
+            wpt.name = u"%s: %s" % (poi.type, poi.name)
+            wpt.description = poi.description
+            self.gpx.waypoints.append(wpt)
diff --git a/geotrek/trekking/views.py b/geotrek/trekking/views.py
--- a/geotrek/trekking/views.py
+++ b/geotrek/trekking/views.py
@@ -12,7 +12,7 @@
 from mapentity.views import (MapEntityLayer, MapEntityList, MapEntityJsonList, MapEntityFormat,
                              MapEntityDetail, MapEntityMapImage, MapEntityDocument, MapEntityCreate, MapEntityUpdate, MapEntityDelete,
                              LastModifiedMixin, JSONResponseMixin, DocumentConvert)
-from mapentity.serializers import plain_text, GPXSerializer
+from mapentity.serializers import plain_text
 from paperclip.models import Attachment
 
 from geotrek.core.models import AltimetryMixin
@@ -22,6 +22,7 @@
 from .models import Trek, POI, WebLink
 from .filters import TrekFilterSet, POIFilterSet
 from .forms import TrekForm, TrekRelationshipFormSet, POIForm, WebLinkCreateFormPopup
+from .serializers import TrekGPXSerializer
 
 
 class FlattenPicturesMixin(object):
@@ -110,7 +111,7 @@ def dispatch(self, *args, **kwargs):
         return super(TrekGPXDetail, self).dispatch(*args, **kwargs)
 
     def render_to_response(self, context):
-        gpx_serializer = GPXSerializer()
+        gpx_serializer = TrekGPXSerializer()
         response = HttpResponse(mimetype='application/gpx+xml')
         response['Content-Disposition'] = 'attachment; filename=trek-%s.gpx' % self.get_object().pk
         gpx_serializer.serialize([self.get_object()], stream=response, geom_field='geom')


Test patch:
diff --git a/geotrek/trekking/tests/test_views.py b/geotrek/trekking/tests/test_views.py
--- a/geotrek/trekking/tests/test_views.py
+++ b/geotrek/trekking/tests/test_views.py
@@ -191,25 +191,6 @@ def test_pois_geojson(self):
         poifeature = poislayer['features'][0]
         self.assertTrue('thumbnail' in poifeature['properties'])
 
-    def test_gpx(self):
-        trek = TrekWithPOIsFactory.create()
-        trek.description_en = 'Nice trek'
-        trek.description_it = 'Bonnito iti'
-        trek.description_fr = 'Jolie rando'
-        trek.save()
-
-        url = reverse('trekking:trek_gpx_detail', kwargs={'pk': trek.pk})
-        response = self.client.get(url, HTTP_ACCEPT_LANGUAGE='it-IT')
-        self.assertEqual(response.status_code, 200)
-        self.assertEqual(response['Content-Type'], 'application/gpx+xml')
-
-        parsed = BeautifulSoup(response.content)
-        self.assertEqual(len(parsed.findAll('rte')), 1)
-        self.assertEqual(len(parsed.findAll('rtept')), 2)
-        route = parsed.findAll('rte')[0]
-        description = route.find('desc').string
-        self.assertTrue(description.startswith(trek.description_it))
-
     def test_kml(self):
         trek = TrekWithPOIsFactory.create()
         url = reverse('trekking:trek_kml_detail', kwargs={'pk': trek.pk})
@@ -286,6 +267,45 @@ def test_weblink_popup(self):
         self.assertEqual(response.status_code, 200)
 
 
+class TrekGPXTest(TrekkingManagerTest):
+
+    def setUp(self):
+        self.login()
+
+        self.trek = TrekWithPOIsFactory.create()
+        self.trek.description_en = 'Nice trek'
+        self.trek.description_it = 'Bonnito iti'
+        self.trek.description_fr = 'Jolie rando'
+        self.trek.save()
+
+        url = reverse('trekking:trek_gpx_detail', kwargs={'pk': self.trek.pk})
+        self.response = self.client.get(url, HTTP_ACCEPT_LANGUAGE='it-IT')
+        self.parsed = BeautifulSoup(self.response.content)
+
+    def test_gpx_is_served_with_content_type(self):
+        self.assertEqual(self.response.status_code, 200)
+        self.assertEqual(self.response['Content-Type'], 'application/gpx+xml')
+
+    def test_gpx_trek_as_route_points(self):
+        self.assertEqual(len(self.parsed.findAll('rte')), 1)
+        self.assertEqual(len(self.parsed.findAll('rtept')), 2)
+
+    def test_gpx_translated_using_accept_language(self):
+        route = self.parsed.findAll('rte')[0]
+        description = route.find('desc').string
+        self.assertTrue(description.startswith(self.trek.description_it))
+
+    def test_gpx_contains_pois(self):
+        waypoints = self.parsed.findAll('wpt')
+        pois = self.trek.pois.all()
+        self.assertEqual(len(waypoints), len(pois))
+        waypoint = waypoints[0]
+        name = waypoint.find('name').string
+        description = waypoint.find('desc').string
+        self.assertEqual(name, u"%s: %s" % (pois[0].type, pois[0].name))
+        self.assertEqual(description, pois[0].description)
+
+
 class TrekViewTranslationTest(TrekkingManagerTest):
     def setUp(self):
         self.trek = TrekFactory.build()