Beispiel #1
0
def best(req):
    logger.debug('Requesting best');
    page = to_int(req.GET.get('page'), 1)

    countPerPage = 30
    products, count = Product.get_bids(page, countPerPage)
    context = {
        'menu': JSArray(get_menu()),
        'category': 'Лучшие предложения',
        'count': count,
        'products': js.eval(products),
        'paginator': {
            'totalPages': math.ceil(float(count) / countPerPage) or 1,
            'currentPage': page,
            'url': req.path,
            'config': {
                'HTTP': {
                    'list': '#'
                }
            }
        }
    }

    html = js.render(context, 'pages.category', env=get_env())
    res = HttpResponse(html);
    return res
Beispiel #2
0
def main(req):
    logger.debug("Requesting main")

    products, count = Product.get_bids(1, 5)
    promo = Banner.objects.filter(hidden=False)
    if req.is_ajax():
        return HttpResponse(
            json.dumps(
                {"promo": map(BannerSerializer().normalize, promo), "products": json.loads(products), "count": count},
                cls=JSONEncoder,
            )
        )

    ctime = time()

    context = {
        "menu": get_menu(),
        "promo": map(BannerSerializer().normalize, promo),
        "products": json.loads(products),
        "count": count,
    }
    context = json.dumps(context, cls=JSONEncoder)
    logger.info("Generating context: %gs" % (time() - ctime))

    ctime = time()
    html = js.render(context, 'pages["index.str"]', env=get_env())
    logger.info("Render: %gs" % (time() - ctime))

    res = HttpResponse(html)
    return res
Beispiel #3
0
def product(req, slug):
    try:
        product = Product.objects.prefetch_related('item_set').get(slug__exact=slug)
    except Product.DoesNotExist:
        raise Http404

    items = product.item_set.all()
    if not len(items):
        raise Http404

    #TODO: merge
    if (req.is_ajax()):
        context = {
            'title': generate_title(product),
            'description': generate_description(product),

            #breadcrumbs
            'name': product.name,
            'category': product.type.name,
            'categoryUrl': "/%s" % product.type.url,

            'item': json.loads(product.json())
        }
        return HttpResponse(json.dumps(context), 'application/json')

    context = {
        'title': generate_title(product),
        'description': generate_description(product),

        #breadcrumbs
        'name': product.name,
        'category': product.type.name,
        'categoryUrl': "/%s" % product.type.url,

        'menu': JSArray(get_menu()),
        'item': product.json(),
    }

    html = js.render(context, 'pages["item.json"]', env=get_env())
    return HttpResponse(html)
Beispiel #4
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