当函数返回http响应时,我们如何将值转换为python对象?

时间:2016-02-15 10:35:01

标签: python django-views

Views.py。在这里,我从'yeartarg'函数调用'total_performance_ratio'函数来计算目标比率。 'total_performance_ratio'将http响应返回为{“status”:“[{\”estimated_code \“:\”MRO \“,\”percentage \“:\”0.00 \“,\”估计值\“:496.55172413793105,\ “won_deals \”:0,\“targetedvalue \”:0}]“}。我们如何将http响应转换为python对象`def yeartarg(request,year = None):

now = datetime.datetime.now()
if year is None:
    start_date = date(date.today().year, 1, 1)

else:
    start_date = year+'-'+01+'-'+01
last_date = date(date.today().year, 12, 31)
if date.today() < last_date :
    end_date = date.today()
else:
    end_date = last_date
user = get_user_id(request)
uid = user[0]['user_id']
cur = connection.cursor()
cur.execute("select role_id from myapp_custom_user_model where user_ptr_id="+str(uid))
role = dictfetchall(cur)
cur.close()
user_level = role[0]['role_id']
if user_level==1:
    user_performance = total_performance_ratio(request,start_date,end_date)

return JsonResponse({'status':user_performance})`

total_performance_ratio

def total_performance_ratio(request,start_date = None,end_date = None,amount = None):

    now = datetime.datetime.now()
    st_date, end_date = week_magic(date.today())
    cur = connection.cursor()
    cur.execute("select SUM(myapp_deal.deal_value) as d_value,myapp_currency_list.code from myapp_deal INNER JOIN myapp_currency_list on myapp_currency_list.id = myapp_deal.currency_id where myapp_deal.status=1 and myapp_deal.approved =0 and DATE_FORMAT(myapp_deal.closed_date,'%%Y-%%m-%%d') BETWEEN " '%s' " and " '%s' " group by myapp_deal.currency_id" %(start_date,end_date))
    CompletedTarget = dictfetchall(cur)
    cur.close()
    TargetValue = 0
    Wondeals = 0
    monthly_target = total_monthly_target()
    for data in CompletedTarget:
        TargetValue += convertCurrency( data['d_value'],data['code'],monthly_target[0]['code'])
        Wondeals  += data['won_deals']
    yearly_target = monthly_target[0]['monthly_target'] * 12
    start_date = date(date.today().year, 1, 1)
    last_date = date(date.today().year, 12, 31)
    diff  = last_date-start_date
    completed_days = diff.days

    now = datetime.datetime.now()
    this_month_total_days=calendar.monthrange(now.year, now.month)[1]
    one_day = float(monthly_target[0]['monthly_target'])/this_month_total_days
    date_st_date= datetime.date(st_date.year,st_date.month,st_date.day)
    str_st_date = date_st_date.strftime('%Y-%m-%d')
    strf = datetime.datetime.strptime(str_st_date, "%Y-%m-%d")
    date_f_strf= datetime.date(strf.year,strf.month,strf.day)
    date_end_date= datetime.date(end_date.year,end_date.month,end_date.day)
    str_end_date = date_end_date.strftime('%Y-%m-%d')
    strf_end_date = datetime.datetime.strptime(str_end_date, "%Y-%m-%d")
    end_date_f_strf= datetime.date(strf_end_date.year,strf_end_date.month,strf_end_date.day)


    diff  = end_date_f_strf - date_f_strf
    completed_days   = diff.days
    target_upto_this =  one_day * completed_days


    percentage = (TargetValue/float(target_upto_this) ) * 100
    if percentage >= 100:
          percent = 100
    else :
         percent = number_format(percentage,2)
    target1 =[]
    target1.append({'targetedvalue':TargetValue, 'estimatedvalue':target_upto_this,'estimated_code':monthly_target[0]['code'],'percentage':percent,'won_deals':Wondeals})
    data_json = json.dumps(target1)

    return json_response({'status':data_json})

1 个答案:

答案 0 :(得分:0)

更改

return json_response({'status':data_json})

def total_performance_ratio(request,start_date=None,end_date=None, amount=None)函数中

return {'status':data_json}

因为json_response应仅在视图中创建。

编辑:如果您想将total_performance_ratio的结果用作单独的视图,那么只需创建

def total_performance_ratio_view(request,start_date=None,end_date=None, amount=None):
    return JsonResponse(total_performance_ratio(request, start_date, end_date, amount))

这样,总性能比的计算可以在多个视图中重复使用。