Пример #1
0
def spider_listings_div(request):
    context = get_status(request)
    form = SpidersPanelForm(user=request.user, data=request.GET)

    if not form.is_valid():
        return HttpResponse(json.dumps({'error': 'Invalid form'}),
                            content_type='application/json')

    request.session['form_session'] = form.cleaned_data

    # Convert date parameters
    start_date, end_date = parse_date_range(form)

    country = form.cleaned_data['country']
    site_ids = [int(site) for site in form.cleaned_data['players']]
    sites = [SITE_MODEL.objects.get(id=site_id) for site_id in site_ids]

    data = analyze(sites, start_date, end_date)
    context['sites'] = data
    context['error_categories'] = ERROR_CATEGORIES

    if data:
        return render_to_response('spider_div.html', context)
    else:
        return render_to_response('main/no_results.html', context)
Пример #2
0
def item_div(request):
    context = get_status(request)
    form = ItemsPanelForm(user=request.user, data=request.GET)

    if not form.is_valid():
        return render_to_response('main/form_error.html', context)

    start_date, end_date = parse_date_range(form)
    fields = form.cleaned_data['fields']

    sites = [SITE_MODEL.objects.get(id=site_id) for site_id in
             [int(site_id) for site_id in form.cleaned_data['players']]]
    items = ITEM_MODEL.objects.filter(site__in=sites,
                                      date_time__gte=start_date,
                                      date_time__lte=end_date)

    paginator = Paginator(items, 25)

    page = request.GET.get('page')
    try:
        display_page = paginator.page(page)
    except PageNotAnInteger:
        # Wrong input
        display_page = paginator.page(1)
    except EmptyPage:
        # Out of range
        display_page = paginator.page(paginator.num_pages)

    context['items'] = display_page
    context['fields'] = fields
    return render_to_response('item_div.html', context,
                              context_instance=RequestContext(request))
Пример #3
0
def spider_listings_div(request):
    context = get_status(request)
    form = SpidersPanelForm(user=request.user, data=request.GET)

    if not form.is_valid():
        return HttpResponse(json.dumps({'error': 'Invalid form'}),
                            content_type='application/json')

    request.session['form_session'] = form.cleaned_data

    # Convert date parameters
    start_date, end_date = parse_date_range(form)

    country = form.cleaned_data['country']
    site_ids = [int(site) for site in form.cleaned_data['players']]
    sites = [SITE_MODEL.objects.get(id=site_id) for site_id in site_ids]

    data = analyze(sites, start_date, end_date)
    context['sites'] = data
    context['error_categories'] = ERROR_CATEGORIES

    if data:
        return render_to_response('spider_div.html', context)
    else:
        return render_to_response('main/no_results.html', context)
Пример #4
0
def item_div(request):
    context = get_status(request)
    form = ItemsPanelForm(user=request.user, data=request.GET)

    if not form.is_valid():
        return render_to_response('main/form_error.html', context)

    start_date, end_date = parse_date_range(form)
    fields = form.cleaned_data['fields']

    sites = [
        SITE_MODEL.objects.get(id=site_id) for site_id in
        [int(site_id) for site_id in form.cleaned_data['players']]
    ]
    items = ITEM_MODEL.objects.filter(site__in=sites,
                                      date_time__gte=start_date,
                                      date_time__lte=end_date)

    paginator = Paginator(items, 25)

    page = request.GET.get('page')
    try:
        display_page = paginator.page(page)
    except PageNotAnInteger:
        # Wrong input
        display_page = paginator.page(1)
    except EmptyPage:
        # Out of range
        display_page = paginator.page(paginator.num_pages)

    context['items'] = display_page
    context['fields'] = fields
    return render_to_response('item_div.html',
                              context,
                              context_instance=RequestContext(request))
Пример #5
0
def analytics_div(request):
    # Things the analytics panel provides:
    # Sales:
    # - Sales volume over time
    # - Percentage of companies that sell each category (Pie)
    # - Percentage of categories sold by each company (Pie)
    # - Total sales in the period
    # - Total sales volume
    # - Sales volume share by company
    # - Sales volume share by category
    # - Evolution of the above two over the time period (Stacked area chart)
    # Deals Offered and Coupons sold:
    # - By company, over time
    # - Number of deals sold by a company, per category (Pie)
    # - Number of deals sold of each category, per company (Pie)
    # - Total number of deals sold
    # - Total number of deals sold, by company
    # - Percentage of deals sold by each company
    # - Percentage of deals sold in each category

    context = get_status(request)
    form = PanelForm(user=request.user, data=request.GET)

    if not form.is_valid():
        return render_to_response('main/form_error.html', context)

    request.session['form_session'] = form.cleaned_data
    result_type = form.cleaned_data['result_type']
    country = Country.objects.get(code=form.cleaned_data['country'])
    start_date, end_date = parse_date_range(form)
    sites = [
        SITE_MODEL.objects.get(id=site_id) for site_id in
        [int(site_id) for site_id in form.cleaned_data['players']]
    ]
    items = ITEM_MODEL.objects.filter(site__in=sites,
                                      date_time__gte=start_date,
                                      date_time__lte=end_date)

    context['result_type'] = result_type
    context['result_type_name'] = [
        choice[1] for choice in RESULT_TYPE_CHOICES if choice[0] == result_type
    ][0]

    if result_type == 'sales':
        context['analysis'] = analytics.SalesAnalysis(items)
    elif result_type == 'offered':
        context['analysis'] = analytics.OfferedAnalysis(items)
    elif result_type == 'sold':
        context['analysis'] = analytics.SoldAnalysis(items)

    return render_to_response('analytics_div.html', context)
