Commits

Yohan Boniface  committed 921e0c8

Sort datalayer.features cf yohanboniface/Leaflet.Storage#52)

  • Participants
  • Parent commits 6a57fb4

Comments (0)

Files changed (3)

File leaflet_storage/base_models.py

 # -*- coding: utf-8 -*-
 
+from itertools import chain
+
 from django.contrib.gis.db import models
 from django.db.models import get_model as dj_get_model
 from django.conf import settings
             markers = get_model("Marker").objects.filter(**filters)
             polylines = get_model("Polyline").objects.filter(**filters)
             polygons = get_model("Polygon").objects.filter(**filters)
-            self._features = list(markers) + list(polylines) + list(polygons)
+            self._features = sorted(chain(markers, polylines, polygons), key=lambda i: i.name)
         return self._features
 
     @classmethod

File leaflet_storage/tests/base.py

 
 import factory
 
-from leaflet_storage.models import Map, TileLayer, Licence, DataLayer, Marker
+from leaflet_storage.models import Map, TileLayer, Licence, DataLayer, Marker, Polygon,\
+                                   Polyline
 from leaflet_storage.forms import DEFAULT_CENTER
 
 
     latlng = '{"type": "Point","coordinates": [-0.1318359375,51.474540439419755]}'
 
 
+class PolylineFactory(BaseFeatureFactory):
+    FACTORY_FOR = Polyline
+    latlng = '{"type": "LineString", "coordinates": [[8.756103515625, 49.55372551347579], [9.25048828125, 48.879167148960214], [8.580322265624998, 48.76343113791796], [8.3056640625, 48.980216985374994]]}'
+
+
+class PolygonFactory(BaseFeatureFactory):
+    FACTORY_FOR = Polygon
+    latlng = '{"type": "Polygon", "coordinates": [[[5.679931640625, 50.13466432216694], [3.8891601562499996, 49.7173764049358], [5.09765625, 49.001843917978526], [6.39404296875, 49.167338606291075], [5.679931640625, 50.13466432216694]]]}'
+
+
 class BaseTest(TestCase):
     """
     Provide miminal data need in tests.

File leaflet_storage/tests/base_models.py

 from django.contrib.auth.models import AnonymousUser
 
 from leaflet_storage.models import Marker, Map, DataLayer
-from .base import BaseTest, UserFactory, MarkerFactory, DataLayerFactory
+from .base import BaseTest, UserFactory, MarkerFactory, DataLayerFactory,\
+                  PolygonFactory, PolylineFactory
 
 
 class MapModel(BaseTest):
             list(self.map.datalayer_set.all()),
             [c1, c2, c3, c4, self.datalayer]
         )
+
+    def test_features_should_be_mixed_and_ordered_by_name(self):
+        f4 = MarkerFactory(datalayer=self.datalayer, name="eeee")
+        f1 = PolygonFactory(datalayer=self.datalayer, name="1111")
+        f3 = PolylineFactory(datalayer=self.datalayer, name="cccc")
+        f2 = MarkerFactory(datalayer=self.datalayer, name="aaaa")
+        self.assertEqual(
+            list(self.datalayer.features),
+            [f1, f2, f3, f4]
+        )