上QQ阅读APP看书,第一时间看更新
How to do it...
The procedure to put the Git timestamp in the STATIC_URL setting consists of the following two steps:
- Add the following content to the misc.py file placed in utils/:
# utils/misc.py
import subprocess
from datetime import datetime
def get_git_changeset(absolute_path):
repo_dir = absolute_path
git_show = subprocess.Popen(
"git show --pretty=format:%ct --quiet HEAD",
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True,
cwd=repo_dir,
universal_newlines=True)
timestamp = git_show.communicate()[0].partition(‘\n’)[0]
try:
timestamp = datetime.utcfromtimestamp(int(timestamp))
except ValueError:
return ""
changeset = timestamp.strftime(‘%Y%m%d%H%M%S’)
return changeset
- Import the newly created get_git_changeset() function in the settings and use it for the STATIC_URL path, as follows:
# settings.py
# ... somewhere after BASE_DIR definition ...
from utils.misc import get_git_changeset
STATIC_URL = f'/static/{get_git_changeset(BASE_DIR)}/'