]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Separate generation of static URLs and versioning in order ease customization
authorBirk Nilson <me@birknilson.se>
Fri, 2 Dec 2011 12:28:03 +0000 (12:28 +0000)
committerBirk Nilson <me@birknilson.se>
Fri, 2 Dec 2011 12:28:03 +0000 (12:28 +0000)
of how versioning is handled in subclasses.

tornado/web.py

index c7801a4fefb2d3a93b5a18451017a15e90b362f5..906b01284585277172861bdad99bb96ca38dff65 100644 (file)
@@ -1528,6 +1528,21 @@ class StaticFileHandler(RequestHandler):
         is the static path being requested.  The url returned should be
         relative to the current host.
         """
+        static_url_prefix = settings.get('static_url_prefix', '/static/')
+        version_hash = cls.get_version(settings, path)
+        if version_hash:
+            return static_url_prefix + path + "?v=" + version_hash
+        return static_url_prefix + path
+
+    @classmethod
+    def get_version(cls, settings, path):
+        """Generate the version string to be appended as a query string
+        to the static URL - allowing aggressive caching.
+
+        ``settings`` is the `Application.settings` dictionary and ```path``
+        is the relative location of the requested asset on the filesystem.
+        """
+        hsh = None
         abs_path = os.path.join(settings["static_path"], path)
         with cls._lock:
             hashes = cls._static_hashes
@@ -1539,12 +1554,8 @@ class StaticFileHandler(RequestHandler):
                 except Exception:
                     logging.error("Could not open static file %r", path)
                     hashes[abs_path] = None
-            hsh = hashes.get(abs_path)
-        static_url_prefix = settings.get('static_url_prefix', '/static/')
-        if hsh:
-            return static_url_prefix + path + "?v=" + hsh[:5]
-        else:
-            return static_url_prefix + path
+            hsh = hashes.get(abs_path)[:5]
+        return hsh
 
 
 class FallbackHandler(RequestHandler):