Skip to content
Browse files

Use Django's static finder if appropriate.

When DEBUG is true and the new USE_STATIC setting is set, let Django see
if it can find the static file. This allows files to be found that are
in 'project/app/static', instead of only the centralized static
directory.

This change really only effects development, but it prevents having to
run './manage.py collecstatic' after every change while in development.
Instead files static files will be updated as they are changed so edit,
save, refresh works.
  • Loading branch information...
1 parent e2b96eb commit 58c0b004bcf4b6c8ddc6c434419e081a292216b0 @mythmon committed Sep 5, 2012
Showing with 39 additions and 5 deletions.
  1. +39 −5 jingo_minify/helpers.py
View
44 jingo_minify/helpers.py
@@ -3,9 +3,10 @@
import time
from django.conf import settings
+from django.contrib.staticfiles.finders import find as static_finder
import jinja2
-from jingo import register, env
+from jingo import register
try:
@@ -39,7 +40,28 @@ def get_media_url():
return settings.MEDIA_URL
-path = lambda *a: os.path.join(get_media_root(), *a)
+def get_path(path):
+ """Get a system path for a given file.
+
+ This properly handles storing files in `project/app/static`, and any other
+ location that Django's static files system supports.
+
+ ``path`` should be relative to ``STATIC_ROOT``.
+
+ """
+ debug = getattr(settings, 'DEBUG', False)
+ static = getattr(settings, 'JINGO_MINIFY_USE_STATIC', False)
+
+ full_path = os.path.join(get_media_root(), path)
+
+ if debug and static:
+ found_path = static_finder(path)
+ # If the path is not found by Django's static finder (like we are
+ # trying to get an output path), it returns None, so fall back.
+ if found_path is not None:
+ full_path = found_path
+
+ return full_path
def _get_item_path(item):
@@ -122,21 +144,33 @@ def css(bundle, media=False, debug=settings.TEMPLATE_DEBUG):
return _build_html(items,
'<link rel="stylesheet" media="%s" href="%%s" />' % media)
+
+def ensure_path_exists(path):
+ try:
+ os.makedirs(path)
+ except OSError as e:
+ # If the directory already exists, that is fine. Otherwise re-raise.
+ if e.errno != os.errno.EEXIST:
+ raise e
+
+
def build_less(item):
- path_css = path('%s.css' % item)
- path_less = path(item)
+ path_css = get_path('%s.css' % item)
+ path_less = get_path(item)
- updated_less = os.path.getmtime(path(item))
+ updated_less = os.path.getmtime(get_path(item))
updated_css = 0 # If the file doesn't exist, force a refresh.
if os.path.exists(path_css):
updated_css = os.path.getmtime(path_css)
# Is the uncompiled version newer? Then recompile!
if updated_less > updated_css:
+ ensure_path_exists(os.path.dirname(path_css))
with open(path_css, 'w') as output:
subprocess.Popen([settings.LESS_BIN, path_less],
stdout=output)
+
def build_ids(request):
"""A context processor for injecting the css/js build ids."""
return {'BUILD_ID_CSS': BUILD_ID_CSS, 'BUILD_ID_JS': BUILD_ID_JS,

0 comments on commit 58c0b00

Please sign in to comment.
Something went wrong with that request. Please try again.