1 # -*- coding: utf-8 -*- 2 from dss.Serializer import serializer 3 from django.http import HttpResponse 4 5 6 def response_as_json(data, foreign_penetrate=False): 7 jsonString = serializer(data=data, output_type="json", foreign=foreign_penetrate) 8 response = HttpResponse( 9 # json.dumps(data, cls=MyEncoder), 10 jsonString, 11 content_type="application/json; charset=utf-8", 12 ) 13 response["Access-Control-Allow-Origin"] = "*" 14 return response 15 16 17 def json_response(data, code=200, foreign_penetrate=False, **kwargs): 18 data = { 19 "code": code, 20 "msg": "success", 21 "data": data, 22 } 23 data.update(**kwargs) 24 return response_as_json(data, foreign_penetrate=foreign_penetrate) 25 26 27 def json_error(error_string="", code=500, **kwargs): 28 data = { 29 "code": code, 30 "msg": error_string, 31 "data": {} 32 } 33 data.update(kwargs) 34 return response_as_json(data) 35 36 37 JsonResponse = json_response 38 JsonError = json_error