<s> import json <EOL> import logging <EOL> import traceback <EOL> from functools import wraps <EOL> from django import http <EOL> from django . conf import settings <EOL> from django . core . exceptions import PermissionDenied <EOL> from django . core . urlresolvers import reverse as _reverse <EOL> from django . utils . http import urlencode <EOL> from dimagi . utils . web import get_url_base <EOL> from corehq . util import global_request <EOL> JSON = '<STR_LIT:application/json>' <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> def set_file_download ( response , filename ) : <EOL> response [ "<STR_LIT>" ] = '<STR_LIT>' % filename <EOL> class BadRequest ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def json_error ( f ) : <EOL> """<STR_LIT>""" <EOL> @ wraps ( f ) <EOL> def inner ( request , * args , ** kwargs ) : <EOL> try : <EOL> response = f ( request , * args , ** kwargs ) <EOL> if isinstance ( response , http . HttpResponseNotAllowed ) : <EOL> blob = json . dumps ( { <EOL> '<STR_LIT:error>' : <NUM_LIT> , <EOL> '<STR_LIT:message>' : '<STR_LIT>' <EOL> } ) <EOL> return http . HttpResponse ( blob , status = <NUM_LIT> , content_type = JSON ) <EOL> return response <EOL> except http . Http404 as e : <EOL> return _get_json_exception_response ( <NUM_LIT> , request , e , log_message = '<STR_LIT>' ) <EOL> except PermissionDenied as e : <EOL> return _get_json_exception_response ( <NUM_LIT> , request , e , log_message = '<STR_LIT>' ) <EOL> except BadRequest as e : <EOL> return _get_json_exception_response ( <NUM_LIT> , request , e ) <EOL> except Exception as e : <EOL> return _get_json_exception_response ( <NUM_LIT> , request , e ) <EOL> return inner <EOL> def _get_json_exception_response ( code , request , exception , log_message = None ) : <EOL> if log_message : <EOL> logger . warning ( <EOL> log_message , request . path , <EOL> extra = { <EOL> '<STR_LIT>' : code , <EOL> '<STR_LIT>' : request , <EOL> } ) <EOL> data = _json_exception_response_data ( code , exception ) <EOL> return http . HttpResponse ( <EOL> status = code , <EOL> content = json . dumps ( data ) , <EOL> content_type = JSON <EOL> ) <EOL> def _json_exception_response_data ( code , exception ) : <EOL> if isinstance ( exception . message , unicode ) : <EOL> message = unicode ( exception ) <EOL> else : <EOL> message = str ( exception ) . decode ( '<STR_LIT:utf-8>' ) <EOL> data = { <EOL> '<STR_LIT:error>' : code , <EOL> '<STR_LIT:message>' : message <EOL> } <EOL> if code == <NUM_LIT> and settings . DEBUG : <EOL> data [ '<STR_LIT>' ] = traceback . format_exc ( ) <EOL> return data <EOL> def get_request ( ) : <EOL> return global_request . get_request ( ) <EOL> def reverse ( viewname , params = None , absolute = False , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> url = _reverse ( viewname , ** kwargs ) <EOL> if absolute : <EOL> url = "<STR_LIT>" . format ( get_url_base ( ) , url ) <EOL> if params : <EOL> url = "<STR_LIT>" . format ( url , urlencode ( params ) ) <EOL> return url <EOL> def absolute_reverse ( * args , ** kwargs ) : <EOL> return reverse ( * args , absolute = True , ** kwargs ) </s>
