Example #1
0
def deal_list_date(request):
    context = get_status(request)
    style = get_style()
    style_ref = get_style_ref(style)

    company_name = request.GET.get('company_name')
    datetime_str = request.GET.get('start_date')
    country = request.GET.get('country')
    start_date_time = datetime.strptime(datetime_str, '%d %m %Y %H:%M:%S')
    categories = request.GET.get('categories')

    if categories:
        category_ids = [int(c_id) for c_id in categories.split(",")]
    else:
        categories = Category.objects.all()
        category_ids = []
        for category in categories:
            if category.name != 'root':
                category_ids.append(category.id)

    if company_name:
        items = DayWatchItem.objects.filter(
            company__name=company_name,
            start_date_time__year=start_date_time.year,
            start_date_time__month=start_date_time.month,
            start_date_time__day=start_date_time.day,
            category__id__in=category_ids
        )
    else:
        items = DayWatchItem.objects.filter(
            start_date_time__year=start_date_time.year,
            start_date_time__month=start_date_time.month,
            start_date_time__day=start_date_time.day,
            category__id__in=category_ids
        )

    context['items'] = items
    context['country'] = country
    context['style_ref'] = style_ref

    return render_to_response(
        'deal_list_date.html',
        context,
        context_instance=RequestContext(request)
    )
Example #2
0
def history_listings_div(request):
    #prepare the params
    context = get_status(request)
    form = HistoryPanelExportForm(user=request.user, data=request.GET)

    if form.is_valid():
        request.session['form_session'] = form.cleaned_data
        period = form.cleaned_data['period']
        style = get_style()
        style_ref = get_style_ref(style)
        user = request.user

        # Convert date parameters
        end_date = datetime.now()
        if period == 'last_30_d':
            start_date = datetime.now() - timedelta(days=30)
        elif period == 'last_15_d':
            start_date = datetime.now() - timedelta(days=15)
        elif period == 'last_7_d':
            start_date = datetime.now() - timedelta(days=7)
        elif period == 'custom':
            d = form.cleaned_data['start_date']
            start_date = datetime(d.year, d.month, d.day)
            d = form.cleaned_data['end_date']
            end_date = datetime(d.year, d.month, d.day, 23, 59)

        country = form.cleaned_data['country']
        context['use_local_currency'] = country in LOCAL_CURRENCY_COUNTRIES
        context['local_currency'] = CURRENCY_DICT[country]

        history_limit = 0
        out_of_range_error = False
        out_of_range_warning = False
        if not user.has_full_access_for_country(country):
            if user.week_history_limit > 0:
                #user is history limited, limit start and end dates
                week_limit = user.week_history_limit
                history_limit = datetime.now() - timedelta(weeks=week_limit)
                if end_date < history_limit:
                    out_of_range_error = True
                elif start_date < history_limit:
                    start_date = history_limit
                    out_of_range_warning = True
                history_limit = history_limit.date()

        # Get deals for this query
        if not out_of_range_error:
            player_ids = form.cleaned_data['players']
            player_ids = [int(p_id) for p_id in player_ids]

            if form.cleaned_data['all_categories']:
                items = DayWatchItem.objects.filter(
                    site__id__in=player_ids,
                    date_time__gte=start_date,
                    date_time__lte=end_date
                )
                categories = Category.objects.all()
                category_ids = []
                for category in categories:
                    if category.name != 'root':
                        category_ids.append(category.id)
            else:
                category_ids = form.cleaned_data['categories']
                category_ids = [int(c_id) for c_id in category_ids]
                items = DayWatchItem.objects.filter(
                    site__id__in=player_ids,
                    category__id__in=category_ids,
                    date_time__gte=start_date,
                    date_time__lte=end_date
                )
        else:
            items = DayWatchItem.objects.none()

        # Prepare and return results to upper layers
        context['items'] = items
        context['country'] = country
        context['style_ref'] = style_ref

        context['history_limit'] = history_limit
        context['out_of_range_error'] = out_of_range_error
        context['out_of_range_warning'] = out_of_range_warning

    # excel button clicked
    if form.data.get('excel'):
        if not request.user.premium_access:
            msg = " Sorry, Excel exports are limited to Premium Users."
            return warningResponse(request, _(msg))

        if not user.is_staff:
            # We limit exportable deals to a month and a half from today
            floor_date = datetime.now() - timedelta(weeks=7)
            context['items'] = context['items'].filter(
                                        start_date_time__gte=floor_date)

            if start_date < floor_date:
                context['floor_date_warn'] = floor_date

        filename = "DayWatch_report_%s" % (
            datetime.now().strftime("%d-%m-%Y_%H-%M"),
        )
        result = render_to_string(
            'includes/history_table_xls.html',
            context,
            context_instance=RequestContext(request)
        )
        response = HttpResponse(
            result,
            content_type='application/vnd.ms-excel;charset=utf-8'
        )
        content_disposition = 'attachment; filename="%s.xls"' % (filename,)
        response['Content-Disposition'] = content_disposition

        return response

    # Normal results rendering
    # col_index_name_map is required for correct sorting behavior
    index_name_map = {
        0: 'offer',
        1: 'company',
        2: 'start_date_time',
        3: 'end_date_time',
        4: 'price',
        5: 'price_usd',
        6: 'discount',
        7: 'category',
        8: 'is_main_deal',
        9: 'sold_count',
        10: 'total_sales_usd',
        11: 'merchant_name',
    }
    if context['use_local_currency']:
        index_name_map[10] = 'total_sales_local'

    json_template = 'includes/history_table_json.txt'

    return get_datatables_records(
        request, context['items'],
        index_name_map, context, json_template
    )