<commit_before>import sys
import logging
try:
    import cjson
    dumps = cjson.encode
    loads = cjson.decode
except ImportError:
    from django.utils.simplejson import dumps, loads

from django.http import HttpResponse
from ella.newman.permission import model_category_fk
from ella.newman import models
from ella.newman.config import STATUS_OK, STATUS_GENERIC_ERROR
from ella.newman.config import CATEGORY_FILTER, USER_CONFIG, JSON_CONVERSIONS
from ella.core.models import Category

log = logging.getLogger('ella.newman')

def json_encode(data):
    """ Encode python data into JSON. Try faster cjson first. """

    return dumps(data)

def json_decode(str):
    """ Decode JSON string into python. """

    return loads(str)

def JsonResponse(message, data={}, errors={}, status=STATUS_OK):
    """ Return JSON response in newman's standard format. """

    out_dict = {
        'status': status,
        'message': message,
    }
    http_status = 200
    if data:
        try: 
            data = json_encode(data)
        except: 
            http_status = 405
            log.error('Cannot decode json data in JsonResponse(), data=[%s]' % data)
        
        out_dict['data'] = data
    if errors:
        http_status = 405
        out_dict['errors'] = errors
    out = json_encode(out_dict)
    return HttpResponse(out, mimetype='text/plain;charset=utf-8', status=http_status)

def JsonResponseError(message, status=STATUS_GENERIC_ERROR):
    """ use this function if one message describes error well, so  """
    err_dict = {'error_description': message}
    return JsonResponse(message, errors=err_dict, status=status)

def decode_category_filter_json(data):
    decoded = json_decode(data)<commit_msg>JsonResponseError modified.<commit_after>import sys
import logging
try:
    import cjson
    dumps = cjson.encode
    loads = cjson.decode
except ImportError:
    from django.utils.simplejson import dumps, loads

from django.http import HttpResponse
from ella.newman.permission import model_category_fk
from ella.newman import models
from ella.newman.config import STATUS_OK, STATUS_GENERIC_ERROR
from ella.newman.config import CATEGORY_FILTER, USER_CONFIG, JSON_CONVERSIONS
from ella.core.models import Category

log = logging.getLogger('ella.newman')

def json_encode(data):
    """ Encode python data into JSON. Try faster cjson first. """

    return dumps(data)

def json_decode(str):
    """ Decode JSON string into python. """

    return loads(str)

def JsonResponse(message, data={}, errors={}, status=STATUS_OK, http_status=200):
    """ Return JSON response in newman's standard format. """

    out_dict = {
        'status': status,
        'message': message,
    }
    if data:
        try: 
            data = json_encode(data)
        except: 
            http_status = 405
            log.error('Cannot decode json data in JsonResponse(), data=[%s]' % data)
        
        out_dict['data'] = data
    if errors:
        http_status = 405
        out_dict['errors'] = errors
    out = json_encode(out_dict)
    return HttpResponse(out, mimetype='text/plain;charset=utf-8', status=http_status)

def JsonResponseError(message, status=STATUS_GENERIC_ERROR):
    """ use this function if one message describes error well, so  """
    return JsonResponse(message, status=status, http_status=405)

def decode_category_filter_json(data):
    decoded = json_decode(data)
{'fraction_non_alphanumeric': 0.0970873786407767, 'fraction_numerical': 0.006766696087084437, 'mean_word_length': 3.5152722443559097, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5680708', 'n_tokens_mistral': 1025, 'n_tokens_neox': 990, 'n_words': 280}