Пример #6
0
def analytics_div(request):
    # Things the analytics panel provides:
    # Sales:
    # - Sales volume over time
    # - Percentage of companies that sell each category (Pie)
    # - Percentage of categories sold by each company (Pie)
    # - Total sales in the period
    # - Total sales volume
    # - Sales volume share by company
    # - Sales volume share by category
    # - Evolution of the above two over the time period (Stacked area chart)
    # Deals Offered and Coupons sold:
    # - By company, over time
    # - Number of deals sold by a company, per category (Pie)
    # - Number of deals sold of each category, per company (Pie)
    # - Total number of deals sold
    # - Total number of deals sold, by company
    # - Percentage of deals sold by each company
    # - Percentage of deals sold in each category

    context = get_status(request)
    form = PanelForm(user=request.user, data=request.GET)

    if not form.is_valid():
        return render_to_response('main/form_error.html', context)

    request.session['form_session'] = form.cleaned_data
    result_type = form.cleaned_data['result_type']
    country = Country.objects.get(code=form.cleaned_data['country'])
    start_date, end_date = parse_date_range(form)
    sites = [SITE_MODEL.objects.get(id=site_id) for site_id in
             [int(site_id) for site_id in form.cleaned_data['players']]]
    items = ITEM_MODEL.objects.filter(site__in=sites,
                                      date_time__gte=start_date,
                                      date_time__lte=end_date)

    context['result_type'] = result_type
    context['result_type_name'] = [choice[1] for choice in
                                   RESULT_TYPE_CHOICES
                                   if choice[0] == result_type][0]

    if result_type == 'sales':
        context['analysis'] = analytics.SalesAnalysis(items)
    elif result_type == 'offered':
        context['analysis'] = analytics.OfferedAnalysis(items)
    elif result_type == 'sold':
        context['analysis'] = analytics.SoldAnalysis(items)

    return render_to_response('analytics_div.html', context)
Пример #7
0
def trends_div(request):
    context = get_status(request)
    form = TrendsPanelForm(user=request.user, data=request.GET)

    if not form.is_valid():
        return render_to_response('main/form_error.html', context)

    start_date, end_date = parse_date_range(form)
    sites = [
        SITE_MODEL.objects.get(id=site_id) for site_id in
        [int(site_id) for site_id in form.cleaned_data['players']]
    ]
    fields = form.cleaned_data['fields']
    metric = form.cleaned_data['metric']

    def get_items(start_date, end_date):
        return ITEM_MODEL.objects.filter(site__in=sites,
                                         date_time__gte=start_date,
                                         date_time__lte=end_date)

    def analyze_items(items, field):
        if metric == SUM:
            return items_sum(items, field)
        elif metric == AVERAGE:
            return items_average(items, field)

    evolution = {}
    for field in fields:
        # Find how the field has evolved over time
        # Step over every day between the start and end date
        delta = (end_date - start_date)
        metric_evolution = []
        for i in range(delta.days + 1):
            start_day = (start_date + timedelta(days=i))
            end_day = start_day + timedelta(days=1)
            items = get_items(start_day, end_day)
            analysis = analyze_items(items, field)
            if analysis:
                metric_evolution += [(start_day.strftime('%s'), analysis)]
        evolution[field] = metric_evolution

    context['evolution'] = evolution
    return render_to_response('trends_div.html',
                              context,
                              context_instance=RequestContext(request))
Пример #8
0
def trends_div(request):
    context = get_status(request)
    form = TrendsPanelForm(user=request.user, data=request.GET)

    if not form.is_valid():
        return render_to_response('main/form_error.html', context)

    start_date, end_date = parse_date_range(form)
    sites = [SITE_MODEL.objects.get(id=site_id) for site_id in
             [int(site_id) for site_id in form.cleaned_data['players']]]
    fields = form.cleaned_data['fields']
    metric = form.cleaned_data['metric']

    def get_items(start_date, end_date):
        return ITEM_MODEL.objects.filter(site__in=sites,
                                         date_time__gte=start_date,
                                         date_time__lte=end_date)

    def analyze_items(items, field):
        if metric == SUM:
            return items_sum(items, field)
        elif metric == AVERAGE:
            return items_average(items, field)

    evolution = {}
    for field in fields:
        # Find how the field has evolved over time
        # Step over every day between the start and end date
        delta = (end_date - start_date)
        metric_evolution = []
        for i in range(delta.days + 1):
            start_day = (start_date + timedelta(days=i))
            end_day = start_day + timedelta(days=1)
            items = get_items(start_day, end_day)
            analysis = analyze_items(items, field)
            if analysis:
                metric_evolution += [(start_day.strftime('%s'), analysis)]
        evolution[field] = metric_evolution

    context['evolution'] = evolution
    return render_to_response('trends_div.html', context,
                              context_instance=RequestContext(request))