Created
May 19, 2012 10:59
-
-
Save rahulkmr/2730466 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| def auto_version(endpoint, **values): | |
| """ | |
| Auto versions static assets. | |
| """ | |
| from flask import current_app as app | |
| from flask import url_for | |
| if app.debug: | |
| return url_for(endpoint, **values) | |
| # Check if accessing static file. | |
| static_root = None | |
| if endpoint == 'static': | |
| static_root = app.static_folder | |
| if not static_root: | |
| m = re.match(r'(.+)\.static', endpoint) | |
| if m: | |
| try: | |
| static_root = app.blueprints[m.group(1)].static_folder | |
| except: | |
| pass | |
| # Get the timestamp of the file. | |
| if static_root: | |
| filename = values.get('filename', None) | |
| if filename: | |
| file_path = os.path.join(static_root, filename) | |
| if os.path.splitext(file_path) in ['.css', '.coffee']: | |
| file_path = re.sub('(css|coffee)', {'css': 'less', 'coffee': 'js'}["\\1"], file_path) | |
| if os.path.isfile(file_path): | |
| values['q'] = int(os.stat(file_path).st_mtime) | |
| return url_for(endpoint, **values) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment