-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
"Merge pull request #57 from Code4SA/django-manifest\n\nIntegrate wit…
…h Django's ManifestStaticFileStorage"
- Loading branch information
Showing
3 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
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
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
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 |
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