繁体   English   中英

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

[英]How can we convert the values in to python object when http response is returned by a function?

Views.py。 在这里,我从“ yeartarg”函数调用“ total_performance_ratio”函数,以计算目标比率。 'total_performance_ratio'返回http响应为{“ status”:“ [{\\” estimated_code \\“:\\” MRO \\“,\\” percentage \\“:\\” 0.00 \\“,\\” estimatedvalue \\“: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(请求,开始日期=无,结束日期=无,金额=无):

    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})

更改

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))

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM