# -*- coding: UTF-8 -*-
import json
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt

import tushare as ts
from .publiceClass import DateEncoder

@csrf_exempt
def sh_margins(request):
    try:
        start = request.POST.get('start','')#选填
        end = request.POST.get('end','')#选填
        data = ts.sh_margins(start,end)
        res = {'columns':[
            '信用交易日期',
            '本日融资余额(元)',
            '本日融资买入额(元)',
            '本日融券余量',
            '本日融券余量金额(元)',
            '本日融券卖出量',
            '本日融资融券余额(元)'
        ],'data':json.loads(json.dumps(data.values,cls=DateEncoder))}
    except(BaseException):
        return HttpResponse(BaseException)
    else:

        return HttpResponse(json.dumps(res),content_type="application/json")


[((14, 15, 14, 39), 'tushare.sh_margins', 'ts.sh_margins', ({(14, 29, 14, 34): 'start', (14, 35, 14, 38): 'end'}, {}), '(start, end)', True, 'import tushare as ts\n'), ((25, 15, 25, 42), 'django.http.HttpResponse', 'HttpResponse', ({(25, 28, 25, 41): 'BaseException'}, {}), '(BaseException)', False, 'from django.http import HttpResponse\n'), ((28, 28, 28, 43), 'json.dumps', 'json.dumps', ({(28, 39, 28, 42): 'res'}, {}), '(res)', False, 'import json\n'), ((23, 28, 23, 67), 'json.dumps', 'json.dumps', (), '', False, 'import json\n')]