Commits

Yohan Boniface committed 5ddcbb3

Cache Category.features

Comments (0)

Files changed (2)

leaflet_storage/base_models.py

 
     @property
     def features(self):
-        filters = {
-            "category": self
-        }
-        markers = Marker.objects.filter(**filters)
-        polylines = Polyline.objects.filter(**filters)
-        polygons = Polygon.objects.filter(**filters)
-        return list(markers) + list(polylines) + list(polygons)
+        if not hasattr(self, "_features"):
+            filters = {
+                "category": self
+            }
+            markers = Marker.objects.filter(**filters)
+            polylines = Polyline.objects.filter(**filters)
+            polygons = Polygon.objects.filter(**filters)
+            self._features = list(markers) + list(polylines) + list(polygons)
+        return self._features
 
     class Meta:
         ordering = ["rank"]

leaflet_storage/tests/base_models.py

         self.assertEqual(Map.objects.filter(pk=self.map.pk).count(), 1)
         self.assertEqual(Category.objects.filter(pk=self.category.pk).count(), 1)
         self.assertEqual(Marker.objects.filter(pk=marker.pk).count(), 1)
+
+
+class CategoryModel(BaseTest):
+
+    def test_features_should_be_locally_cached(self):
+        MarkerFactory(category=self.category)
+        MarkerFactory(category=self.category)
+        MarkerFactory(category=self.category)
+        self.category.features
+        with self.assertNumQueries(0):
+            self.category.features