summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorJannis Leidel <jannis@leidel.info>2012-05-16 13:21:46 +0200
committerJannis Leidel <jannis@leidel.info>2012-05-16 13:21:50 +0200
commit5f75ac91df2ef1c19946f65e08aa50165f061cd4 (patch)
treeb26e5faf50074bc8b84696adf4e33d2bea31658c /django
parent085c03e08b29fafc92ef49c2a3717ec23cf1c500 (diff)
Fixed #17896 -- Added file_hash method to CachedStaticFilesStorage to be able to customize the way the hashed name of a file is created. Thanks to mkai for the initial patch.
Diffstat (limited to 'django')
-rw-r--r--django/contrib/staticfiles/storage.py23
1 files changed, 16 insertions, 7 deletions
diff --git a/django/contrib/staticfiles/storage.py b/django/contrib/staticfiles/storage.py
index 7b5866a699..fd8f9efb02 100644
--- a/django/contrib/staticfiles/storage.py
+++ b/django/contrib/staticfiles/storage.py
@@ -64,6 +64,17 @@ class CachedFilesMixin(object):
compiled = re.compile(pattern)
self._patterns.setdefault(extension, []).append(compiled)
+ def file_hash(self, name, content=None):
+ """
+ Retuns a hash of the file with the given name and optional content.
+ """
+ if content is None:
+ return None
+ md5 = hashlib.md5()
+ for chunk in content.chunks():
+ md5.update(chunk)
+ return md5.hexdigest()[:12]
+
def hashed_name(self, name, content=None):
parsed_name = urlsplit(unquote(name))
clean_name = parsed_name.path.strip()
@@ -78,13 +89,11 @@ class CachedFilesMixin(object):
return name
path, filename = os.path.split(clean_name)
root, ext = os.path.splitext(filename)
- # Get the MD5 hash of the file
- md5 = hashlib.md5()
- for chunk in content.chunks():
- md5.update(chunk)
- md5sum = md5.hexdigest()[:12]
- hashed_name = os.path.join(path, u"%s.%s%s" %
- (root, md5sum, ext))
+ file_hash = self.file_hash(clean_name, content)
+ if file_hash is not None:
+ file_hash = u".%s" % file_hash
+ hashed_name = os.path.join(path, u"%s%s%s" %
+ (root, file_hash, ext))
unparsed_name = list(parsed_name)
unparsed_name[2] = hashed_name
# Special casing for a @font-face hack, like url(myfont.eot?#iefix")