views.py 1.4 KB
Newer Older
1 2 3
"""Views for debugging and diagnostics"""

import pprint
4
import traceback
5 6

from django.contrib.auth.decorators import login_required
7
from django.http import Http404, HttpResponse
8
from django.utils.html import escape
9
from django.views.decorators.csrf import ensure_csrf_cookie
10 11

from codejail.safe_exec import safe_exec
12
from edxmako.shortcuts import render_to_response
13

14

15 16 17
@login_required
@ensure_csrf_cookie
def run_python(request):
18
    """A page to allow testing the Python sandbox on a production server."""
19 20 21 22 23 24 25
    if not request.user.is_staff:
        raise Http404
    c = {}
    c['code'] = ''
    c['results'] = None
    if request.method == 'POST':
        py_code = c['code'] = request.POST.get('code')
26
        g = {}
27
        try:
28
            safe_exec(py_code, g)
29
        except Exception:   # pylint: disable=broad-except
30
            c['results'] = traceback.format_exc()
31
        else:
32
            c['results'] = pprint.pformat(g)
33
    return render_to_response("debug/run_python_form.html", c)
34 35 36 37


@login_required
def show_parameters(request):
38 39 40 41 42 43 44
    """A page that shows what parameters were on the URL and post."""
    html = []
    for name, value in sorted(request.GET.items()):
        html.append(escape("GET {}: {!r}".format(name, value)))
    for name, value in sorted(request.POST.items()):
        html.append(escape("POST {}: {!r}".format(name, value)))
    return HttpResponse("\n".join("<p>{}</p>".format(h) for h in html))