<commit_before>#!/usr/bin/env python
# encoding: utf-8

from django.http import HttpResponse
import json, mimetypes

class HttpResponseUnauthorized(HttpResponse):
    '''
    Http Response for status 401
    '''
    status_code = 401


class FileResponse(HttpResponse):
    '''
    Http Response with file content
    '''
    def __init__(self, content='', status=None, content_type=None):
        HttpResponse.__init__(self, content      = content, 
                                    mimetype     = mimetypes.guess_type(content.name)[0], 
                                    status       = status, 
                                    content_type = content_type, )
        self['Content-Disposition'] = 'attachment; filename=' + content.name


class JsonResponse(HttpResponse):
    '''
    Http Response with Json content type
    '''
    def __init__(self, content='', mimetype=None, status=None):
        HttpResponse.__init__(self, content      = json.dumps( content or [], indent=2 ), 
                                    mimetype     = mimetype, 
                                    status       = status, 
                                    content_type = 'application/json', )


class JsonpResponse(HttpResponse):
    '''
    Http Response with Jsonp content type
    '''
    def __init__(self, content='', mimetype=None, status=None, param='callback'):
        content = json.dumps( content or [], indent=2 )
        HttpResponse.__init__(self, content      = "%s(%s)" %(param, content), 
                                    mimetype     = mimetype,
                                    status       = status, 
                                    content_type = 'text/javascript; charset=utf-8', )


class ProperJsonResponse:
    '''
    Json or Jsonp Response according to request
    '''    
    def __init__(self, request):
        self.__jsonp_param = None
        for param in ['callback', 'jsonp']:
            if param in request.GET:
                self.__jsonp_param = param
                
    def __call__(self, *args, **kwargs):
        if self.__jsonp_param:
            return JsonpResponse(*args, param=self.__jsonp_param, **kwargs) 
        return JsonResponse(*args, **kwargs)


from settings import DEBUG_TOOLBAR_CONFIG
TAG = DEBUG_TOOLBAR_CONFIG['TAG'] if 'TAG' in DEBUG_TOOLBAR_CONFIG else 'body'

class JsonDebugResponse(HttpResponse):
    '''
    HTTP Response for debug purposes (django-debug-toolbar)
    '''
    def __init__(self, content='', mimetype=None, content_type=None, status=None):
        content = json.dumps( content or [], indent=2 )
        HttpResponse.__init__(self, content      = self.__json_to_html(content),
                                    mimetype     = mimetype,
                                    status       = status, 
                                    content_type = content_type,)
                                    
    def __json_to_html(self, content):<commit_msg>bugfix: check for DEBUG_TOOLBAR_CONFIG firstly<commit_after>#!/usr/bin/env python
# encoding: utf-8

from django.http import HttpResponse
import json, mimetypes

class HttpResponseUnauthorized(HttpResponse):
    '''
    Http Response for status 401
    '''
    status_code = 401


class FileResponse(HttpResponse):
    '''
    Http Response with file content
    '''
    def __init__(self, content='', status=None, content_type=None):
        HttpResponse.__init__(self, content      = content, 
                                    mimetype     = mimetypes.guess_type(content.name)[0], 
                                    status       = status, 
                                    content_type = content_type, )
        self['Content-Disposition'] = 'attachment; filename=' + content.name


class JsonResponse(HttpResponse):
    '''
    Http Response with Json content type
    '''
    def __init__(self, content='', mimetype=None, status=None):
        HttpResponse.__init__(self, content      = json.dumps( content or [], indent=2 ), 
                                    mimetype     = mimetype, 
                                    status       = status, 
                                    content_type = 'application/json', )


class JsonpResponse(HttpResponse):
    '''
    Http Response with Jsonp content type
    '''
    def __init__(self, content='', mimetype=None, status=None, param='callback'):
        content = json.dumps( content or [], indent=2 )
        HttpResponse.__init__(self, content      = "%s(%s)" %(param, content), 
                                    mimetype     = mimetype,
                                    status       = status, 
                                    content_type = 'text/javascript; charset=utf-8', )


class ProperJsonResponse:
    '''
    Json or Jsonp Response according to request
    '''    
    def __init__(self, request):
        self.__jsonp_param = None
        for param in ['callback', 'jsonp']:
            if param in request.GET:
                self.__jsonp_param = param
                
    def __call__(self, *args, **kwargs):
        if self.__jsonp_param:
            return JsonpResponse(*args, param=self.__jsonp_param, **kwargs) 
        return JsonResponse(*args, **kwargs)


import settings
if hasattr(settings, 'DEBUG_TOOLBAR_CONFIG'):
    if 'TAG' in settings.DEBUG_TOOLBAR_CONFIG:
        TAG = settings.DEBUG_TOOLBAR_CONFIG['TAG']
TAG = 'body'


class JsonDebugResponse(HttpResponse):
    '''
    HTTP Response for debug purposes (django-debug-toolbar)
    '''
    def __init__(self, content='', mimetype=None, content_type=None, status=None):
        content = json.dumps( content or [], indent=2 )
        HttpResponse.__init__(self, content      = self.__json_to_html(content),
                                    mimetype     = mimetype,
                                    status       = status, 
                                    content_type = content_type,)
                                    
    def __json_to_html(self, content):
{'fraction_non_alphanumeric': 0.09802278820375335, 'fraction_numerical': 0.004021447721179625, 'mean_word_length': 1.7892523364485982, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3791149', 'n_tokens_mistral': 1591, 'n_tokens_neox': 1485, 'n_words': 368}