kastielspb

Static hesher

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