Example #1
0
    def test_css_signal_multiple_media_attributes(self):
        css = """\
<link rel="stylesheet" href="/media/css/one.css" media="handheld" type="text/css" />
<style type="text/css" media="print">p { border:5px solid green;}</style>
<link rel="stylesheet" href="/media/css/two.css" type="text/css" />"""
        css_node = CssCompressor(css)
        def listener(sender, **kwargs):
            pass
        callback = Mock(wraps=listener)
        post_compress.connect(callback)
        css_node.output()
        self.assertEquals(3, callback.call_count)
Example #2
0
 def test_css_signal_sent(self):
     def listener(sender, **kwargs):
         pass
     callback = Mock(wraps=listener)
     post_compress.connect(callback)
     self.css_node.output()
     args, kwargs = callback.call_args
     self.assertEqual(CssCompressor, kwargs['sender'])
     self.assertEqual('css', kwargs['type'])
     self.assertEqual('file', kwargs['mode'])
     context = kwargs['context']
     assert 'url' in context['compressed']
Example #3
0
 def test_css_signal_sent(self):
     def listener(sender, **kwargs):
         pass
     callback = Mock(wraps=listener)
     post_compress.connect(callback)
     self.css_node.output()
     args, kwargs = callback.call_args
     self.assertEquals(CssCompressor, kwargs['sender'])
     self.assertEquals('css', kwargs['type'])
     self.assertEquals('file', kwargs['mode'])
     context = kwargs['context']
     assert 'url' in context['compressed']
Example #4
0
 def test_named_compress_tag(self):
     template = u"""{% load compress %}{% compress js inline foo %}
     <script type="text/javascript">obj.value = "value";</script>
     {% endcompress %}
     """
     def listener(sender, **kwargs):
         pass
     callback = Mock(wraps=listener)
     post_compress.connect(callback)
     render(template)
     args, kwargs = callback.call_args
     context = kwargs['context']
     self.assertEqual('foo', context['name'])
Example #5
0
 def test_named_compress_tag(self):
     template = u"""{% load compress %}{% compress js inline foo %}
     <script type="text/javascript">obj.value = "value";</script>
     {% endcompress %}
     """
     def listener(sender, **kwargs):
         pass
     callback = Mock(wraps=listener)
     post_compress.connect(callback)
     render(template)
     args, kwargs = callback.call_args
     context = kwargs['context']
     self.assertEqual('foo', context['name'])
Example #6
0
    def test_css_signal_multiple_media_attributes(self):
        css = """\
<link rel="stylesheet" href="/static/css/one.css" media="handheld" type="text/css" />
<style type="text/css" media="print">p { border:5px solid green;}</style>
<link rel="stylesheet" href="/static/css/two.css" type="text/css">"""
        css_node = CssCompressor('css', css)

        def listener(sender, **kwargs):
            pass
        callback = Mock(wraps=listener)
        post_compress.connect(callback)
        css_node.output()
        self.assertEqual(3, callback.call_count)
Example #7
0
    def test_css_signal_sent(self):
        def listener(sender, **kwargs):
            pass

        callback = Mock(wraps=listener)
        post_compress.connect(callback)
        self.css_node.output()
        args, kwargs = callback.call_args
        self.assertEqual(CssCompressor, kwargs["sender"])
        self.assertEqual("css", kwargs["type"])
        self.assertEqual("file", kwargs["mode"])
        context = kwargs["context"]
        assert "url" in context["compressed"]
Example #8
0
    def test_css_signal_sent(self):
        def listener(sender, **kwargs):
            pass

        callback = Mock(wraps=listener)
        post_compress.connect(callback)
        self.css_node.output()
        args, kwargs = callback.call_args
        self.assertEquals("django-compressor", kwargs["sender"])
        self.assertEquals("css", kwargs["type"])
        self.assertEquals("file", kwargs["mode"])
        context = kwargs["context"]
        assert "url" in context
    content_types = {
        'css': 'text/css',
        'js': 'text/javascript',
    }

    content_type = content_types[type]

    headers = {
        "Cache-Control": "max-age=315360000, public",
        "Expires": "Thu, 31 Dec 2037 23:55:55 GMT",
        "Content-Encoding": "gzip",
        'Content-Type': content_type,
    }

    conn = S3Connection(*aws)
    bucket = conn.get_bucket(Config['compress_bucket'])

    key = Key(bucket)
    key.key = destination_name
    try:
        if key.exists():
            print "Skipping", destination_name, " already exists."
        else:
            print "Uploading %s (%s kb)" % (destination_name, len(filedata) // 1024)
            key.set_contents_from_string(filedata, headers=headers)
    except BotoServerError, bse:
        print bse

post_compress.connect(upload_compressed_files_to_s3)

from compressor.signals import post_compress

from canvas.redis_models import RedisHash

class _CompressedFiles(object):
    hashes = {
        'css': RedisHash('compressed:css'),
        'js': RedisHash('compressed:js'),
    }

    def set(self, type, name, path):
        self.hashes[type].hset(name, path)

    def get(self, type, name):
        return self.hashes[type].hget(name)

compressed_files = _CompressedFiles()

def watch_compressed_files(sender, type, mode, context, **kwargs):
    """ Only works with Django templates, since the Jinja one doesn't parse the name field. """
    name = context.get('name')
    if name and mode == "file":
        compressed_files.set(type, name, context['url'])

post_compress.connect(watch_compressed_files)

Example #11
0
        'css': 'text/css',
        'js': 'text/javascript',
    }

    content_type = content_types[type]

    headers = {
        "Cache-Control": "max-age=315360000, public",
        "Expires": "Thu, 31 Dec 2037 23:55:55 GMT",
        "Content-Encoding": "gzip",
        'Content-Type': content_type,
    }

    conn = S3Connection(*aws)
    bucket = conn.get_bucket(Config['compress_bucket'])

    key = Key(bucket)
    key.key = destination_name
    try:
        if key.exists():
            print "Skipping", destination_name, " already exists."
        else:
            print "Uploading %s (%s kb)" % (destination_name,
                                            len(filedata) // 1024)
            key.set_contents_from_string(filedata, headers=headers)
    except BotoServerError, bse:
        print bse


post_compress.connect(upload_compressed_files_to_s3)
Example #12
0
from django.conf.urls import patterns, include, url
from compressor.signals import post_compress
from django.contrib import admin
from djangosuit.compress import post_less_compile

admin.autodiscover()
urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'djangosuit.views.home', name='home'),
    # url(r'^djangosuit/', include('djangosuit.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),
)


# Post compress handler for less files
post_compress.connect(post_less_compile)
Example #13
0
from compressor.signals import post_compress

from canvas.redis_models import RedisHash


class _CompressedFiles(object):
    hashes = {
        'css': RedisHash('compressed:css'),
        'js': RedisHash('compressed:js'),
    }

    def set(self, type, name, path):
        self.hashes[type].hset(name, path)

    def get(self, type, name):
        return self.hashes[type].hget(name)


compressed_files = _CompressedFiles()


def watch_compressed_files(sender, type, mode, context, **kwargs):
    """ Only works with Django templates, since the Jinja one doesn't parse the name field. """
    name = context.get('name')
    if name and mode == "file":
        compressed_files.set(type, name, context['url'])


post_compress.connect(watch_compressed_files)