Last active
July 24, 2017 16:51
-
-
Save onitonitonito/5fcf6e22afc73af62e5fa40737f6cea0 to your computer and use it in GitHub Desktop.
Solving CSS Cache Problems in FLASK web framework on Windows Platform
This file contains 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
''' | |
flask.org suggests to use snipets of cache buster as below | |
http://flask.pocoo.org/snippets/40/ | |
''' | |
@app.context_processor | |
def override_url_for(): | |
return dict(url_for=dated_url_for) | |
def dated_url_for(endpoint, **values): | |
if endpoint == 'static': | |
filename = values.get('filename', None) | |
if filename: | |
file_path = os.path.join(app.root_path, | |
endpoint, filename) | |
values['q'] = int(os.stat(file_path).st_mtime) | |
return url_for(endpoint, **values) | |
''' | |
but problem happens line 16 --> os.stat(file_path).st_mtime | |
it returns wrong file_path mixed with '/' and '\' like this and causing problem | |
....Flask\static\css/flask.css?q=1280549780 | |
''' |
This file contains 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
''' | |
while HTML has CSS ref as below. | |
funtion gives override on path+filename like, css/flask.css?q=7831243, the numbers were randomly given. | |
<link rel="stylesheet" href="{{ url_for('static', filename='css/flask.css') }}"> | |
''' | |
import os, random | |
@app.context_processor | |
def override_url_for(): | |
# app.logger.debug(dict(url_for=dated_url_for)) | |
return dict(url_for=dated_url_for) | |
def dated_url_for(endpoint, **values): # double stars = dict_type | |
if endpoint == 'static' : | |
filename = values.get('filename', None) # None = default values if it's missing. | |
if filename and filename[0:3] == 'css': | |
file_path = os.path.join(app.root_path, endpoint, filename) | |
values['q'] = int(random.randint(0,10000000)) | |
app.logger.debug(values) | |
return url_for(endpoint, **values) | |
This file contains 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
''' | |
Stackoverflow Q&A saids Error's a matter of flatform,windows | |
https://stackoverflow.com/questions/237079/how-to-get-file-creation-modification-date-times-in-python | |
''' | |
import os | |
import platform | |
def creation_date(path_to_file): | |
""" | |
Try to get the date that a file was created, falling back to when it was | |
last modified if that isn't possible. | |
See http://stackoverflow.com/a/39501288/1709587 for explanation. | |
""" | |
if platform.system() == 'Windows': | |
return os.path.getctime(path_to_file) | |
else: | |
stat = os.stat(path_to_file) | |
try: | |
return stat.st_birthtime | |
except AttributeError: | |
# We're probably on Linux. No easy way to get creation dates here, | |
# so we'll settle for when its content was last modified. | |
return stat.st_mtime |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Logging would be...