Esempio n. 1
0
def sort(req, category):
    logger.debug('Request sorting %s' % req.path)
    if not req.is_ajax():
        raise Http404

    page_number = to_int(req.GET.get('page'), 1)
    sort_key = to_string(req.GET.get('sort'), None)

    filters = {
        'gems': GET_int(req, 'gem'),
        'shops': GET_int(req, 'store'),
        'materials': GET_int(req, 'material'),
    }

    products = Product.customs.filter(type__url__exact=category)
    products = products.filter_by_gems(filters['gems'])\
                       .filter_by_shops(filters['shops'])\
                       .filter_by_materials(filters['materials'])\
                       .sort(sort_key)

    paginator = Paginator(products, 30)
    try:
        page = paginator.page(page_number)
    except:
        page = paginator.page(1)
    serializer = CustomsProductSerializer()

    products = json.dumps({
        'list': map(lambda x: serializer.normalize(x), page.object_list),
        'count': paginator.count,
    })
    return HttpResponse(products, 'application/json')
Esempio n. 2
0
def category(req, category):
    logger.debug('Requestings category %s' % req.path);
    cat = Type.objects.get(url=category)
    page_number = to_int(req.GET.get('page'), 1)
    sort_key = to_string(req.GET.get('sort'), None)

    filters = {
        'gems': GET_int(req, 'gem'),
        'shops': GET_int(req, 'store'),
        'materials': GET_int(req, 'material'),
    }

    products = Product.customs.filter(type__url__exact=category)
    products = products.filter_by_gems(filters['gems'])\
                       .filter_by_shops(filters['shops'])\
                       .filter_by_materials(filters['materials'])\
                       .sort(sort_key)

    paginator = Paginator(products, 30)
    try:
        page = paginator.page(page_number)
    except EmptyPage:
        page = paginator.page(1)
    serializer = CustomsProductSerializer()

    context = {
        'menu': get_with_active_menu(category),
        'category': cat.name,
        'count': paginator.count,
        'products': map(lambda p: serializer.normalize(p), page.object_list),
        'sortParams': [{
            'name': 'По алфавиту',
            'value': 'name'
        }, {
            'name': 'Сначала дорогие',
            'value': 'tprice'
        }, {
            'name': 'Сначала дешёвые',
            'value': 'price'
        }],
        'paginator': {
            'totalPages': paginator.num_pages,
            'currentPage': page.number,
            'url': req.path,
            'config': {
                'HTTP': {
                    'list': req.path + '/json'
                }
            }
        },
        "filters": get_filters(cat),
    }

    if req.is_ajax():
        return HttpResponse(json.dumps(context), content_type="application/json")

    c = time()
    html = js.render(json.dumps(context), 'pages["category.json"]', env=get_env())
    logger.info('Rendered %fs' % (time() - c))

    res = HttpResponse(html)
    return res