#coding=utf-8

"""JSON helper functions"""
import copy
import traceback
import sys
import _thread

from django.conf import settings
from django.http import JsonResponse as DJR

def JsonEditData(data=None):
    ret = {
        'success': 1,
        'url': data,
        'message': 'success'
    }
    return DJR(ret, safe=False)

def JsonData(data=None):
    return DJR(data, safe=False)

def JsonResponse(data=None):
    ret = {
        'code': 0,
        'data': data,
    }
    return DJR(ret, safe=False)

def JsonError(error_string):
    data = {
        'code': 1,
        'msg': error_string
    }
    return DJR(data, safe=False, json_dumps_params={'ensure_ascii':False})

def ForbiddenResponse():
    data = {
        'code': 1001,
        'msg': u'拒绝访问',
    }
    return DJR(data, safe=False)

def DataGridResponse(data, total, more=None):
    result = {
        'code': 0,
        'count':total,
        'data': data
    }
    if more:
        result['more'] = more
    return DJR(result, safe=False)

def EasyuiDataGridResponse(data, total):
    result = {
        'total':total,
        'rows': data,
    }
    return DJR(result, safe=False)

# For backwards compatability purposes
JSONResponse = JsonResponse
JSONError = JsonError
DataGridJSONResponse = DataGridResponse
ForbiddenJSONResponse = ForbiddenResponse
JsonData = JsonData
JSONEditdata = JsonEditData
