Repository: GeotrekCE/Geotrek-admin

Issue:
Pragma no cache for path graph json
As in https://github.com/makinacorpus/django-mapentity/pull/48

Related https://github.com/makinacorpus/Geotrek/issues/1026



Discussion:

Resolving patch:
diff --git a/geotrek/core/views.py b/geotrek/core/views.py
--- a/geotrek/core/views.py
+++ b/geotrek/core/views.py
@@ -4,6 +4,7 @@
 from django.utils.decorators import method_decorator
 from django.contrib.auth.decorators import login_required
 from django.views.decorators.http import last_modified as cache_last_modified
+from django.views.decorators.cache import never_cache as force_cache_validation
 from django.views.generic.edit import BaseDetailView
 from django.core.cache import get_cache
 from django.shortcuts import redirect
@@ -148,6 +149,7 @@ def dispatch(self, *args, **kwargs):
 
 @login_required
 @cache_last_modified(lambda x: Path.latest_updated())
+@force_cache_validation
 def get_graph_json(request):
     cache = get_cache('fat')
     key = 'path_graph_json'


Test patch:
diff --git a/geotrek/core/tests/test_graph.py b/geotrek/core/tests/test_graph.py
--- a/geotrek/core/tests/test_graph.py
+++ b/geotrek/core/tests/test_graph.py
@@ -1,9 +1,11 @@
 import json
+import time
 
 from django.test import TestCase
 from django.contrib.auth.models import User
 from django.contrib.gis.geos import LineString
 from django.core.urlresolvers import reverse
+from django.utils.http import http_date
 
 from geotrek.core.factories import PathFactory
 from geotrek.core.graph import graph_edges_nodes_of_qs
@@ -16,6 +18,7 @@ def setUp(self):
         user = User.objects.create_user('homer', 'h@s.com', 'dooh')
         success = self.client.login(username=user.username, password='dooh')
         self.assertTrue(success)
+        self.url = reverse('core:path_json_graph')
 
     def test_python_graph_from_path(self):
         p_1_1 = (1., 1.)
@@ -55,17 +58,27 @@ def gen_random_point():
         self.assertDictEqual(computed_graph, graph)
 
     def test_json_graph_empty(self):
-        url = reverse('core:path_json_graph')
-        response = self.client.get(url)
+
+        response = self.client.get(self.url)
         self.assertEqual(response.status_code, 200)
         graph = json.loads(response.content)
         self.assertDictEqual({'edges': {}, 'nodes': {}}, graph)
 
     def test_json_graph_simple(self):
         path = PathFactory(geom=LineString((0, 0), (1, 1)))
-        url = reverse('core:path_json_graph')
-        response = self.client.get(url)
+        response = self.client.get(self.url)
         self.assertEqual(response.status_code, 200)
         graph = json.loads(response.content)
         self.assertDictEqual({'edges': {str(path.pk): {u'id': path.pk, u'length': 1.4142135623731, u'nodes_id': [1, 2]}},
                               'nodes': {u'1': {u'2': path.pk}, u'2': {u'1': path.pk}}}, graph)
+
+    def test_json_graph_headers(self):
+        """
+        Last modified depends on
+        """
+        PathFactory(geom=LineString((0, 0), (1, 1)))
+        response = self.client.get(self.url)
+        last_modified = response['Last-Modified']
+        expires = response['Expires']
+        self.assertNotEqual(expires, None)
+        self.assertEqual(expires, last_modified)