Пример #1
0
def preview_scribble(request):
    "Render scribble content or return error information."
    if not request.user.is_authenticated():
        return HttpResponseForbidden()
    can_edit = request.user.has_perm('scribbler.change_scribble')
    can_create = request.user.has_perm('scribbler.add_scribble')
    if not (can_edit or can_create):
        return HttpResponseForbidden()
    results = {
        'valid': False,
        'html': '',
    }
    form = PreviewForm(request.POST)
    if form.is_valid():
        results['valid'] = True
        template = Template(form.cleaned_data.get('content', ''))
        context = build_scribble_context(form.instance, request)
        results['html'] = template.render(context)
        results['variables'] = get_variables(context)
    else:
        if hasattr(form, 'exc_info'):
            exc_type, exc_value, tb = form.exc_info
            reporter = ExceptionReporter(request, exc_type, exc_value, tb)
            reporter.get_template_exception_info()
            results['error'] = reporter.template_info
        else:
            # Not sure what to do here
            results['error'] = {
                'message': 'Content is not valid',
                'line': '',
            }
    content = json.dumps(results, cls=DjangoJSONEncoder, ensure_ascii=False)
    return HttpResponse(content, content_type='application/json')
Пример #2
0
def preview_scribble(request):
    "Render scribble content or return error information."
    if not request.user.is_authenticated():
        return HttpResponseForbidden()
    can_edit = request.user.has_perm('scribbler.change_scribble')
    can_create = request.user.has_perm('scribbler.add_scribble')
    if not (can_edit or can_create):
        return HttpResponseForbidden()
    results = {
        'valid': False,
        'html': '',
    }
    form = PreviewForm(request.POST)
    if form.is_valid():
        results['valid'] = True
        template = Template(form.cleaned_data.get('content', ''))
        context = build_scribble_context(form.instance, request)
        results['html'] = template.render(context)
        results['variables'] = get_variables(context)
    else:
        if hasattr(form, 'exc_info'):
            exc_type, exc_value, tb = form.exc_info
            reporter = ExceptionReporter(request, exc_type, exc_value, tb)
            reporter.get_template_exception_info()
            results['error'] = reporter.template_info
        else:
            # Not sure what to do here
            results['error'] = {
                'message': 'Content is not valid',
                'line': '',
            }
    content = json.dumps(results, cls=DjangoJSONEncoder, ensure_ascii=False)
    return HttpResponse(content, content_type='application/json')
Пример #3
0
def preview_scribble(request, ct_pk):
    "Render scribble content or return error information."
    if not request.user.is_authenticated:
        return HttpResponseForbidden()
    content_type = get_object_or_404(ContentType, pk=ct_pk)
    change_scribble = '{0}.change_{1}'.format(
        content_type.app_label, content_type.model)
    add_scribble = '{0}.add_{1}'.format(
        content_type.app_label, content_type.model)
    can_edit = request.user.has_perm(change_scribble)
    can_create = request.user.has_perm(add_scribble)
    if not (can_edit or can_create):
        return HttpResponseForbidden()
    results = {
        'valid': False,
        'html': '',
    }
    form = PreviewForm(request.POST)
    if form.is_valid():
        results['valid'] = True
        if hasattr(template, 'engines'):
            scribbler_template = template.engines['django'].from_string(form.cleaned_data.get('content', ''))
        else:
            scribbler_template = template.Template(form.cleaned_data.get('content', ''))
        context = build_scribble_context(form.instance)
        results['html'] = scribbler_template.render(context, request)
        results['variables'] = get_variables(RequestContext(request, context))
    else:
        if hasattr(form, 'exc_info'):
            # Pre Django 1.9
            try:
                exc_type, exc_value, tb = form.exc_info
                reporter = ExceptionReporter(request, exc_type, exc_value, tb)
                reporter.get_template_exception_info()
                results['error'] = reporter.template_info
            # Django >= 1.9: get_template_info() is moved from ExceptionReporter
            # onto Template. We pass the data it returns from scribbler/forms.py
            # to here.
            except (ValueError, AttributeError):
                # ValueError is raised when we pass in all 12 the arguments,
                # in form.exc_info and AttributeError is raised when
                # ExceptionReporter.get_template_exception_info() is called.
                results['error'] = form.exc_info
        else:
            # Not sure what to do here
            results['error'] = {
                'message': 'Content is not valid',
                'line': '',
            }
    content = json.dumps(results, cls=DjangoJSONEncoder, ensure_ascii=False)
    return HttpResponse(content, content_type='application/json')
Пример #4
0
def preview_scribble(request, ct_pk):
    "Render scribble content or return error information."
    if not request.user.is_authenticated():
        return HttpResponseForbidden()
    content_type = get_object_or_404(ContentType, pk=ct_pk)
    change_scribble = '{0}.change_{1}'.format(
        content_type.app_label, content_type.model)
    add_scribble = '{0}.add_{1}'.format(
        content_type.app_label, content_type.model)
    can_edit = request.user.has_perm(change_scribble)
    can_create = request.user.has_perm(add_scribble)
    if not (can_edit or can_create):
        return HttpResponseForbidden()
    results = {
        'valid': False,
        'html': '',
    }
    form = PreviewForm(request.POST)
    if form.is_valid():
        results['valid'] = True
        if hasattr(template, 'engines'):
            scribbler_template = template.engines['django'].from_string(form.cleaned_data.get('content', ''))
        else:
            scribbler_template = template.Template(form.cleaned_data.get('content', ''))
        context = build_scribble_context(form.instance)
        results['html'] = scribbler_template.render(context, request)
        results['variables'] = get_variables(RequestContext(request, context))
    else:
        if hasattr(form, 'exc_info'):
            # Pre Django 1.9
            try:
                exc_type, exc_value, tb = form.exc_info
                reporter = ExceptionReporter(request, exc_type, exc_value, tb)
                reporter.get_template_exception_info()
                results['error'] = reporter.template_info
            # Django >= 1.9: get_template_info() is moved from ExceptionReporter
            # onto Template. We pass the data it returns from scribbler/forms.py
            # to here.
            except (ValueError, AttributeError):
                # ValueError is raised when we pass in all 12 the arguments,
                # in form.exc_info and AttributeError is raised when
                # ExceptionReporter.get_template_exception_info() is called.
                results['error'] = form.exc_info
        else:
            # Not sure what to do here
            results['error'] = {
                'message': 'Content is not valid',
                'line': '',
            }
    content = json.dumps(results, cls=DjangoJSONEncoder, ensure_ascii=False)
    return HttpResponse(content, content_type='application/json')
Пример #5
0
def template_debug_output():
    """ Usage:

        try:
            self.client.get(...)
        except:
            template_debug_output()
    """
    import sys
    import html
    from django.views.debug import ExceptionReporter

    reporter = ExceptionReporter(None, *sys.exc_info())
    reporter.get_template_exception_info()
    info = reporter.template_info
    print()
    print("Exception Message: " + info["message"])
    print("Template: " + info["name"])
    print()
    for line in info["source_lines"]:
        if line[0] == info["line"]:
            print("-->" + html.unescape(line[1])[3:-1])
        else:
            print(html.unescape(line[1])[:-1])