http.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #coding=utf-8
  2. """JSON helper functions"""
  3. import traceback
  4. import sys
  5. import thread
  6. from django.conf import settings
  7. from django.http import JsonResponse as DJR
  8. #from exceptionhandler import post_traceback
  9. def JsonResponse(data=None):
  10. ret = {
  11. 'code': 0,
  12. 'data': data,
  13. }
  14. return DJR(ret, safe=False)
  15. def JsonError(error_string):
  16. # if not 'dev' in settings.LICENSE_KEY:
  17. # exc_type, exc_value, exc_traceback_obj = sys.exc_info()
  18. # if exc_type and exc_type.__name__ != 'ValueError':
  19. # trace = traceback.format_exc()
  20. # try:
  21. # thread.start_new_thread(post_traceback, (exc_value, trace,))
  22. # except:
  23. # print "Error: unable to start report exception thread"
  24. # #print '******', exc_type.__name__, type(exc_type.__name__)
  25. data = {
  26. 'code': 1,
  27. 'msg': error_string
  28. }
  29. return DJR(data, safe=False, json_dumps_params={'ensure_ascii':False})
  30. def ForbiddenResponse():
  31. data = {
  32. 'code': 1001,
  33. 'msg': u'拒绝访问',
  34. }
  35. return DJR(data, safe=False)
  36. def DataGridResponse(data, total, more=None):
  37. result = {
  38. 'code': 0,
  39. 'count':total,
  40. 'data': data
  41. }
  42. if more:
  43. result['more'] = more
  44. return DJR(result, safe=False)
  45. def EasyuiDataGridResponse(data, total):
  46. result = {
  47. 'total':total,
  48. 'rows': data,
  49. }
  50. return DJR(result, safe=False)
  51. # For backwards compatability purposes
  52. JSONResponse = JsonResponse
  53. JSONError = JsonError
  54. DataGridJSONResponse = DataGridResponse
  55. ForbiddenJSONResponse = ForbiddenResponse