Skip to content

Commit

Permalink
Set cache expire headers to two months on frontend image serve view
Browse files Browse the repository at this point in the history
  • Loading branch information
kaedroho committed Jul 18, 2014
1 parent 055233b commit b1d412f
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ services:
# Package installation
install:
- python setup.py install
- pip install psycopg2 elasticsearch wand embedly
- pip install psycopg2 elasticsearch wand embedly mock python-dateutil
- pip install coveralls
# Pre-test configuration
before_script:
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
coverage==3.7.1
flake8==2.2.1
mock==1.0.1
python-dateutil==2.2
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ dj16=
Django>=1.6,<1.7
elasticsearch==1.1.0
mock==1.0.1
python-dateutil==2.2

[tox]
envlist =
Expand Down
11 changes: 11 additions & 0 deletions wagtail/wagtailimages/tests.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import json
import datetime

from mock import MagicMock
import dateutil.parser

from django.utils import six
from django.utils.http import urlquote
from django.utils import timezone
from django.test import TestCase
from django import template
from django.contrib.auth.models import User, Group, Permission
Expand Down Expand Up @@ -538,6 +541,14 @@ def test_get(self):
self.assertEqual(response.status_code, 200)
self.assertEqual(response['Content-Type'], 'image/jpeg')

# Make sure the cache headers are set to expire after at least one month
self.assertIn('Cache-Control', response)
self.assertEqual(response['Cache-Control'].split('=')[0], 'max-age')
self.assertTrue(int(response['Cache-Control'].split('=')[1]) > datetime.timedelta(days=30).seconds)

self.assertIn('Expires', response)
self.assertTrue(dateutil.parser.parse(response['Expires']) > timezone.now() + datetime.timedelta(days=30))

def test_get_invalid_signature(self):
"""
Test that an invalid signature returns a 403 response
Expand Down
2 changes: 2 additions & 0 deletions wagtail/wagtailimages/views/frontend.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from django.shortcuts import get_object_or_404
from django.http import HttpResponse
from django.core.exceptions import PermissionDenied
from django.views.decorators.cache import cache_page

from wagtail.wagtailimages.models import get_image_model
from wagtail.wagtailimages.utils import InvalidFilterSpecError, verify_signature
from wagtail.wagtailimages import image_processor


@cache_page(60 * 60 * 24 * 60) # Cache for 60 days
def serve(request, signature, image_id, filter_spec):
image = get_object_or_404(get_image_model(), id=image_id)

Expand Down

0 comments on commit b1d412f

Please sign in to comment.