<s> from __future__ import absolute_import , unicode_literals <EOL> import logging <EOL> import traceback <EOL> import warnings <EOL> from functools import wraps <EOL> from importlib import import_module <EOL> from django import http <EOL> from django . conf import settings <EOL> from django . core . exceptions import PermissionDenied <EOL> from django . core . handlers . base import BaseHandler <EOL> from django . core . serializers . json import DjangoJSONEncoder <EOL> from django . core . signals import got_request_exception <EOL> from django . utils import six <EOL> from django . utils . module_loading import import_string <EOL> from . exceptions import BadRequest <EOL> json = import_module ( getattr ( settings , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> JSON = '<STR_LIT:application/json>' <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> logger . info ( '<STR_LIT>' , json . __name__ ) <EOL> def _dump_json ( data ) : <EOL> options = getattr ( settings , '<STR_LIT>' , { } ) <EOL> if '<STR_LIT>' in options : <EOL> if isinstance ( options [ '<STR_LIT>' ] , six . string_types ) : <EOL> options [ '<STR_LIT>' ] = import_string ( options [ '<STR_LIT>' ] ) <EOL> else : <EOL> try : <EOL> use_django = getattr ( settings , '<STR_LIT>' ) <EOL> except AttributeError : <EOL> use_django = True <EOL> else : <EOL> warnings . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> DeprecationWarning ) <EOL> if use_django : <EOL> options [ '<STR_LIT>' ] = DjangoJSONEncoder <EOL> return json . dumps ( data , ** options ) <EOL> def json_view ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> content_type = kwargs . get ( '<STR_LIT>' , JSON ) <EOL> def decorator ( f ) : <EOL> @ wraps ( f ) <EOL> def _wrapped ( request , * a , ** kw ) : <EOL> try : <EOL> status = <NUM_LIT:200> <EOL> headers = { } <EOL> ret = f ( request , * a , ** kw ) <EOL> if isinstance ( ret , tuple ) : <EOL> if len ( ret ) == <NUM_LIT:3> : <EOL> ret , status , headers = ret <EOL> else : <EOL> ret , status = ret <EOL> if isinstance ( ret , http . HttpResponseNotAllowed ) : <EOL> blob = _dump_json ( { <EOL> '<STR_LIT:error>' : <NUM_LIT> , <EOL> '<STR_LIT:message>' : '<STR_LIT>' <EOL> } ) <EOL> return http . HttpResponse ( <EOL> blob , status = <NUM_LIT> , content_type = JSON ) <EOL> if isinstance ( ret , http . HttpResponse ) : <EOL> return ret <EOL> blob = _dump_json ( ret ) <EOL> response = http . HttpResponse ( blob , status = status , <EOL> content_type = content_type ) <EOL> for k in headers : <EOL> response [ k ] = headers [ k ] <EOL> return response <EOL> except http . Http404 as e : <EOL> blob = _dump_json ( { <EOL> '<STR_LIT:error>' : <NUM_LIT> , <EOL> '<STR_LIT:message>' : six . text_type ( e ) , <EOL> } ) <EOL> logger . warning ( '<STR_LIT>' , request . path , <EOL> extra = { <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : request , <EOL> } ) <EOL> return http . HttpResponseNotFound ( blob , content_type = JSON ) <EOL> except PermissionDenied as e : <EOL> logger . warning ( <EOL> '<STR_LIT>' , request . path , <EOL> extra = { <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : request , <EOL> } ) <EOL> blob = _dump_json ( { <EOL> '<STR_LIT:error>' : <NUM_LIT> , <EOL> '<STR_LIT:message>' : six . text_type ( e ) , <EOL> } ) <EOL> return http . HttpResponseForbidden ( blob , content_type = JSON ) <EOL> except BadRequest as e : <EOL> blob = _dump_json ( { <EOL> '<STR_LIT:error>' : <NUM_LIT> , <EOL> '<STR_LIT:message>' : six . text_type ( e ) , <EOL> } ) <EOL> return http . HttpResponseBadRequest ( blob , content_type = JSON ) <EOL> except Exception as e : <EOL> exc_data = { <EOL> '<STR_LIT:error>' : <NUM_LIT> , <EOL> '<STR_LIT:message>' : '<STR_LIT>' , <EOL> } <EOL> if settings . DEBUG : <EOL> exc_data [ '<STR_LIT:message>' ] = six . text_type ( e ) <EOL> exc_data [ '<STR_LIT>' ] = traceback . format_exc ( ) <EOL> blob = _dump_json ( exc_data ) <EOL> logger . error ( <EOL> '<STR_LIT>' , request . path , <EOL> exc_info = True , <EOL> extra = { <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : request <EOL> } <EOL> ) <EOL> got_request_exception . send ( sender = BaseHandler , request = request ) <EOL> return http . HttpResponseServerError ( blob , content_type = JSON ) <EOL> return _wrapped <EOL> if len ( args ) == <NUM_LIT:1> and callable ( args [ <NUM_LIT:0> ] ) : <EOL> return decorator ( args [ <NUM_LIT:0> ] ) <EOL> else : <EOL> return decorator </s>
