Add easy way to do xkey based purging of a template
authorMagnus Hagander <magnus@hagander.net>
Wed, 16 Sep 2020 17:48:51 +0000 (19:48 +0200)
committerMagnus Hagander <magnus@hagander.net>
Wed, 16 Sep 2020 17:59:18 +0000 (19:59 +0200)
Since we can purge based on the md5 hash of a template name, expose this
in the purge dialog so one doesn't have to manually calculate the hash
to use it.

pgweb/core/views.py
templates/core/admin_purge.html

index 5ab4cb3f19e8df44b66b68279f28614fb12a5cd6..3373718e97669a4d77abc376219a348c725081fb 100644 (file)
@@ -16,6 +16,7 @@ from datetime import date, datetime, timedelta
 import os
 import re
 import urllib.parse
+import hashlib
 
 from pgweb.util.decorators import cache, nocache
 from pgweb.util.contexts import render_pgweb, get_nav_menu, PGWebContextProcessor
@@ -471,19 +472,28 @@ def admin_purge(request):
     if request.method == 'POST':
         url = request.POST['url']
         expr = request.POST['expr']
+        template = request.POST['template']
         xkey = request.POST['xkey']
-        l = len([_f for _f in [url, expr, xkey] if _f])
+        l = len([_f for _f in [url, expr, template, xkey] if _f])
         if l == 0:
             # Nothing specified
             return HttpResponseRedirect('.')
         elif l > 1:
-            messages.error(request, "Can only specify one of url, expression and xkey!")
+            messages.error(request, "Can only specify one of url, expression, template and xkey!")
             return HttpResponseRedirect('.')
 
         if url:
             varnish_purge(url)
         elif expr:
             varnish_purge_expr(expr)
+        elif template:
+            path = os.path.abspath(os.path.join(settings.PROJECT_ROOT, '../templates', template))
+            if not os.path.isfile(path):
+                messages.error(request, "Template {} does not exist!".format(template))
+                return HttpResponseRedirect('.')
+            # Calculate the xkey
+            xkey = "pgwt_{}".format(hashlib.md5(template.encode('ascii')).hexdigest())
+            varnish_purge_xkey(xkey)
         else:
             varnish_purge_xkey(xkey)
 
index 46c417afa6ae0a95074db94686214e5b402868dc..33155a4c3050a1e0f6e146e90ae6ead764fe22e6 100644 (file)
        <td>Full expression:</td>
        <td><input type="text" name="expr"></td>
      </tr>
+     <tr>
+       <td>Template:</td>
+       <td><input type="text" name="template"></td>
+     </tr>
      <tr>
        <td>XKey:</td>
        <td><input type="text" name="xkey"></td>