Skip to content

Conversation

@submarcos
Copy link
Member

Description

Related Issue

Checklist

  • I have followed the guidelines in our Contributing document
  • My code respects the Definition of done available in the Development section of the documentation
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes
  • I added an entry in the changelog file
  • My commits are all using prefix convention (emoji + tag name) and references associated issues
  • I added a label to the PR corresponding to the perimeter of my contribution
  • The title of my PR mentionned the issue associated

@submarcos submarcos marked this pull request as ready for review December 12, 2025 16:38
@submarcos submarcos changed the title fix(restricted_area): Fix cache with specifi restricted area endpoint by type Fix restricted area endpoint by type with specifi cache key compute Dec 12, 2025
@submarcos submarcos linked an issue Dec 12, 2025 that may be closed by this pull request
@submarcos submarcos changed the title Fix restricted area endpoint by type with specifi cache key compute Fix restricted area endpoint by type with specific cache key Dec 12, 2025
@cypress
Copy link

cypress bot commented Dec 12, 2025

Geotrek-admin    Run #15103

Run Properties:  status check passed Passed #15103  •  git commit 6e24007fd1 ℹ️: Merge f0bdd50d4f8300f5a73fb6ba1d6e404a84f4a83b into b07dddc372217963d18f212ffb52...
Project Geotrek-admin
Branch Review refs/pull/5150/merge
Run status status check passed Passed #15103
Run duration 02m 07s
Commit git commit 6e24007fd1 ℹ️: Merge f0bdd50d4f8300f5a73fb6ba1d6e404a84f4a83b into b07dddc372217963d18f212ffb52...
Committer Jean-Etienne Castagnede
View all properties for this run ↗︎

Test results
Tests that failed  Failures 0
Tests that were flaky  Flaky 0
Tests that did not run due to a developer annotating a test with .skip  Pending 0
Tests that did not run due to a failure in a mocha hook  Skipped 0
Tests that passed  Passing 22
View all changes introduced in this branch ↗︎

@codecov
Copy link

codecov bot commented Dec 12, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.47%. Comparing base (13dd405) to head (f0bdd50).
⚠️ Report is 10 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #5150   +/-   ##
=======================================
  Coverage   98.47%   98.47%           
=======================================
  Files         272      272           
  Lines       22269    22285   +16     
=======================================
+ Hits        21929    21945   +16     
  Misses        340      340           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@submarcos submarcos merged commit 67200b9 into master Dec 16, 2025
37 of 42 checks passed
@submarcos submarcos deleted the fix_restricted_area_zoning branch December 16, 2025 09:22
@submarcos submarcos requested a review from Copilot December 16, 2025 09:22
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a caching issue where restricted area GeoJSON endpoints were not properly differentiating cache entries by type. The fix adds a custom view_cache_key method to RestrictedAreaViewSet that incorporates the type_pk parameter into the cache key, and implements a new latest_updated method on the RestrictedArea model that can filter by type to get the appropriate timestamp for cache invalidation.

Key Changes:

  • Adds view_cache_key method to RestrictedAreaViewSet that generates type-specific cache keys
  • Implements latest_updated classmethod on RestrictedArea model with optional type filtering
  • Adds comprehensive model tests for the new latest_updated functionality

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
geotrek/zoning/views.py Adds custom view_cache_key method to generate cache keys that include type_pk parameter
geotrek/zoning/models.py Implements latest_updated classmethod with optional type_id filtering to support type-specific cache invalidation
geotrek/zoning/tests/test_models.py Adds tests for latest_updated method behavior with and without type filtering
docs/changelog.rst Documents the bug fix with issue reference #5136

Comment on lines +355 to +374
def test_latest_updated_is_different_by_type(self):
type_without_data = RestrictedAreaTypeFactory()
type_with_data = RestrictedAreaTypeFactory()
type_with_data_2 = RestrictedAreaTypeFactory()
RestrictedAreaFactory.create_batch(5, area_type=type_with_data)
RestrictedAreaFactory.create_batch(5, area_type=type_with_data_2)

self.assertIsNone(RestrictedArea.latest_updated(type_without_data.pk))
self.assertEqual(
RestrictedArea.latest_updated(type_with_data.pk),
type_with_data.restrictedarea_set.only("date_update")
.latest("date_update")
.date_update,
)
self.assertEqual(
RestrictedArea.latest_updated(type_with_data_2.pk),
type_with_data_2.restrictedarea_set.only("date_update")
.latest("date_update")
.date_update,
)
Copy link

Copilot AI Dec 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While the new tests cover the latest_updated model method well, there's no test coverage for the view_cache_key method in RestrictedAreaViewSet. Tests should be added to verify that the cache key correctly incorporates the type_pk parameter and that different types generate different cache keys. This is critical since the whole purpose of this PR is to fix cache differentiation by type.

Copilot uses AI. Check for mistakes.
try:
qs = cls.objects.all()
if type_id:
qs = cls.objects.filter(area_type_id=type_id)
Copy link

Copilot AI Dec 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The queryset initialization on line 41 is redundant because when type_id is provided, line 43 reinitializes the queryset from scratch using cls.objects.filter(), effectively discarding the assignment from line 41. This should be refactored to filter the initial queryset instead.

Suggested change
qs = cls.objects.filter(area_type_id=type_id)
qs = qs.filter(area_type_id=type_id)

Copilot uses AI. Check for mistakes.
def view_cache_key(self):
"""Used by the ``view_cache_response_content`` decorator."""
language = get_language()
geojson_lookup = None
Copy link

Copilot AI Dec 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable geojson_lookup is initialized to None but then immediately overwritten on line 41. This initialization is unnecessary and can be removed.

Suggested change
geojson_lookup = None

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Visualisation de la même couche de zonage réglementaire

2 participants