Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fails with remote backends (eg S3) #14

Open
sibson opened this issue Jan 27, 2014 · 5 comments
Open

Fails with remote backends (eg S3) #14

sibson opened this issue Jan 27, 2014 · 5 comments

Comments

@sibson
Copy link

sibson commented Jan 27, 2014

We use django-storages and S3/Cloudfront for our static resources due to the direct open call in https://github.com/roverdotcom/django-inlinecss/blob/master/django_inlinecss/templatetags/inlinecss.py#L30

File "django/template/base.py", line 830, in render
bit = self.render_node(node, context)
File "django/template/base.py", line 844, in render_node
return node.render(context)
File "django_inlinecss/templatetags/inlinecss.py", line 28, in render
expanded_path = staticfiles_storage.path(path)
File "django/core/files/storage.py", line 85, in path
raise NotImplementedError("This backend doesn't support absolute paths.")

Using staticfiles_storage.open (https://docs.djangoproject.com/en/dev/ref/files/storage/#django.core.files.storage.Storage.open) rather than path() + open is a possible solution

@samcheng
Copy link

I've run into this as well - any other workarounds?

@derek73
Copy link

derek73 commented Aug 17, 2014

I was just perusing the project to see if I wanted to try it. Haven't run into the problem but it seems like it's because the file is not stored locally. Maybe then the cached s3 storage would work around it?

from storages.backends.s3boto import S3BotoStorage
class CachedS3BotoStorage(S3BotoStorage):
    """
    S3 storage backend that saves the files both remotely and locally.

    See http://django_compressor.readthedocs.org/en/latest/remote-storages/
    """
    def __init__(self, *args, **kwargs):
        super(CachedS3BotoStorage, self).__init__(*args, **kwargs)
        self.local_storage = get_storage_class(
            "django.core.files.storage.FileSystemStorage")()

    def save(self, name, content):
        name = super(CachedS3BotoStorage, self).save(name, content)
        self.local_storage._save(name, content)
        return name

@owais
Copy link

owais commented Sep 15, 2014

@raverdotcom any update on this?

@sttwister
Copy link

Fixed it by including local storage backend as well

class CachedS3BotoStorage(S3BotoStorage):
    """
    S3 storage backend that saves the files locally, too.

    It's needed for static files and django-compressor to get along :)
    """
    def __init__(self, *args, **kwargs):
        super(CachedS3BotoStorage, self).__init__(*args, **kwargs)
        self.local_storage = get_storage_class(
            "compressor.storage.CompressorFileStorage")()

    def save(self, name, content):
        """Save both on S3 and locally."""
        name = super(CachedS3BotoStorage, self).save(name, content)
        self.local_storage._save(name, content)
        return name

    def path(self, name):
        """
        Return absolute path as saved in local stoarge backend.

        Required by django-inlinecss.
        """
        return self.local_storage.path(name)

@derek73
Copy link

derek73 commented Mar 1, 2016

Adding that path() method to the cached storage backend causes other css files not to build for me.

This seems to be the right solution:

lukeburden@f3d74c1

staticfiles_storage.path() seems the wrong thing to use to find the css file. Finders are for finding static files. Many of the other recent forks of this project explore other ways of always using finders too. Here's where it was added. cc40f4b and 7771f98 Doesn't make sense to me.

ldgarcia pushed a commit to ldgarcia/django-inlinecss that referenced this issue Jan 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants