Created
May 17, 2011 01:23
-
-
Save fsouza/975704 to your computer and use it in GitHub Desktop.
Decorator for using django-htmlmin on Flask views.
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
from functools import wraps | |
from htmlmin.minify import html_minify | |
def minified_response(function): | |
@wraps(function) | |
def minified_view(*args, **kwargs): | |
return_value = function(*args, **kwargs) | |
if type(return_value) == unicode: | |
return html_minify(return_value.encode('utf-8')) | |
return return_value | |
return minified_view |
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
import unittest | |
class TestDecorators(unittest.TestCase): | |
def test_should_minify_the_rendered_content_from_view_using_a_decorator(self): | |
from decorators import minified_response | |
@index.app.route("/bla-bla-bla") | |
@minified_response | |
def bla_bla_bla(): | |
return u'<html> <body>Hello world</body> </html>' | |
response = self.app.get('/bla-bla-bla') | |
self.assertEqual(u'<html><body>Hello world</body></html>', response.data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment