Beispiel #1
0
def url_save(request):
    '''
    柱图线图的条件过长,先用POST存起来,再用GET方法给swfobject
    '''
    if request.method=='POST':
        url_get = request.POST.get('url')
        url_get = url_get.encode("utf-8")
        url_get = urllib.unquote(url_get)
        url_get = urllib.unquote(url_get)
        url_submit = dict(eval("{%s}"%url_get.replace("=",":")).items())
        view_id = url_submit["view_id"]
        user_id = request.user.id
        u_d = get_user_dimension(user_id,view_id)
        if country_session(u_d):tips="false"
        elif url_submit.has_key("provname") and len(url_submit["provname"].split(",")) > 10:tips="true"
        elif url_submit.has_key("cityname") and len(url_submit["cityname"].split(",")) > 10:tips="true"
        else:tips="false"
        if request.POST.has_key("ind"):
            indicator = request.POST.get('ind').encode("utf-8")
            p = Flashurl(url = "{%s,'indicator':'%s'}"%(url_get.replace("=",":"),indicator))
        else:
            p = Flashurl(url = "{%s}"%url_get.replace("=",":"))
        p.save()
        time.sleep(2)
        result = "?tid=%s|%s"%(p.id,tips)
    return HttpResponse(result)
Beispiel #2
0
def show_table(request):
    """
    execute sql and fetch results.
    """
    user_id = request.user.id
    data = request.POST.copy()
    data.pop('timestamp')
    provlist = len(data['provname'].split(",")) if data.has_key('provname') else 0
    try:
        view_id = data.get('view_id')
        data.pop('view_id')
        v = View.objects.get(id=view_id)
    except:
        raise Http404
    # get container div, sent it back to client and put table in that container.
    try:
        page = data['current_page']
        data.pop('current_page')
        page = int(page)
    except:
        page = 1
    container_id = data.get('container')
    data.pop('container')
    view_obj = ViewObj(v, request)
    view_id = view_obj.obj['view_id']
    t = loader.get_template('results.html')
    try:
        v_query = view_obj.get_query()
        v_query = query_session(v_query)
        u_d = get_user_dimension(user_id,view_id)
        #  !!! 生成sql语句
        sql_sum =  SQLGenerator(data, view_obj, u_d,request).get_sum().encode('utf-8')  #求和
        object_sql = SQLGenerator(data, view_obj, u_d,request)
        sql = object_sql.get_sql().encode('utf-8') #查询内容的sql语句
        sql_sum_column = object_sql.sum_column  #求和的字段
        #分页:例 第1页显示 0,30--limit 0,30   第2页显示 30,30 -- limit 30,30
        #所以公式为  (页数-1)*每页显示 , 页数*每页显示
        sql_limit = "%s limit %s,%s"%(sql,(page-1)*MAX_DATA,MAX_DATA+1) #+1多取1条判断是否有下页
        # !!! 双线程执行sql语句,t_sum求和  t_sql取前50条
        t_sum = Mythread(args=(sql_sum,))
        t_sql = Mythread(args=(sql_limit,))
        t_sum.start()
        t_sql.start()
        t_sum.join()
        t_sql.join()
        try:
            sql_column_sum =  t_sum.v
            sum_data = dict(zip(sql_sum_column,sql_column_sum[0])) 
        except:       #某些报表没有指标! 只有维度,跑sql语句会报错的! 擦!
            sum_data = None
        res = t_sql.v
        #分页
        contacts = MyPaginator(len(res), page, MAX_DATA)
        res = res[:MAX_DATA]
        u_dimension = u_d.split(",") if len(u_d)>0 else []
        res = format_table(res, view_obj,u_dimension,sum_data)
        head,res = (res[0],res[1:]) if res else ("",[])
        tips,u_session="",True
        if country_session(u_d) and provlist<HIGHEST_AUTHORITY and v_query:
            tips = "如果要看分省/市数据,请在维度设置中勾选省/市<p>如果查看全国数据,请将省条件全选"
            u_session = False
        
        #log
        try:
            log_id = "%s%05d" % (datetime.datetime.now().strftime("%Y%m%d%H%M%S"),random.randint(0,10000))
            sql_limit_log = "%s|Query_time: %s, sql: %s"%("show_table",
                                                          t_sql.time,
                                                          sql_limit.encode("utf-8"))
            tolog(request,sql_limit_log,log_id)   
            sql_sum_log = "%s|Query_time: %s, sql: %s"%("show_table",
                                                             t_sum.time,
                                                             sql_sum.encode("utf-8"))
            tolog(request,sql_sum_log,log_id)   
        except:
            pass
        html = t.render(Context({'view_id': view_id,
                                'res': res,
                                'contacts': contacts,
                                'u_session': u_session,
                                'tips': tips,
                                'head': head,
                                'ud': u_dimension,
                                'container_id': container_id,
                                'headers': view_obj.get_headers(),
                                'table_name': view_obj.get_body()['dataset'].cname,
                                }))
        json_text = simplejson.dumps({'container':container_id,'content':html})
        return HttpResponse(json_text)
    except:
        json_text = showtable_500(t,container_id,view_obj)
        return HttpResponse(json_text)