def category_view(request, slug, parent_slugs='', template='product/category.html'): """Display the category, its child categories, and its products. Parameters: - slug: slug of category - parent_slugs: ignored """ try: category = Category.objects.get(slug=slug) products = list(category.active_products()) sale = find_best_auto_discount(products) except Category.DoesNotExist: return bad_or_missing(request, _('The category you have requested does not exist.')) child_categories = category.get_all_children() ctx = { 'category': category, 'child_categories': child_categories, 'sale' : sale, 'products' : products, } index_prerender.send(Product, request=request, context=ctx, category=category, object_list=products) return render_to_response(template, RequestContext(request, ctx))
def category_view(request, slug, parent_slugs='', template='product/category.html'): """Display the category, its child categories, and its products. Parameters: - slug: slug of category - parent_slugs: ignored """ try: category = Category.objects.get_by_site(slug=slug) products = list(category.active_products()) sale = find_best_auto_discount(products) except Category.DoesNotExist: return bad_or_missing( request, _('The category you have requested does not exist.')) child_categories = category.get_all_children() ctx = { 'category': category, 'child_categories': child_categories, 'sale': sale, 'products': products, } index_prerender.send(Product, request=request, context=ctx, category=category, object_list=products) return render(request, template, ctx)
def category_view_kamel(request, slug, parent_slugs='', template='product/category.html'): """Display the category, its child categories, and its products. Parameters: - slug: slug of category - parent_slugs: ignored """ if request.method == "GET": currpage = request.GET.get('page', 1) town = request.GET.get('town') else: currpage = 1 town = 'Gdansk' is_paged = False page = None try: category = Category.objects.get_by_site(slug=slug) products = list(category.active_products()) sale = find_best_auto_discount(products) count = config_value('PRODUCT', 'NUM_PAGINATED') paginator = Paginator(products, count) try: paginator.validate_number(currpage) except EmptyPage: return bad_or_missing(request, _("Invalid page number")) is_paged = paginator.num_pages > 1 page = paginator.page(currpage) except Category.DoesNotExist: return bad_or_missing( request, _('The category you have requested does not exist.')) child_categories = category.get_all_children() ctx = { 'category': category, 'child_categories': child_categories, 'sale': sale, 'town': town, 'products': page.object_list, 'is_paginated': is_paged, 'page_obj': page, 'paginator': paginator } index_prerender.send(Product, request=request, context=ctx, category=category, object_list=products) return render_to_response(template, context_instance=RequestContext(request, ctx))
def get_context_data(self, **kwargs): context = super(CategoryView, self).get_context_data(**kwargs) context['category'] = self.category context['child_categories'] = self.category.get_all_children() context['sale'] = find_best_auto_discount(self.products) context['parent_slugs'] = self.kwargs['parent_slugs'] index_prerender.send(Product, request=self.request, context=context, category=self.category, object_list=self.products) return context
def get_context_data(self, **kwargs): context = super(CategoryView, self).get_context_data(**kwargs) products = list(self.object.active_products()) context['child_categories'] = self.object.get_all_children() context['sale'] = find_best_auto_discount(products) context['products'] = products index_prerender.send(Product, request=self.request, context=context, category=self.object, object_list=products) return context
def category_view(request, slug, parent_slugs='', template='product/category.html'): """Display the category, its child categories, and its products. Parameters: - slug: slug of category - parent_slugs: ignored """ if request.method == "GET": currpage = request.GET.get('page', 1) else: currpage = 1 try: category = Category.objects.get_by_site(slug=slug) products = list(category.active_products()) sale = find_best_auto_discount(products) except Category.DoesNotExist: return bad_or_missing(request, _('The category you have requested does not exist.')) child_categories = category.get_all_children() count = config_value('PRODUCT','NUM_PAGINATED') paginator = Paginator(products, count) try: paginator.validate_number(currpage) except InvalidPage: return bad_or_missing(request, _("Invalid page number")) page = paginator.page(currpage) min_in_page = (count * (page.number - 1) + 1) max_in_page = min_in_page + (len(page.object_list) - 1) ctx = { 'category': category, 'child_categories': child_categories, 'sale': sale, 'products': page.object_list, 'page_obj': page, 'paginator': paginator, 'min_in_page': min_in_page, 'max_in_page': max_in_page } index_prerender.send(Product, request=request, context=ctx, category=category, object_list=products) return render_to_response(template, context_instance=RequestContext(request, ctx))
def category_view(request, slug, parent_slugs='', template='product/category.html'): """Display the category, its child categories, and its products. Parameters: - slug: slug of category - parent_slugs: ignored """ if request.GET.has_key('order_by') and request.GET['order_by']: orderBy= request.GET['order_by'] if orderBy == 'price_inc': try: category = Category.objects.get_by_site(slug=slug) #products = list(category.active_products()) products = list(category.active_products()) if len(products): productsAndPrice = [] for p in products: price = p.unit_price productsAndPrice.append([price,p]) productsAndPrice.sort() sortedProducts = [] for pp in productsAndPrice: sortedProducts.append(pp[1]) products = sortedProducts # sale = find_best_auto_discount(products) except Category.DoesNotExist: return bad_or_missing(request, _('The category you have requested does not exist.')) child_categories = category.get_all_children() cart = Cart.objects.from_request(request) if cart.numItems > 0: productsInCart = [item.product for item in cart.cartitem_set.all()] sale = find_best_auto_discount(productsInCart) else: sale = None ctx = { 'category': category, 'child_categories': child_categories, 'sale' : sale, 'products' : products, 'order_by' : 'price_inc' } index_prerender.send(Product, request=request, context=ctx, category=category, object_list=products) return render_to_response(template, context_instance=RequestContext(request, ctx)) else: try: category = Category.objects.get_by_site(slug=slug) #products = list(category.active_products()) products = list(category.active_products()) if len(products): productsAndPrice = [] for p in products: price = -p.unit_price productsAndPrice.append([price,p]) productsAndPrice.sort() sortedProducts = [] for pp in productsAndPrice: sortedProducts.append(pp[1]) products = sortedProducts # sale = find_best_auto_discount(products) except Category.DoesNotExist: return bad_or_missing(request, _('The category you have requested does not exist.')) child_categories = category.get_all_children() cart = Cart.objects.from_request(request) if cart.numItems > 0: productsInCart = [item.product for item in cart.cartitem_set.all()] sale = find_best_auto_discount(productsInCart) else: sale = None ctx = { 'category': category, 'child_categories': child_categories, 'sale' : sale, 'products' : products, 'order_by' : 'price_dec' } index_prerender.send(Product, request=request, context=ctx, category=category, object_list=products) return render_to_response(template, context_instance=RequestContext(request, ctx)) else: try: category = Category.objects.get_by_site(slug=slug) #products = list(category.active_products()) products = list(category.active_products()) # productsAndPrice = [] # for p in products: # price = p.unit_price # productsAndPrice.append([price,p]) # productsAndPrice.sort() # sortedProducts = [] # for pp in productsAndPrice: # sortedProducts.append(pp[1]) # products = sortedProducts # sale = find_best_auto_discount(products) except Category.DoesNotExist: return bad_or_missing(request, _('The category you have requested does not exist.')) child_categories = category.get_all_children() cart = Cart.objects.from_request(request) if cart.numItems > 0: productsInCart = [item.product for item in cart.cartitem_set.all()] sale = find_best_auto_discount(productsInCart) else: sale = None ctx = { 'category': category, 'child_categories': child_categories, 'sale' : sale, 'products' : products, } index_prerender.send(Product, request=request, context=ctx, category=category, object_list=products) return render_to_response(template, context_instance=RequestContext(request, ctx))