summaryrefslogtreecommitdiff
path: root/django/archives/util.py
diff options
context:
space:
mode:
authorJonathan S. Katz2018-04-17 18:10:27 +0000
committerJonathan S. Katz2018-04-18 15:56:37 +0000
commit08788312647862be2f07e6f141880bab1e93a821 (patch)
tree219686508db4404fb9710277db2e2691e51dda43 /django/archives/util.py
parent6d5af128e6030e8c45699ba38d70c007cd6f2bcd (diff)
Add PGWebContextProcessor from pgweb
This adapts the code for the "gitrev" variable that can be used in templates. This will enable cache busting for self hosted asset files. The path for where the archive app is stored differs from pgweb, so the relative directory is changed in this implementation.
Diffstat (limited to 'django/archives/util.py')
-rw-r--r--django/archives/util.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/django/archives/util.py b/django/archives/util.py
index 4bfe306..425fffe 100644
--- a/django/archives/util.py
+++ b/django/archives/util.py
@@ -1,5 +1,6 @@
from django.http import HttpResponse
from django.db import connection
+from django.utils.functional import SimpleLazyObject
def validate_new_user(username, email, firstname, lastname):
# Only allow user creation if they are already a subscriber
@@ -13,3 +14,31 @@ def validate_new_user(username, email, firstname, lastname):
return HttpResponse("You are not currently subscribed to any mailing list on this server. Account not created.")
+def _get_gitrev():
+ # Return the current git revision, that is used for
+ # cache-busting URLs.
+ try:
+ with open('../.git/refs/heads/master') as f:
+ return f.readline()[:8]
+ except IOError:
+ # A "git gc" will remove the ref and replace it with a packed-refs.
+ try:
+ with open('../.git/packed-refs') as f:
+ for l in f.readlines():
+ if l.endswith("refs/heads/master\n"):
+ return l[:8]
+ # Not found in packed-refs. Meh, just make one up.
+ return 'ffffffff'
+ except IOError:
+ # If packed-refs also can't be read, just give up
+ return 'eeeeeeee'
+
+# Template context processor to add information about the root link and
+# the current git revision. git revision is returned as a lazy object so
+# we don't spend effort trying to load it if we don't need it (though
+# all general pages will need it since it's used to render the css urls)
+def PGWebContextProcessor(request):
+ gitrev = SimpleLazyObject(_get_gitrev)
+ return {
+ 'gitrev': gitrev,
+ }