Advertisement
OlegKl

Untitled

Oct 24th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.71 KB | None | 0 0
  1. import os
  2.  
  3. from urllib.parse import urlsplit, unquote, urlunsplit
  4.  
  5. from django.contrib.staticfiles.storage import ManifestStaticFilesStorage
  6.  
  7.  
  8. class DjsManifestStaticFilesStorage(ManifestStaticFilesStorage):
  9.     manifest_strict = False
  10.  
  11.     def hashed_name(self, name, content=None, filename=None):
  12.         # `filename` is the name of file to hash if `content` isn't given.
  13.         # `name` is the base name to construct the new hashed filename from.
  14.         parsed_name = urlsplit(unquote(name))
  15.         clean_name = parsed_name.path.strip()
  16.         if filename:
  17.             filename = urlsplit(unquote(filename)).path.strip()
  18.         filename = filename or clean_name
  19.         opened = False
  20.         if content is None:
  21.             try:
  22.                 content = self.open(filename)
  23.             except IOError:
  24.                 # Handle directory paths and fragments
  25.                 return name
  26.             opened = True
  27.         try:
  28.             file_hash = self.file_hash(clean_name, content)
  29.         finally:
  30.             if opened:
  31.                 content.close()
  32.         path, filename = os.path.split(clean_name)
  33.         root, ext = os.path.splitext(filename)
  34.         if file_hash is not None:
  35.             file_hash = ".%s" % file_hash
  36.         hashed_name = os.path.join(path, "%s%s%s" %
  37.                                    (root, file_hash, ext))
  38.         unparsed_name = list(parsed_name)
  39.         unparsed_name[2] = hashed_name
  40.         # Special casing for a @font-face hack, like url(myfont.eot?#iefix")
  41.         # http://www.fontspring.com/blog/the-new-bulletproof-font-face-syntax
  42.         if '?#' in name and not unparsed_name[3]:
  43.             unparsed_name[2] += '?'
  44.         return urlunsplit(unparsed_name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement