Skip to content

Commit 67200b9

Browse files
authored
Fix restricted area endpoint by type with specific cache key (#5150)
* fix(restricted_area): Fix cache with specifi restricted area endpoint by type fix #5136 * fix(restricted_area): Update changelog for restricted area geojson cache fix * fix(restricted_area): Update changelog for restricted area geojson cache fix * test(restricted_area): Add tests for latest_updated method by type * fix(restricted_area): Handle case when latest_updated returns None in geojson lookup
1 parent b07dddc commit 67200b9

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

docs/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
**Bug fixes**
99

1010
* Fix ``label_en`` content on sensitivity module parser
11+
* Fix restricted area geojson that are not different by type in cache (#5136)
1112

1213
2.121.1 (2025-11-17)
1314
----------------------------

geotrek/zoning/models.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ class RestrictedArea(TimeStampedModelMixin, models.Model):
3535
help_text=_("Visible on Geotrek-rando"),
3636
)
3737

38+
@classmethod
39+
def latest_updated(cls, type_id=None):
40+
try:
41+
qs = cls.objects.all()
42+
if type_id:
43+
qs = cls.objects.filter(area_type_id=type_id)
44+
return qs.only("date_update").latest("date_update").date_update
45+
except cls.DoesNotExist:
46+
return None
47+
3848
class Meta:
3949
ordering = ["area_type", "name"]
4050
verbose_name = _("Restricted area")

geotrek/zoning/tests/test_models.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
CityFactory,
1212
DistrictFactory,
1313
RestrictedAreaFactory,
14+
RestrictedAreaTypeFactory,
1415
)
1516

1617

@@ -347,3 +348,27 @@ def test_restricted_area_last_updated_with_data(self):
347348
restricted_area = RestrictedAreaFactory()
348349
self.assertIsNotNone(RestrictedArea.latest_updated())
349350
self.assertEqual(RestrictedArea.latest_updated(), restricted_area.date_update)
351+
352+
def test_latest_updated_when_no_data_at_all(self):
353+
self.assertIsNone(RestrictedArea.latest_updated())
354+
355+
def test_latest_updated_is_different_by_type(self):
356+
type_without_data = RestrictedAreaTypeFactory()
357+
type_with_data = RestrictedAreaTypeFactory()
358+
type_with_data_2 = RestrictedAreaTypeFactory()
359+
RestrictedAreaFactory.create_batch(5, area_type=type_with_data)
360+
RestrictedAreaFactory.create_batch(5, area_type=type_with_data_2)
361+
362+
self.assertIsNone(RestrictedArea.latest_updated(type_without_data.pk))
363+
self.assertEqual(
364+
RestrictedArea.latest_updated(type_with_data.pk),
365+
type_with_data.restrictedarea_set.only("date_update")
366+
.latest("date_update")
367+
.date_update,
368+
)
369+
self.assertEqual(
370+
RestrictedArea.latest_updated(type_with_data_2.pk),
371+
type_with_data_2.restrictedarea_set.only("date_update")
372+
.latest("date_update")
373+
.date_update,
374+
)

geotrek/zoning/views.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from django.conf import settings
22
from django.contrib.gis.db.models.functions import Transform
33
from django.shortcuts import get_object_or_404
4+
from django.utils.translation import get_language
45
from mapentity.decorators import view_cache_latest, view_cache_response_content
56
from mapentity.renderers import GeoJSONRenderer
67
from rest_framework import permissions, viewsets
@@ -32,6 +33,19 @@ class RestrictedAreaViewSet(LandGeoJSONAPIViewMixin, viewsets.ReadOnlyModelViewS
3233
model = RestrictedArea
3334
serializer_class = RestrictedAreaSerializer
3435

36+
def view_cache_key(self):
37+
"""Used by the ``view_cache_response_content`` decorator."""
38+
language = get_language()
39+
geojson_lookup = None
40+
latest_saved = self.model.latest_updated(type_id=self.kwargs.get("type_pk"))
41+
geojson_lookup = "{}_restricted_area_{}_{}_{}_geojson_layer".format(
42+
language,
43+
self.kwargs.get("type_pk", "all"),
44+
latest_saved.isoformat() if latest_saved else "no_data",
45+
self.request.user.pk if settings.SURICATE_WORKFLOW_ENABLED else "",
46+
)
47+
return geojson_lookup
48+
3549
def get_queryset(self):
3650
type_pk = self.kwargs.get("type_pk")
3751
qs = super().get_queryset()

0 commit comments

Comments
 (0)