Repository: django-tastypie/django-tastypie

Issue:
Allow use of timeout=0 in Simple cache
Fixes #1212 



Discussion:

Resolving patch:
diff --git a/tastypie/cache.py b/tastypie/cache.py
--- a/tastypie/cache.py
+++ b/tastypie/cache.py
@@ -60,7 +60,7 @@ def __init__(self, cache_name='default', timeout=None, public=None,
         """
         super(SimpleCache, self).__init__(*args, **kwargs)
         self.cache = get_cache(cache_name)
-        self.timeout = timeout or self.cache.default_timeout
+        self.timeout = timeout if timeout is not None else self.cache.default_timeout
         self.public = public
         self.private = private
 


Test patch:
diff --git a/tests/basic/api/resources.py b/tests/basic/api/resources.py
--- a/tests/basic/api/resources.py
+++ b/tests/basic/api/resources.py
@@ -30,6 +30,14 @@ class Meta:
         cache = SimpleCache(timeout=3600, public=True)
 
 
+class CacheDisabledUserResource(ModelResource):
+    class Meta:
+        allowed_methods = ('get', )
+        queryset = User.objects.all()
+        resource_name = 'cache_disabled_users'
+        cache = SimpleCache(timeout=0)
+
+
 class PrivateCachedUserResource(ModelResource):
     class Meta:
         allowed_methods = ('get', )
diff --git a/tests/basic/api/urls.py b/tests/basic/api/urls.py
--- a/tests/basic/api/urls.py
+++ b/tests/basic/api/urls.py
@@ -1,13 +1,14 @@
 from tastypie.api import Api
 from basic.api.resources import NoteResource, UserResource, BustedResource,\
     CachedUserResource, PublicCachedUserResource, PrivateCachedUserResource,\
-    SlugBasedNoteResource, SessionUserResource
+    CacheDisabledUserResource, SlugBasedNoteResource, SessionUserResource
 
 
 api = Api(api_name='v1')
 api.register(NoteResource(), canonical=True)
 api.register(UserResource(), canonical=True)
 api.register(CachedUserResource(), canonical=True)
+api.register(CacheDisabledUserResource(), canonical=True)
 api.register(PublicCachedUserResource(), canonical=True)
 api.register(PrivateCachedUserResource(), canonical=True)
 
diff --git a/tests/basic/tests/http.py b/tests/basic/tests/http.py
--- a/tests/basic/tests/http.py
+++ b/tests/basic/tests/http.py
@@ -36,7 +36,7 @@ def test_get_apis_json(self):
         connection.close()
         data = response.read().decode('utf-8')
         self.assertEqual(response.status, 200)
-        self.assertEqual(data, '{"cached_users": {"list_endpoint": "/api/v1/cached_users/", "schema": "/api/v1/cached_users/schema/"}, "notes": {"list_endpoint": "/api/v1/notes/", "schema": "/api/v1/notes/schema/"}, "private_cached_users": {"list_endpoint": "/api/v1/private_cached_users/", "schema": "/api/v1/private_cached_users/schema/"}, "public_cached_users": {"list_endpoint": "/api/v1/public_cached_users/", "schema": "/api/v1/public_cached_users/schema/"}, "users": {"list_endpoint": "/api/v1/users/", "schema": "/api/v1/users/schema/"}}')
+        self.assertEqual(data, '{"cache_disabled_users": {"list_endpoint": "/api/v1/cache_disabled_users/", "schema": "/api/v1/cache_disabled_users/schema/"}, "cached_users": {"list_endpoint": "/api/v1/cached_users/", "schema": "/api/v1/cached_users/schema/"}, "notes": {"list_endpoint": "/api/v1/notes/", "schema": "/api/v1/notes/schema/"}, "private_cached_users": {"list_endpoint": "/api/v1/private_cached_users/", "schema": "/api/v1/private_cached_users/schema/"}, "public_cached_users": {"list_endpoint": "/api/v1/public_cached_users/", "schema": "/api/v1/public_cached_users/schema/"}, "users": {"list_endpoint": "/api/v1/users/", "schema": "/api/v1/users/schema/"}}')
 
     def test_get_apis_invalid_accept(self):
         connection = self.get_connection()
@@ -64,7 +64,7 @@ def test_get_apis_xml(self):
         connection.close()
         data = response.read().decode('utf-8')
         self.assertEqual(response.status, 200)
-        self.assertEqual(data, '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<response><cached_users type="hash"><list_endpoint>/api/v1/cached_users/</list_endpoint><schema>/api/v1/cached_users/schema/</schema></cached_users><notes type="hash"><list_endpoint>/api/v1/notes/</list_endpoint><schema>/api/v1/notes/schema/</schema></notes><private_cached_users type="hash"><list_endpoint>/api/v1/private_cached_users/</list_endpoint><schema>/api/v1/private_cached_users/schema/</schema></private_cached_users><public_cached_users type="hash"><list_endpoint>/api/v1/public_cached_users/</list_endpoint><schema>/api/v1/public_cached_users/schema/</schema></public_cached_users><users type="hash"><list_endpoint>/api/v1/users/</list_endpoint><schema>/api/v1/users/schema/</schema></users></response>')
+        self.assertEqual(data, '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<response><cache_disabled_users type="hash"><list_endpoint>/api/v1/cache_disabled_users/</list_endpoint><schema>/api/v1/cache_disabled_users/schema/</schema></cache_disabled_users><cached_users type="hash"><list_endpoint>/api/v1/cached_users/</list_endpoint><schema>/api/v1/cached_users/schema/</schema></cached_users><notes type="hash"><list_endpoint>/api/v1/notes/</list_endpoint><schema>/api/v1/notes/schema/</schema></notes><private_cached_users type="hash"><list_endpoint>/api/v1/private_cached_users/</list_endpoint><schema>/api/v1/private_cached_users/schema/</schema></private_cached_users><public_cached_users type="hash"><list_endpoint>/api/v1/public_cached_users/</list_endpoint><schema>/api/v1/public_cached_users/schema/</schema></public_cached_users><users type="hash"><list_endpoint>/api/v1/users/</list_endpoint><schema>/api/v1/users/schema/</schema></users></response>')
 
     def test_get_list(self):
         connection = self.get_connection()
@@ -125,6 +125,19 @@ def test_cache_control(self):
         self.assertEqual(cache_control, set(["s-maxage=3600", "max-age=3600"]))
         self.assertTrue('"johndoe"' in response.read().decode('utf-8'))
 
+    def test_cache_disabled_control(self):
+        connection = self.get_connection()
+        connection.request('GET', '/api/v1/cache_disabled_users/', headers={'Accept': 'application/json'})
+        response = connection.getresponse()
+        connection.close()
+        self.assertEqual(response.status, 200)
+
+        headers = dict(response.getheaders())
+        cache_control = set([x.strip().lower() for x in headers[header_name("Cache-Control")].split(",") if x.strip()])
+
+        self.assertEqual(cache_control, set(["s-maxage=0", "max-age=0"]))
+        self.assertTrue('"johndoe"' in response.read().decode('utf-8'))
+
     def test_public_cache_control(self):
         connection = self.get_connection()
         connection.request('GET', '/api/v1/public_cached_users/', headers={'Accept': 'application/json'})
diff --git a/tests/basic/tests/views.py b/tests/basic/tests/views.py
--- a/tests/basic/tests/views.py
+++ b/tests/basic/tests/views.py
@@ -20,7 +20,7 @@ def test_gets(self):
         resp = self.client.get('/api/v1/', data={'format': 'json'})
         self.assertEqual(resp.status_code, 200)
         deserialized = json.loads(resp.content.decode('utf-8'))
-        self.assertEqual(len(deserialized), 5)
+        self.assertEqual(len(deserialized), 6)
         self.assertEqual(deserialized['notes'], {'list_endpoint': '/api/v1/notes/', 'schema': '/api/v1/notes/schema/'})
 
         resp = self.client.get('/api/v1/notes/', data={'format': 'json'})