Skip to content

Instantly share code, notes, and snippets.

@onitonitonito
Last active July 24, 2017 16:51
Show Gist options
  • Save onitonitonito/5fcf6e22afc73af62e5fa40737f6cea0 to your computer and use it in GitHub Desktop.
Save onitonitonito/5fcf6e22afc73af62e5fa40737f6cea0 to your computer and use it in GitHub Desktop.
Solving CSS Cache Problems in FLASK web framework on Windows Platform
'''
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
'''
'''
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)
'''
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
@onitonitonito
Copy link
Author

onitonitonito commented Jun 28, 2017

Logging would be...

--------------------------------------------------------------------------------
DEBUG in flask_app [flask_app.py:19]:
{'filename': 'css/flask.css', 'q': 668776}
--------------------------------------------------------------------------------
127.0.0.1 - - [28/Jun/2017 15:31:59] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [28/Jun/2017 15:31:59] "GET /css/flask.css?q=668776 HTTP/1.1" 200 -
127.0.0.1 - - [28/Jun/2017 15:32:00] "GET /imgs/0_angry_tig.jpg HTTP/1.1" 200 -

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment