Skip to content
Browse files

Merge pull request #13 from sergiocharpineljr/bug_588136_cache

588136: Make ispdb cache-friendly
  • Loading branch information...
2 parents ae6d56a + 61076a3 commit 8056653f658dcd1f12b82887948d9318a46995c9 @Sancus Sancus committed Aug 20, 2012
Showing with 118 additions and 3 deletions.
  1. +31 −2 ispdb/config/views.py
  2. +37 −1 ispdb/fixtures/xml_testdata.json
  3. +8 −0 ispdb/settings.py
  4. +42 −0 ispdb/tests/test_cache.py
View
33 ispdb/config/views.py
@@ -11,12 +11,15 @@
from django.contrib.comments.views.comments import post_comment
from django.contrib.contenttypes.models import ContentType
from django.core.urlresolvers import reverse
-from django.db.models import Q
+from django.db.models import Q, Max
from django.forms.models import modelformset_factory
from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404, render_to_response
from django.template import RequestContext
from django.utils import simplejson, timezone
+from django.utils.cache import patch_cache_control
+from django.views.decorators.cache import cache_control
+from django.views.decorators.http import last_modified
from ispdb.config import serializers
from ispdb.config.configChecks import do_config_checks, do_domain_checks
@@ -63,6 +66,17 @@ def intro(request):
context_instance=RequestContext(request))
+def last_modified_list_xml(request, format="html"):
+ if format == "html":
+ return None # don't need to return last_update to html responses
+ res = Config.objects.filter(status='approved').aggregate(
+ Max('last_update_datetime'))
+ if res:
+ return res['last_update_datetime__max']
+ return None
+
+
+@last_modified(last_modified_list_xml)
def list(request, format="html"):
if format == "xml":
providers = ET.Element("providers")
@@ -80,6 +94,8 @@ def list(request, format="html"):
response = HttpResponse(mimetype="text/xml")
response.write(output.getvalue())
output.close()
+ # set cache settings
+ patch_cache_control(response, no_cache=True)
return response
configs = Config.objects.all()
return render_to_response("config/list.html", {'configs': configs},
@@ -126,10 +142,23 @@ def details(request, id, error=None):
context_instance=RequestContext(request))
+def last_modified_export_xml(request, version=None, id=None, domain=None):
+ config = None
+ if id is not None:
+ config = get_object_or_404(Config, pk=int(id))
+ elif domain is not None:
+ config = Domain.objects.filter(name=domain)[0].config
+ if not config:
+ return None
+ return config.last_update_datetime
+
+
+@cache_control(no_cache=True)
+@last_modified(last_modified_export_xml)
def export_xml(request, version=None, id=None, domain=None):
config = None
if id is not None:
- config = Config.objects.filter(id=int(id))[0]
+ config = get_object_or_404(Config, pk=int(id))
elif domain is not None:
config = Domain.objects.filter(name=domain)[0].config
serialize = serializers.get(version)
View
38 ispdb/fixtures/xml_testdata.json
@@ -8,6 +8,14 @@
}
},
{
+ "pk":2,
+ "model":"config.domain",
+ "fields": {
+ "config": 2,
+ "name": "test2.com"
+ }
+ },
+ {
"pk":1,
"model":"config.config",
"fields": {
@@ -24,7 +32,7 @@
"incoming_hostname":"hostname_in",
"outgoing_username_form":"%EMAILLOCALPART%",
"display_short_name":"netzero",
- "status":"requested",
+ "status":"approved",
"incoming_port":143,
"last_update_datetime": "2012-05-31T21:08:11.305Z",
"incoming_username_form":"",
@@ -34,6 +42,34 @@
}
},
{
+ "pk":2,
+ "model":"config.config",
+ "fields": {
+ "outgoing_use_global_preferred_server":false,
+ "incoming_socket_type":"SSL",
+ "outgoing_port":25,
+ "flagged_by_email":"",
+ "flagged_as_incorrect":false,
+ "email_provider_id":"test.com",
+ "incoming_authentication":"password-cleartext",
+ "display_name":"NetZero Email",
+ "outgoing_add_this_server":false,
+ "outgoing_socket_type":"SSL",
+ "created_datetime": "2012-05-31T21:08:09.314Z",
+ "outgoing_authentication":"password-cleartext",
+ "incoming_hostname":"hostname_in",
+ "outgoing_username_form":"%EMAILLOCALPART%",
+ "display_short_name":"netzero",
+ "status":"approved",
+ "incoming_port":143,
+ "last_update_datetime": "2012-04-30T21:08:11.305Z",
+ "incoming_username_form":"",
+ "outgoing_hostname":"hostname_out",
+ "incoming_type":"imap",
+ "locked": "False"
+ }
+ },
+ {
"pk": 1,
"model": "config.docurl",
"fields": {
View
8 ispdb/settings.py
@@ -100,13 +100,15 @@
)
MIDDLEWARE_CLASSES = (
+ 'django.middleware.cache.UpdateCacheMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
# Uncomment the next line for simple clickjacking protection:
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
+ 'django.middleware.cache.FetchFromCacheMiddleware',
)
ROOT_URLCONF = 'ispdb.urls'
@@ -155,3 +157,9 @@
"django.core.context_processors.request",
"django_browserid.context_processors.browserid_form",
)
+
+CACHES = {
+ 'default': {
+ 'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
+ }
+}
View
42 ispdb/tests/test_cache.py
@@ -0,0 +1,42 @@
+# -*- coding: utf-8 -*-
+
+from datetime import datetime
+from nose.tools import assert_true
+
+from django.core.urlresolvers import reverse
+from django.test import TestCase
+from django.utils.timezone import utc
+
+from ispdb.config.models import Config
+
+
+class CacheTest(TestCase):
+
+ fixtures = ['login_testdata', 'xml_testdata']
+
+ def test_default_cache(self):
+ res = self.client.get(reverse("ispdb_list"))
+ assert_true('Cache-Control' in res)
+ assert_true(res['Cache-Control'] == 'max-age=600')
+
+ def test_list_xml_cache(self):
+ res = self.client.get(reverse("ispdb_list", args=['xml']))
+ assert_true('Cache-Control' in res)
+ assert_true(res['Cache-Control'] == 'no-cache, max-age=600')
+ config = Config.objects.get(pk=1)
+ assert_true('Last-Modified' in res)
+ d = datetime.strptime(res['Last-Modified'], "%a, %d %b %Y %H:%M:%S "
+ "%Z").replace(tzinfo=utc)
+ c = config.last_update_datetime.replace(microsecond=0)
+ assert_true(d == c)
+
+ def test_export_xml_cache(self):
+ res = self.client.get(reverse("ispdb_export_xml", args=[2]))
+ assert_true('Cache-Control' in res)
+ assert_true(res['Cache-Control'] == 'no-cache, max-age=600')
+ config = Config.objects.get(pk=2)
+ assert_true('Last-Modified' in res)
+ d = datetime.strptime(res['Last-Modified'], "%a, %d %b %Y %H:%M:%S "
+ "%Z").replace(tzinfo=utc)
+ c = config.last_update_datetime.replace(microsecond=0)
+ assert_true(d == c)

0 comments on commit 8056653

Please sign in to comment.
Something went wrong with that request. Please try again.