views.py 2.12 KB
Newer Older
1
# View for semi-static templatized content.
Piotr Mitros committed
2 3 4 5
#
# List of valid templates is explicitly managed for (short-term)
# security reasons.

David Baumgold committed
6
from edxmako.shortcuts import render_to_response, render_to_string
7
from mako.exceptions import TopLevelLookupException
Piotr Mitros committed
8
from django.shortcuts import redirect
9
from django.conf import settings
10
from django.http import HttpResponseNotFound, HttpResponseServerError, Http404
11 12
from django_future.csrf import ensure_csrf_cookie

13
from util.cache import cache_if_anonymous
Piotr Mitros committed
14

15
valid_templates = []
16

17
if settings.STATIC_GRAB:
18 19 20 21 22
    valid_templates = valid_templates + [
        'server-down.html',
        'server-error.html'
        'server-overloaded.html',
    ]
Piotr Mitros committed
23

24 25

def index(request, template):
Piotr Mitros committed
26
    if template in valid_templates:
27
        return render_to_response('static_templates/' + template, {})
Piotr Mitros committed
28 29
    else:
        return redirect('/')
Piotr Mitros committed
30

31

32
@ensure_csrf_cookie
33
@cache_if_anonymous
34 35 36 37 38
def render(request, template):
    """
    This view function renders the template sent without checking that it
    exists. Do not expose template as a regex part of the url. The user should
    not be able to ender any arbitray template name. The correct usage would be:
39

40
    url(r'^jobs$', 'static_template_view.views.render', {'template': 'jobs.html'}, name="jobs")
41
    """
42
    return render_to_response('static_templates/' + template, {})
43

44

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
@ensure_csrf_cookie
@cache_if_anonymous
def render_press_release(request, slug):
    """
    Render a press release given a slug.  Similar to the "render" function above,
    but takes a slug and does a basic conversion to convert it to a template file.
    a) all lower case,
    b) convert dashes to underscores, and
    c) appending ".html"
    """
    template = slug.lower().replace('-', '_') + ".html"
    try:
        resp = render_to_response('static_templates/press_releases/' + template, {})
    except TopLevelLookupException:
        raise Http404
    else:
        return resp


64
def render_404(request):
65
    return HttpResponseNotFound(render_to_string('static_templates/404.html', {}))
66 67


68
def render_500(request):
69
    return HttpResponseServerError(render_to_string('static_templates/server-error.html', {}))