@require_POST
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, __HOLE__):
                # 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')
dataset/ETHPy150Open caktus/django-scribbler/scribbler/views.py/preview_scribble