def product(request, id): product = get_object_or_404(Product, pk=id, display=1) breadcrumbs = _breadcrumbs(product.category_id, []) categories_list = [] for category in breadcrumbs: categories_list.append(category['id']) categories_list.reverse() sidemenu = _build_sidemenu(categories_list) if product: if product.new_price: product.price = _calculate_percent(product.price, product.new_price) if product.new_title: product.title = product.new_title if product.new_description: product.description = product.new_description if product.new_features: product.features = product.new_features if product.brand_id is not None and product.brand_id > 0: brand = Brand.objects.get(pk=product.brand_id) product.brand_title = brand.new_title or brand.title product.url = brand.new_url or brand.url else: product.brand_title = '' product.url = '' image = _get_img_path(product) product.image = image[0] product.height = image[1] product.width = image[2] return render_to_response('main/product.html', { 'product' : product, 'sidemenu' : sidemenu, 'breadcrumbs' : breadcrumbs, }, context_instance=RequestContext(request))
def index(request): products = Product.objects.filter(display=1, bestseller=1, status__in=[1,3,4,]).order_by('ord') for product in products: if product.new_title: product.title = product.new_title if product.new_price: product.price = _calculate_percent(product.price, product.new_price) image = _get_img_path(product) product.image = image[0] product.height = image[1] product.width = image[2] return render_to_response('main/index.html', { 'products' : products, }, context_instance=RequestContext(request))
def category(request, id, page = 1): breadcrumbs = _breadcrumbs(id, []) categories_list_with_products = _get_categories_id(id, []) categories_list = [] for category in breadcrumbs: categories_list.append(category['id']) categories_list.reverse() sidemenu = _build_sidemenu(categories_list) products_query = Product.objects.filter(display=1, status__in=[1,3,4,], category__in=categories_list_with_products).order_by('-price') paginator = Paginator(products_query, config.MAX_PRODUCTS) last = paginator.num_pages first = 1 try: page = int(page) except ValueError: page = 1 if page > last: page = last try: products = paginator.page(page) except (InvalidPage, EmptyPage): products = paginator.page(paginator.num_pages) if (page + 3) < last: range_last = page + 3 else: range_last = last last = False if page - 3 > first: range_first = page - 3 else: range_first = first first = False paginator_range = range(range_first, range_last + 1) for product in products.object_list: if product.new_price: product.price = _calculate_percent(product.price, product.new_price) image = _get_img_path(product, 'preview') product.image = image[0] product.height = image[1] product.width = image[2] return render_to_response('main/category.html', { 'breadcrumbs' : breadcrumbs, 'sidemenu' : sidemenu, 'products' : products, 'id' : id, 'range' : paginator_range, 'first' : first, 'last' : last, }, context_instance=RequestContext(request))
def search(request, page = 1): if request.method != 'GET': return redirect('index') value = request.GET.get('search', '').strip() if value: try: value_int=int(value) except ValueError: value_int=-1 products_query = Product.objects.filter( Q(display=1), Q(status__in=[1,3,4,]), Q(description__icontains=value) | Q(id=value_int) ).order_by('ord') paginator = Paginator(products_query, config.MAX_PRODUCTS) last = paginator.num_pages first = 1 try: page = int(page) except ValueError: page = 1 if page > last: page = last try: products = paginator.page(page) except (InvalidPage, EmptyPage): products = paginator.page(paginator.num_pages) if (page + 3) < last: range_last = page + 3 else: range_last = last last = False if page - 3 > first: range_first = page - 3 else: range_first = first first = False paginator_range = range(range_first, range_last + 1) for product in products.object_list: if product.new_price: product.price = _calculate_percent(product.price, product.new_price) image = _get_img_path(product, 'preview') product.image = image[0] product.height = image[1] product.width = image[2] return render_to_response('main/search.html', { 'products' : products, 'search_value' : value, 'range' : paginator_range, 'first' : first, 'last' : last, }, context_instance=RequestContext(request))