Allow displaying SVG images in documentation
authorMagnus Hagander <magnus@hagander.net>
Sun, 31 Mar 2019 12:19:45 +0000 (14:19 +0200)
committerMagnus Hagander <magnus@hagander.net>
Sun, 31 Mar 2019 12:26:42 +0000 (14:26 +0200)
PostgreSQL 12 adds SVG images in the documentation, so the website
should be able to display them.

The images themselves were already loaded by the docsloader, but the
regexps in the URL would block them from being seen. Fix this by
creating a separate function for the SVGs, since we also don't want to
render them inside teh templates.

This new view must also be tagged with @allow_frames, since the browser
considers the <object> tag used to be a subframe. Without this, they
would be blocked from viewing even on our own site.

pgweb/docs/views.py
pgweb/urls.py

index 928c1ca893a3d516c136bff715d44912e664b38e..1480903cf82229461b9e909c9829e8ad9aff87b7 100644 (file)
@@ -1,6 +1,6 @@
 from django.shortcuts import render, get_object_or_404
 from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect
-from django.http import Http404
+from django.http import HttpResponse, Http404
 from pgweb.util.decorators import login_required, allow_frames, content_sources
 from django.db.models import Q
 from django.conf import settings
@@ -113,6 +113,25 @@ def docpage(request, version, filename):
     })
 
 
+@allow_frames
+def docsvg(request, version, filename):
+    if version == 'current':
+        ver = Version.objects.filter(current=True)[0].tree
+    elif version == 'devel':
+        ver = Decimal(0)
+    else:
+        ver = Decimal(version)
+        if ver == Decimal(0):
+            raise Http404("Version not found")
+
+    if ver < Decimal(12) and ver > Decimal(0):
+        raise Http404("SVG images don't exist in this version")
+
+    page = get_object_or_404(DocPage, version=ver, file="{0}.svg".format(filename))
+
+    return HttpResponse(page.content, content_type="image/svg+xml")
+
+
 def docspermanentredirect(request, version, typ, page, *args):
     """Provides a permanent redirect from the old static/interactive pages to
     the modern pages that do not have said keywords.
index 80913c3fc23eaec66dde0a55f7df8ad6b1097d34..fe1e1c8d6a255ca7de1292ede8d1851e2c99c71d 100644 (file)
@@ -63,6 +63,7 @@ urlpatterns = [
     # Legacy URLs for accessing the docs page; provides a permanent redirect
     url(r'^docs/(current|devel|\d+(?:\.\d)?)/(static|interactive)/((.*).html?)?$', pgweb.docs.views.docspermanentredirect),
     url(r'^docs/(current|devel|\d+(?:\.\d)?)/(.*).html?$', pgweb.docs.views.docpage),
+    url(r'^docs/(current|devel|\d+(?:\.\d)?)/(.*).svg$', pgweb.docs.views.docsvg),
     url(r'^docs/(current|devel|\d+(?:\.\d)?)/$', pgweb.docs.views.docsrootpage),
     url(r'^docs/(current|devel|\d+(?:\.\d)?)/$', pgweb.docs.views.redirect_root),