Last active
July 19, 2018 17:11
-
-
Save glitchedmob/ccc58c37c8f05bd75fad878a8202e80f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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