def report_view(request,tp_id = None):
    tp_id = tp_id_proc(request,tp_id)
    tp_obj = TradePlanModel.objects.get(pk = tp_id)

    selected = get_selected_symbols(tp_obj.tradeframe,tp_obj.diff_s_overview)
    mdi_obj = MarketDetailInfo.objects

    s_queryset = mdi_obj.filter(market_overview = tp_obj.diff_s_overview,
                                timeframe = trade_frame_map(tp_obj.tradeframe)[0]).order_by('-symbol_name')
    mov_s_res  = tp_obj.diff_s_overview.market_result
    b_queryset = mdi_obj.filter(market_overview = tp_obj.diff_s_overview,
                                timeframe = trade_frame_map(tp_obj.tradeframe)[1]).order_by('-symbol_name')
    mov_b_res  = tp_obj.diff_b_overview.market_result

    mdi_query_set =  MarketDetailInfo.objects.filter(market_overview = tp_obj.diff_s_overview,
                                                     timeframe = tp_obj.tradeframe).order_by('-symbol_name')

    movd_formset   = MovDetailInlineFormset(instance = tp_obj.market_overview)
    mov_form       = MarketOverViewForm(instance = tp_obj.market_overview)


    if selected is not None:
        selected_view = SelectedFormset(queryset = MarketDetailInfo.objects.filter(
                Q(exclude_reason = 'N'  ,market_overview = tp_obj.diff_s_overview) |
                Q(exclude_reason__isnull = True,market_overview = tp_obj.diff_s_overview),
                symbol_name__in=selected).order_by('symbol_name','-timeframe'))
        sym_count = selected_view.total_form_count()/len(trade_frame_map(tp_obj.tradeframe))
        selected = 1
    else:
        selected = 0
        selected_view = 0
        sym_count = 0

    first_select_view = FirstSelectFormset( queryset = mdi_query_set )

    return render_to_response("tradesys/ReportView.html", {
            "tradeframe_dict" : dict(TRADEFRAME),
            "tradetype_dict"  : dict(TRADETYPE),
            "timeframe_dict"  : dict(TIMEFRAME),
            "sub_dir"         : dict(SUB_DIR),
            "obj_dir"         : dict(OBJ_DIR),
            "normative"       : dict(NORMATIVE),
            "exreason"        : dict(EXREASON),
            "tp_obj"          : tp_obj,
            "movd_formset"    : movd_formset,
            "mov_form"        : mov_form,
            "b_diffview_set"  : b_queryset,
            "s_diffview_set"  : s_queryset,
            "mov_b_res"       : mov_b_res,
            "mov_s_res"       : mov_s_res,
            "tradetype"       : tp_obj.tradetype,
            "first_select_view" : first_select_view,
            "selected"        : selected,
            "timeframes"      : trade_frame_map(tp_obj.tradeframe),
            "sym_count"       : sym_count,
            "tf_count"        : len(trade_frame_map(tp_obj.tradeframe)),
            "selected_view"   : selected_view,
            "image_base_url" : settings.IMAGE_BASE_URL,
            },context_instance=RequestContext(request))
def analysis_selected_view(request, tp_id = None):

    tp_id = tp_id_proc(request,tp_id)
    tp_obj  = TradePlanModel.objects.get(pk = tp_id)
    selected = get_selected_symbols(tp_obj.tradeframe,tp_obj.diff_s_overview)
    # 需要写入 不做交易选项. ting

    if selected is None:
        tp_obj.end_time = timezone.now()
        tp_obj.plan_result = 'N';
        tp_obj.save()
        return redirect('/tradesys/MyTradePlan/report_view/%d/' % tp_obj.id )

    if request.method == "POST":
        selected_view = SelectedFormset( request.POST,
            queryset = MarketDetailInfo.objects.filter(
                Q(exclude_reason = 'N'  ,market_overview = tp_obj.diff_s_overview) |
                Q(exclude_reason__isnull = True,market_overview = tp_obj.diff_s_overview),
                symbol_name__in=selected).order_by('symbol_name','-timeframe'))
        if selected_view.is_valid():
            selected_view.save()

        if request.POST.has_key('planresult'):
            return redirect('/tradesys/MyTradePlan/report_view/%d/' % tp_obj.id)

        tradeplan_action_init(tp_obj)

        return redirect('/tradesys/MyTradePlan/tradeplan_action_view')
    else:
        selected_view = SelectedFormset(queryset = MarketDetailInfo.objects.filter(
                Q(exclude_reason = 'N'  ,market_overview = tp_obj.diff_s_overview) |
                Q(exclude_reason__isnull = True,market_overview = tp_obj.diff_s_overview),
                symbol_name__in=selected).order_by('symbol_name','-timeframe'))

    return render_to_response("tradesys/AnalysisSelectedView.html", {
            "tradeframe_dict" : dict(TRADEFRAME),
            "tradetype_dict"  : dict(TRADETYPE),
            "timeframe_dict"  : dict(TIMEFRAME),
            "sym_count"       : selected_view.total_form_count()/len(trade_frame_map(tp_obj.tradeframe)),
            "tf_count"        : len(trade_frame_map(tp_obj.tradeframe)),
            "tradetype"       : tp_obj.tradetype,
            "timeframes"      : trade_frame_map(tp_obj.tradeframe),
            "selected_view"   : selected_view,
            "image_base_url"  : settings.IMAGE_BASE_URL,
            },context_instance=RequestContext(request))