Skip to content

Commit

Permalink
"Merge pull request #57 from Code4SA/django-manifest\n\nIntegrate wit…
Browse files Browse the repository at this point in the history
…h Django's ManifestStaticFileStorage"
  • Loading branch information
miracle2k committed Jul 1, 2015
2 parents b7e2a73 + dc6ff29 commit fd37df7
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion django_assets/management/commands/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
GenericArgparseImplementation)
from django_assets.env import get_env, autoload
from django_assets.loaders import get_django_template_dirs, DjangoLoader

from django_assets.manifest import DjangoManifest # noqa: enables the --manifest django option

try:
from django.core.management import LaxOptionParser
Expand Down
59 changes: 59 additions & 0 deletions django_assets/manifest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import os

from webassets.version import Manifest

try:
from django.contrib.staticfiles.storage import ManifestStaticFilesStorage

class DjangoManifest(Manifest):
"""Stores version data in Django's ManifestStaticFileStorage.
Requires Django 1.7 or later.
"""

id = 'django'

def __init__(self, *a, **kw):
try:
import json
except ImportError:
import simplejson as json
self.json = json
self.storage = ManifestStaticFilesStorage()
self._load_manifest()

def remember(self, bundle, ctx, version):
output_filename = bundle.resolve_output(ctx, version=version)
output_filename = os.path.relpath(output_filename, ctx.directory)
source_filename = self.name_without_hash(bundle.output)

self.storage.hashed_files[source_filename] = output_filename
self._save_manifest()

def query(self, bundle, ctx):
if ctx.auto_build:
self._load_manifest()

source_filename = self.name_without_hash(bundle.output)
output = self.storage.hashed_files.get(source_filename, None)
if output:
# foo.hash.js
name_with_hash, ext = os.path.splitext(output)
output = os.path.splitext(name_with_hash)[1]

return output

def name_without_hash(self, filename):
name_with_hash, ext = os.path.splitext(filename)
name = os.path.splitext(name_with_hash)[0]
return name + ext

def _load_manifest(self):
self.storage.load_manifest()

def _save_manifest(self):
self.storage.save_manifest()

except ImportError:
class DjangoManifest(object):
pass
13 changes: 13 additions & 0 deletions docs/staticfiles.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,16 @@ define an ``ASSETS_ROOT`` setting that points to a different directory
then ``STATIC_ROOT``. Only then will ``collectstatic`` be able to find the
output files created with ``./manage.py build --parse-templates``, and
process them into ``STATIC_ROOT``, like any other static file.

``ManifestStaticFileStorage`` or White Noise
--------------------------------------------

If you are using Django's ``ManifestStaticFilesStorage`` or White Noise's
``GzipManifestStaticFilesStorage`` then you must build your assets after
calling ``collectstatic`` using the ``--manifest django`` option::

./manage.py assets build --manifest django
This will add the built assets to Django's static files manifest. In particular,
this ensures that White Noise realises they are cacheable static files and
will add appropriate far-future expiry headers when serving them.

0 comments on commit fd37df7

Please sign in to comment.