Permalink
Browse files

[fix bug 771770] Cache API replies.

  • Loading branch information...
1 parent ff3efaf commit 2bf3b589672ce90bf74e40b04ae0c9b80574962b @glogiotatidis glogiotatidis committed Aug 22, 2012
Showing with 31 additions and 9 deletions.
  1. +23 −0 remo/api/__init__.py
  2. +4 −1 remo/events/api.py
  3. +4 −8 remo/profiles/api.py
View
@@ -0,0 +1,23 @@
+# Implement HTTP Caching
+# code from http://django-tastypie.readthedocs.org/en/latest/caching.html
+from django.utils.cache import patch_cache_control
+
+class ClientCachedResource(object):
+ """
+ Mixin class which sets Cache-Control headers on API responses
+ using a ``cache_control`` dictionary from the resource's Meta
+ class.
+
+ """
+
+ def create_response(self, request, data, **response_kwargs):
+ response = super(ClientCachedResource,
+ self).create_response(request, data, **response_kwargs)
+
+ if (request.method == "GET" and response.status_code == 200
+ and hasattr(self.Meta, "cache_control")):
+
+ cache_control = self.Meta.cache_control.copy()
+ patch_cache_control(response, **cache_control)
+
+ return response
View
@@ -11,11 +11,13 @@
from tastypie.resources import ModelResource
from tastypie.serializers import Serializer
+from remo.api import ClientCachedResource
+
from helpers import is_multiday
from models import Event
-class EventResource(ModelResource):
+class EventResource(ClientCachedResource, ModelResource):
"""Event Resource."""
local_start = fields.DateTimeField()
local_end = fields.DateTimeField()
@@ -25,6 +27,7 @@ class EventResource(ModelResource):
multiday = fields.BooleanField()
class Meta:
+ cache_control = {"max_age": 1800, "s_maxage": 1800}
queryset = Event.objects.all()
resource_name = 'event'
authentication = Authentication()
View
@@ -12,6 +12,7 @@
from tastypie.constants import ALL, ALL_WITH_RELATIONS
from tastypie.resources import ModelResource
+from remo.api import ClientCachedResource
from remo.base.serializers import CSVSerializer
from remo.profiles.helpers import get_avatar_url
from remo.profiles.models import UserProfile, FunctionalArea
@@ -82,32 +83,27 @@ def dehydrate_council(self, bundle):
return bundle.obj.user.groups.filter(name='Council').count() == 1
-class RepResource(ModelResource):
+class RepResource(ClientCachedResource, ModelResource):
"""Rep Resource."""
fullname = fields.CharField(attribute='get_full_name')
profile = fields.ToOneField(ProfileResource, attribute='userprofile',
full=True, null=True)
class Meta:
+ cache_control = {"max_age": 3600, "s_maxage": 3600}
queryset = User.objects.filter(userprofile__registration_complete=True,
groups__name='Rep')
resource_name = 'rep'
authentication = Authentication()
authorization = ReadOnlyAuthorization()
serializer = CSVSerializer(formats=['json', 'jsonp', 'csv'])
allowed_methods = ['get']
- fields = ['email', 'first_name', 'last_name']
+ fields = ['first_name', 'last_name']
ordering = ['profile', 'first_name', 'last_name']
filtering = {'first_name': ALL,
'last_name': ALL,
'profile': ALL_WITH_RELATIONS}
- def dehydrate_email(self, bundle):
- """Return email only if user is authenticated."""
- if bundle.request.user.is_authenticated():
- return bundle.obj.email
- return None
-
def apply_filters(self, request, applicable_filters):
"""Add special 'query' parameter to filter Reps.

0 comments on commit 2bf3b58

Please sign in to comment.