Skip to content

Instantly share code, notes, and snippets.

@gavinwahl
Last active September 10, 2015 12:41
Show Gist options
  • Save gavinwahl/b7e8de4b92cf508ec13c to your computer and use it in GitHub Desktop.
Save gavinwahl/b7e8de4b92cf508ec13c to your computer and use it in GitHub Desktop.
Settings for django-compressor offline compression with Widgy and Mezzanine. Under the Simplified BSD License (https://www.fusionbox.com/license/).
"""
Provides all the settings that are required to get COMPRESS_OFFLINE mode
working for django-compressor, just import everything from this file into your
settings and you will have turned on the offline compress mode.
If you wish to test it on your local setup, you can import it in your local
settings file.
# myproject/settings.py
from .settings_compress_offline import *
"""
# Restrict the things that are imported when using *
__all__ = ["COMPRESS_OFFLINE", "COMPRESS_OFFLINE_CONTEXT"]
def get_setting_lazy(setting_name):
"""
Returns a function that returns the value of the setting_name.
"""
def inner():
from django.conf import settings
return getattr(settings, setting_name)
return inner
def get_mezzanine_settings():
"""
Returns the value of the mezzanine.conf.context_processors.settings context
processor. Context processors usually can only be run during a request, but
luckily, mezzanine ignores the request parameter, so we don't have to worry
about it.
"""
from mezzanine.conf.context_processors import settings
return settings()['settings']
def get_site():
"""
Returns the Widgy site.
"""
# if not using widgy_mezzanine, just import your widgy site directly here.
from settings import WIDGY_MEZZANINE_SITE
from widgy.utils import fancy_import
return fancy_import(WIDGY_MEZZANINE_SITE)
# Kick compressor into OFFLINE mode
COMPRESS_OFFLINE = True
# This is the context that is provided when compressing assets offline.
COMPRESS_OFFLINE_CONTEXT = {
'STATIC_URL': get_setting_lazy('STATIC_URL'),
# some mezzanine templates require the settings.
'settings': get_mezzanine_settings,
# widgy_field.html needs the site
'site': get_site,
}
"""
Provides all the settings that are required to get COMPRESS_OFFLINE mode
working for django-compressor, just import everything from this file into your
settings and you will have turned on the offline compress mode.
If you wish to test it on your local setup, you can import it in your local
settings file.
# myproject/settings.py
from .settings_compress_offline import *
"""
# Restrict the things that are imported when using *
__all__ = ["COMPRESS_OFFLINE", "COMPRESS_OFFLINE_CONTEXT"]
def get_setting_lazy(setting_name):
"""
Returns a function that returns the value of the setting_name.
"""
def inner():
from django.conf import settings
return getattr(settings, setting_name)
return inner
def get_mezzanine_settings():
"""
Returns the value of the mezzanine.conf.context_processors.settings context
processor. Context processors usually can only be run during a request, but
luckily, mezzanine ignores the request parameter, so we don't have to worry
about it.
"""
from mezzanine.conf.context_processors import settings
return settings()['settings']
def get_site():
"""
Returns the Widgy site.
"""
# if not using widgy_mezzanine, just import your widgy site directly here.
from settings import WIDGY_MEZZANINE_SITE
from widgy.utils import fancy_import
return fancy_import(WIDGY_MEZZANINE_SITE)
# Kick compressor into OFFLINE mode
COMPRESS_OFFLINE = True
# This is the context that is provided when compressing assets offline.
COMPRESS_OFFLINE_CONTEXT = {
'STATIC_URL': get_setting_lazy('STATIC_URL'),
# some mezzanine templates require the settings.
'settings': get_mezzanine_settings,
# widgy_field.html needs the site
'site': get_site,
}
@mauricioabreu
Copy link

Nice! Thanks for this gist.

I am having the following error:
django.template.base.VariableDoesNotExist: Failed lookup for key [request] in u"[{'False': False, 'None': None, 'True': True}, {u'STATIC_URL': '/static/'}]"

I think it is happening because compress is trying to access request when compressing. Is there any way to avoid this? Did you get this error in the past?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment