Пример #1
0
def watchlist_base_chart_contents_extra_context(view):

    extra_context = dict()

    # Captured values
    kwargs = view.kwargs
    chart_id = kwargs.get('ci')
    watchlist_id = kwargs.get('wi')
    content_type = kwargs.get('ct')
    object_id = kwargs.get('oi')
    last_content_type = kwargs.get('lct')
    last_object_id = kwargs.get('loi')

    # Post data
    data = kwargs.get('POST') or QueryDict()
    selected_years = data.getlist('average_years[]')

    watchlist = Watchlist.objects.get(id=watchlist_id)

    # selected sources
    sources = None

    # filter items & types
    if content_type == 'config':
        items = watchlist.children().filter(product__config__id=object_id)

    elif content_type == 'type':
        if last_content_type == 'abstractproduct':
            items = watchlist.children().filter_by_product(product__id=last_object_id)

    elif content_type == 'abstractproduct':
        items = watchlist.children().filter_by_product(product__id=object_id)

    elif content_type == 'source':
        items = watchlist.children().filter_by_product(product__id=last_object_id)
        sources = Source.objects.filter(id=object_id)

    types = Type.objects.filter_by_watchlist_items(watchlist_items=items)
    if content_type == 'type':
        types = types.filter(id=object_id)

    extra_context['unit_json'] = UnitSerializer(items.get_unit()).data

    # get tran data by chart
    series_options = []

    if chart_id in ['1', '2', '5']:
        for t in types:
            end_date = datetime.date.today() if chart_id == '1' else None
            start_date = end_date + datetime.timedelta(days=-13) if chart_id == '1' else None
            option = get_daily_price_volume(_type=t,
                                            items=items.filter(product__type=t),
                                            sources=sources,
                                            start_date=start_date,
                                            end_date=end_date)
            if not option['no_data']:
                series_options.append(option)

        if chart_id == '5':
            event_form = EventForm()
            extra_context['event_form'] = event_form
            extra_context['event_form_js'] = [event_form.media.absolute_path(js) for js in event_form.media._js[1:]]
            if content_type in ['config', 'abstractproduct']:
                extra_context['event_content_type_id'] = ContentType.objects.get(model=content_type).id
                extra_context['event_object_id'] = object_id
            elif content_type in ['type', 'source']:
                extra_context['event_content_type_id'] = ContentType.objects.get(model=last_content_type).id
                extra_context['event_object_id'] = last_object_id

    if chart_id == '3':
        for t in types:
            option = get_daily_price_by_year(_type=t,
                                             items=items.filter(product__type=t),
                                             sources=sources)
            if not option['no_data']:
                series_options.append(option)

    if chart_id == '4':
        this_year = datetime.datetime.now().year
        selected_years = selected_years or [y for y in range(this_year-5, this_year)]  # default latest 5 years
        selected_years = [int(y) for y in selected_years]  # cast to integer

        extra_context['method'] = view.request.method
        extra_context['selected_years'] = selected_years

        for t in types:
            option = get_monthly_price_distribution(_type=t,
                                                    items=items.filter(product__type=t),
                                                    sources=sources,
                                                    selected_years=selected_years)
            if not option['no_data']:
                series_options.append(option)

    extra_context['series_options'] = series_options
    extra_context['chart'] = Chart.objects.get(id=chart_id)

    return extra_context
Пример #2
0
def product_selector_base_extra_context(view):
    extra_context = dict()

    # Captured values
    params = view.request.GET
    source_ids = params.get('sources').split('_') if params.get('sources') != '_' else []
    extra_context['sources'] = params.get('sources')

    kwargs = view.kwargs
    chart_id = kwargs.get('ci')
    type_id = kwargs.get('type')
    product_ids = kwargs.get('products').split('_') if kwargs.get('products') != '_' else []

    # Post data
    data = kwargs.get('POST') or QueryDict()
    selected_years = data.getlist('average_years[]')

    _type = Type.objects.get(id=type_id)

    product_qs = AbstractProduct.objects.filter(id__in=product_ids)
    products = product_qs.exclude(track_item=False)
    # Handling special cases, if there is parent product e.g. FB1, replaces with sub products
    if product_qs.filter(track_item=False):
        sub_products = reduce(
            operator.or_,
            (product.children().filter(track_item=True) for product in product_qs.filter(track_item=False))
        )
        products = products | sub_products

    if product_qs.first().track_item is False and product_qs.first().config.id == 13:
        products = product_qs

    if source_ids:
        sources = Source.objects.filter(id__in=source_ids)
    else:
        sources = []

    extra_context['unit_json'] = UnitSerializer(products.first().unit).data

    # get tran data by chart
    series_options = []

    if chart_id in ['1', '2']:

        end_date = datetime.date.today() if chart_id == '1' else None
        start_date = end_date + datetime.timedelta(days=-13) if chart_id == '1' else None
        option = get_daily_price_volume(_type=_type,
                                        items=products,
                                        sources=sources,
                                        start_date=start_date,
                                        end_date=end_date)
        if not option['no_data']:
            series_options.append(option)

    if chart_id == '3':

        option = get_daily_price_by_year(_type=_type,
                                         items=products,
                                         sources=sources)
        if not option['no_data']:
            series_options.append(option)

    if chart_id == '4':
        this_year = datetime.datetime.now().year
        selected_years = selected_years or [y for y in range(this_year-5, this_year)]  # default latest 5 years
        selected_years = [int(y) for y in selected_years]  # cast to integer

        extra_context['method'] = view.request.method
        extra_context['selected_years'] = selected_years

        option = get_monthly_price_distribution(_type=_type,
                                                items=products,
                                                sources=sources,
                                                selected_years=selected_years)
        if not option['no_data']:
            series_options.append(option)

    extra_context['series_options'] = series_options
    extra_context['chart'] = Chart.objects.get(id=chart_id)

    return extra_context