<s> from __future__ import unicode_literals <EOL> import json <EOL> from django import template <EOL> from django . core . serializers . json import DjangoJSONEncoder <EOL> from django . http import HttpResponse , HttpResponseForbidden <EOL> from django . shortcuts import get_object_or_404 <EOL> from django . template import RequestContext , Template <EOL> from django . views . debug import ExceptionReporter <EOL> from django . views . decorators . http import require_POST <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . shortcuts import render <EOL> from . forms import ScribbleForm , PreviewForm , FieldScribbleForm <EOL> from . models import Scribble <EOL> from . utils import get_variables <EOL> def build_scribble_context ( scribble ) : <EOL> "<STR_LIT>" <EOL> context = { <EOL> '<STR_LIT>' : scribble , <EOL> } <EOL> return context <EOL> @ require_POST <EOL> def preview_scribble ( request , ct_pk ) : <EOL> "<STR_LIT>" <EOL> if not request . user . is_authenticated ( ) : <EOL> return HttpResponseForbidden ( ) <EOL> content_type = get_object_or_404 ( ContentType , pk = ct_pk ) <EOL> change_scribble = '<STR_LIT>' . format ( <EOL> content_type . app_label , content_type . model ) <EOL> add_scribble = '<STR_LIT>' . format ( <EOL> content_type . app_label , content_type . model ) <EOL> can_edit = request . user . has_perm ( change_scribble ) <EOL> can_create = request . user . has_perm ( add_scribble ) <EOL> if not ( can_edit or can_create ) : <EOL> return HttpResponseForbidden ( ) <EOL> results = { <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT:html>' : '<STR_LIT>' , <EOL> } <EOL> form = PreviewForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> results [ '<STR_LIT>' ] = True <EOL> if hasattr ( template , '<STR_LIT>' ) : <EOL> scribbler_template = template . engines [ '<STR_LIT>' ] . from_string ( form . cleaned_data . get ( '<STR_LIT:content>' , '<STR_LIT>' ) ) <EOL> else : <EOL> scribbler_template = template . Template ( form . cleaned_data . get ( '<STR_LIT:content>' , '<STR_LIT>' ) ) <EOL> context = build_scribble_context ( form . instance ) <EOL> results [ '<STR_LIT:html>' ] = scribbler_template . render ( context , request ) <EOL> results [ '<STR_LIT>' ] = get_variables ( RequestContext ( request , context ) ) <EOL> else : <EOL> if hasattr ( form , '<STR_LIT>' ) : <EOL> try : <EOL> exc_type , exc_value , tb = form . exc_info <EOL> reporter = ExceptionReporter ( request , exc_type , exc_value , tb ) <EOL> reporter . get_template_exception_info ( ) <EOL> results [ '<STR_LIT:error>' ] = reporter . template_info <EOL> except ( ValueError , AttributeError ) : <EOL> results [ '<STR_LIT:error>' ] = form . exc_info <EOL> else : <EOL> results [ '<STR_LIT:error>' ] = { <EOL> '<STR_LIT:message>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> content = json . dumps ( results , cls = DjangoJSONEncoder , ensure_ascii = False ) <EOL> return HttpResponse ( content , content_type = '<STR_LIT:application/json>' ) <EOL> @ require_POST <EOL> def create_edit_scribble ( request , scribble_id = None ) : <EOL> "<STR_LIT>" <EOL> if not request . user . is_authenticated ( ) : <EOL> return HttpResponseForbidden ( ) <EOL> if scribble_id is not None : <EOL> scribble = get_object_or_404 ( Scribble , pk = scribble_id ) <EOL> if not request . user . has_perm ( '<STR_LIT>' ) : <EOL> return HttpResponseForbidden ( ) <EOL> else : <EOL> scribble = Scribble ( ) <EOL> if not request . user . has_perm ( '<STR_LIT>' ) : <EOL> return HttpResponseForbidden ( ) <EOL> form = ScribbleForm ( request . POST , instance = scribble ) <EOL> results = { <EOL> '<STR_LIT>' : False , <EOL> } <EOL> if form . is_valid ( ) : <EOL> results [ '<STR_LIT>' ] = True <EOL> scribble = form . save ( ) <EOL> results [ '<STR_LIT:url>' ] = scribble . get_save_url ( ) <EOL> content = json . dumps ( results , cls = DjangoJSONEncoder , ensure_ascii = False ) <EOL> return HttpResponse ( content , content_type = '<STR_LIT:application/json>' ) <EOL> @ require_POST <EOL> def edit_scribble_field ( request , ct_pk , instance_pk , field_name ) : <EOL> if not request . user . is_authenticated ( ) : <EOL> return HttpResponseForbidden ( ) <EOL> content_type = get_object_or_404 ( ContentType , pk = ct_pk ) <EOL> perm_name = '<STR_LIT>' . format ( content_type . app_label , content_type . model ) <EOL> if not request . user . has_perm ( perm_name ) : <EOL> return HttpResponseForbidden ( ) <EOL> form = FieldScribbleForm ( content_type , instance_pk , field_name , data = request . POST ) <EOL> results = { <EOL> '<STR_LIT>' : False , <EOL> } <EOL> if form . is_valid ( ) : <EOL> results [ '<STR_LIT>' ] = True <EOL> form . save ( ) <EOL> else : <EOL> results [ '<STR_LIT:error>' ] = { <EOL> '<STR_LIT:message>' : '<STR_LIT:U+002C>' . join ( '<STR_LIT:%s>' % e for e in form . errors . values ( ) ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> results [ '<STR_LIT:url>' ] = form . get_save_url ( ) <EOL> content = json . dumps ( results , cls = DjangoJSONEncoder , ensure_ascii = False ) <EOL> return HttpResponse ( content , content_type = '<STR_LIT:application/json>' ) <EOL> @ require_POST <EOL> def delete_scribble ( request , scribble_id ) : <EOL> "<STR_LIT>" <EOL> if not request . user . is_authenticated ( ) : <EOL> return HttpResponseForbidden ( ) <EOL> scribble = get_object_or_404 ( Scribble , pk = scribble_id ) <EOL> if not request . user . has_perm ( '<STR_LIT>' ) : <EOL> return HttpResponseForbidden ( ) <EOL> scribble . delete ( ) <EOL> results = { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT:url>' : scribble . get_save_url ( ) <EOL> } <EOL> content = json . dumps ( results , cls = DjangoJSONEncoder , ensure_ascii = False ) <EOL> return HttpResponse ( content , content_type = '<STR_LIT:application/json>' ) </s>
