forked from zpurcey/new-teamtemp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from rloomans/cache-media-files
Allow caching of media files
- Loading branch information
Showing
7 changed files
with
82 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
# https://djangosnippets.org/snippets/2895/ | ||
|
||
|
||
from functools import wraps | ||
|
||
from django.utils.decorators import available_attrs | ||
|
||
|
||
def header(name, value): | ||
# View decorator that sets a response header. | ||
# | ||
# Example: | ||
# @header('X-Powered-By', 'Django') | ||
# def view(request, ...): | ||
# .... | ||
# | ||
# For class-based views use: | ||
# @method_decorator(header('X-Powered-By', 'Django')) | ||
# def get(self, request, ...) | ||
# ... | ||
def decorator(func): | ||
@wraps(func, assigned=available_attrs(func)) | ||
def inner(request, *args, **kwargs): | ||
response = func(request, *args, **kwargs) | ||
response[name] = value | ||
return response | ||
|
||
return inner | ||
|
||
return decorator | ||
|
||
|
||
def x_ua_compatible(ie): | ||
return header('X-UA-Compatible', ie) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from django.shortcuts import render | ||
from django.test import TestCase, RequestFactory | ||
from rest_framework import status | ||
|
||
from measure_mate.headers import header, x_ua_compatible | ||
|
||
|
||
class HeadersTestCases(TestCase): | ||
def test_double_header(self): | ||
@header('X-Test-Header1', 'one') | ||
@header('X-Test-Header2', 'two') | ||
@x_ua_compatible('IE=edge') | ||
def test_headers(req): | ||
return render(req, 'index.html') | ||
|
||
request = RequestFactory() | ||
response = test_headers(request) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(response['X-Test-Header1'], 'one') | ||
self.assertEqual(response['X-Test-Header2'], 'two') | ||
self.assertEqual(response['X-UA-Compatible'], 'IE=edge') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from django.core.urlresolvers import reverse | ||
from django.test import TestCase | ||
|
||
|
||
class MediaViewTestCases(TestCase): | ||
def test_media_view(self): | ||
response = self.client.get('/media/test.png') | ||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(response['Content-Type'], 'image/png') | ||
self.assertEqual(response['Cache-Control'], 'public, max-age=315360000') | ||
self.assertTemplateNotUsed(response, 'index.html') | ||
self.assertGreater(len(response.getvalue()), 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters