forked from apluslms/a-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.py
29 lines (23 loc) · 928 Bytes
/
storage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import os.path
from django.conf import settings
from django.contrib.staticfiles.storage import StaticFilesStorage
class BumpStaticFilesStorage(StaticFilesStorage):
def __init__(self, location=None, base_url=None, *args, **kwargs):
super().__init__(location, base_url, *args, **kwargs)
self.bump = self.create_bump()
def create_bump(self):
git_dir = os.path.join(settings.BASE_DIR, ".git")
if os.path.isdir(git_dir):
bump = self._read_file(os.path.join(git_dir, "HEAD"))
if bump.startswith("ref: "):
bump = self._read_file(os.path.join(git_dir, bump[5:].strip()))
return bump
return None
def _read_file(self, path):
with open(path, "r") as f:
return f.read()
def url(self, name):
url = super().url(name)
if self.bump:
return url + "?" + self.bump
return url