Example #1
0
def test_templatetag(template_string, filename):
    storage = StaticFilesStorage(location=settings.STATICFILES_TEST_DIR)
    try:
        storage.save(name=filename,
                     content=ContentFile("body { background: red; }"))
        resp = T(template_string).render(CTX).strip().split("\n")
        expected_output = '<link href="{}{}" rel="stylesheet" type="text/css" />'.format(
            settings.STATIC_URL, filename)
        assert expected_output in resp
        assert "<!--" not in resp
        assert "-->" not in resp
    finally:
        storage.delete(filename)
Example #2
0
class S3LocalCachedMixin(object):
    """
    Mixin that adds local caching to S3 storage backend
    """
    def __init__(self, *args, **kwargs):
        super(S3LocalCachedMixin, self).__init__(*args, **kwargs)
        self._local = StaticFilesStorage(location=CONVOY_LOCAL_CACHE_ROOT)

    def save(self, name, content, *args, **kwargs):
        if not hasattr(content, 'chunks'):
            content = ContentFile(content)
        sname = self._save(name, content, *args, **kwargs)
        return sname

    def _save(self, name, content, *args, **kwargs):
        # some configurations of s3 backend mutate the content in place
        # Esp when AWS_IS_GZIPPED = True
        # keep a pre-mutation copy for the local cache so we don't save garbage to disk
        orig_content = copy.copy(content)
        sname = super(S3LocalCachedMixin, self)._save(name, content, *args,
                                                      **kwargs)
        if self._local.exists(name):
            self._local.delete(name)
        lname = self._local._save(name, orig_content, *args, **kwargs)
        return name

    def delete(self, *args, **kwargs):
        if self._local.exists(*args, **kwargs):
            self._local.delete(*args, **kwargs)
        return super(S3LocalCachedMixin, self).delete(*args, **kwargs)

    def open(self, name, *args, **kwargs):
        if self._local.exists(name):
            #print "reading %s from cache" % name
            return self._local.open(name, *args, **kwargs)
        else:
            #print "reading %s from network" % name
            the_file = super(S3LocalCachedMixin,
                             self).open(name, *args, **kwargs)
            #we had a cache miss, save it locally for the future
            self._local.save(name, the_file)
            if hasattr(the_file, "seek"):
                the_file.seek(0)
            return the_file

    def local_path(self, *args, **kwargs):
        return self._local.path(*args, **kwargs)
Example #3
0
def test_templatetag_multiple_parts_of_path():
    filenames = ('css/level1.css', 'css/level1-level2.css',
                 'css/level1-level2-level3.css')
    storage = StaticFilesStorage(location=settings.STATICFILES_TEST_DIR)
    try:
        for filename in filenames:
            storage.save(name=filename,
                         content=ContentFile("body { background: red; }"))
        resp = T('{% load path2css %}{% css4path "/level1/level2/level3/" %}'
                 ).render(CTX).strip()
        assert resp.split("\n") == [
            '<link href="{}css/level1.css" rel="stylesheet" type="text/css" />'
            .format(settings.STATIC_URL),
            '<link href="{}css/level1-level2.css" rel="stylesheet" type="text/css" />'
            .format(settings.STATIC_URL),
            '<link href="{}css/level1-level2-level3.css" rel="stylesheet" type="text/css" />'
            .format(settings.STATIC_URL)
        ]
    finally:
        for filename in filenames:
            storage.delete(filename)
Example #4
0
def test_templatetag_assignment(request_path, filenames):
    storage = StaticFilesStorage(location=settings.STATICFILES_TEST_DIR)
    try:
        for filename in filenames:
            storage.save(name=filename,
                         content=ContentFile("body { background: red; }"))
        resp = T('''
        {% load path2css %}
        {% css4path path as GOOSE %}
        {% for part, exists in GOOSE %}
        {% if exists %}{{ part }}{% endif %}
        {% endfor %}
        ''').render(Context({'path': request_path})).strip()
        parts = [x.strip() for x in resp.split("\n") if x.strip()]
        expected_output = [
            "{}{}".format(settings.STATIC_URL, filename)
            for filename in filenames
        ]
        assert parts == expected_output
    finally:
        for filename in filenames:
            storage.delete(filename)