summaryrefslogtreecommitdiff
path: root/postgresqleu/static/views.py
blob: d45cbfb2a4795f70c6982762f1da36898992fcd9 (plain)
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
from django.http import HttpResponse, Http404
from django.template import loader, TemplateDoesNotExist

import re

re_staticfilenames = re.compile("^[0-9A-Z/_-]+$", re.IGNORECASE)


# Fallback handler for URLs not matching anything else. Fall them
# back to a static template. If that one is not found, send a 404
# error.
def static_fallback(request, url):
    # Disallow all URLs that back-step
    if url.find('..') > -1:
        raise Http404('Page not found')

    if not re_staticfilenames.match(url):
        raise Http404('Page not found.')

    if len(url) > 250:
        # Maximum length is really per-directory, but we shouldn't have any pages/fallback
        # urls with anywhere *near* that, so let's just limit it on the whole
        raise Http404('Page not found.')

    try:
        t = loader.get_template('pages/%s.html' % url)
        return HttpResponse(t.render())
    except TemplateDoesNotExist:
        raise Http404('Page not found')