Skip to content

Instantly share code, notes, and snippets.

@glitchedmob
Last active July 19, 2018 17:11
Show Gist options
  • Save glitchedmob/ccc58c37c8f05bd75fad878a8202e80f to your computer and use it in GitHub Desktop.
Save glitchedmob/ccc58c37c8f05bd75fad878a8202e80f to your computer and use it in GitHub Desktop.
import json
from urllib.parse import urljoin
from django import template
from django.conf import settings
register = template.Library()
@register.simple_tag
def mix(path, manifest_directory='static'):
if hasattr(settings, 'MANIFEST_LOCATION'):
manifest_directory = settings.MANIFEST_LOCATION
if not path.startswith('/'):
path = '/{}'.format(path)
if settings.DEBUG:
try:
f = open('{}/hot'.format(manifest_directory), 'r')
url = f.readline()
if url.startswith('https://') or url.startswith('http://'):
url = url.split(':', 1)[1]
return '{}{}'.format(url, path.strip('/'))
except IOError:
pass
try:
manifest_file = open('{}/mix-manifest.json'.format(manifest_directory), 'r').read()
manifest = json.loads(manifest_file)
return urljoin(settings.STATIC_URL, manifest.get(path, path).strip('/'))
except IOError:
return path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment