def best_selling(self, request): best_selling_products = get_best_selling_product_info( shop_ids=[request.GET.get("shop", Shop.objects.first().pk)]) combined_variation_products = defaultdict(int) for product_id, parent_id, qty in best_selling_products: if parent_id: combined_variation_products[parent_id] += qty else: combined_variation_products[product_id] += qty # take here the top `limit` records, because the filter_queryset below can mess with our work product_ids = [ d[0] for d in sorted(six.iteritems(combined_variation_products), key=lambda i: i[1], reverse=True) ] shop_products_qs = ShopProduct.objects.filter( product__id__in=product_ids) shop_products_qs = self.filter_queryset(shop_products_qs).distinct() page = self.paginate_queryset(shop_products_qs) if page is not None: serializer = self.get_serializer_class()( page, many=True, context=self.get_serializer_context()) return self.get_paginated_response(serializer.data) serializer = self.get_serializer_class()( shop_products_qs, many=True, context=self.get_serializer_context()) return Response(serializer.data)
def best_selling(self, request): data = get_best_selling_product_info( shop_ids=[request.GET.get("shop", request.shop.pk)], cutoff_days=30, orderable_only=True, ) sorted_product_ids = sorted(data, key=lambda item: item[1], reverse=True) product_ids = [item[0] for item in sorted_product_ids] catalog = ProductCatalog( ProductCatalogContext( shop=request.GET.get("shop", request.shop.pk), user=getattr(request, "user", None), contact=getattr(request, "customer", None), purchasable_only=True, )) valid_shop_products_qs = catalog.get_shop_products_queryset().filter( product__in=product_ids, product__mode__in=ProductMode.get_parent_modes()) shop_products_qs = self.filter_queryset( valid_shop_products_qs).distinct() page = self.paginate_queryset(shop_products_qs) if page is not None: serializer = self.get_serializer_class()( page, many=True, context=self.get_serializer_context()) return self.get_paginated_response(serializer.data) serializer = self.get_serializer_class()( shop_products_qs, many=True, context=self.get_serializer_context()) return Response(serializer.data)
def best_selling(self, request): """ Returns the top 20 (default) best selling products for a given shop. To change the number of products, set the `limit` query param. """ limit = int(parse_decimal_string(request.GET.get("limit", 20))) best_selling_products = get_best_selling_product_info( shop_ids=[request.GET.get("shop", Shop.objects.first().pk)]) combined_variation_products = defaultdict(int) for product_id, parent_id, qty in best_selling_products: if parent_id: combined_variation_products[parent_id] += qty else: combined_variation_products[product_id] += qty # take here the top `limit` records, because the filter_queryset below can mess with our work product_ids = [ d[0] for d in sorted(six.iteritems(combined_variation_products), key=lambda i: i[1], reverse=True)[:limit] ] shop_products_qs = ShopProduct.objects.filter( product__id__in=product_ids) shop_products_qs = self.filter_queryset(shop_products_qs).distinct() serializer = self.get_serializer_class()( shop_products_qs, many=True, context=self.get_serializer_context()) return Response(serializer.data)
def best_selling(self, request): best_selling_products = get_best_selling_product_info( shop_ids=[request.GET.get("shop", Shop.objects.first().pk)]) combined_variation_products = defaultdict(int) for product_id, parent_id, qty in best_selling_products: if parent_id: combined_variation_products[parent_id] += qty else: combined_variation_products[product_id] += qty # take here the top `limit` records, because the filter_queryset below can mess with our work product_ids = [ d[0] for d in sorted(six.iteritems(combined_variation_products), key=lambda i: i[1], reverse=True) ] shop_products_qs = ShopProduct.objects.filter(product__id__in=product_ids) shop_products_qs = self.filter_queryset(shop_products_qs).distinct() page = self.paginate_queryset(shop_products_qs) if page is not None: serializer = self.get_serializer_class()(page, many=True, context=self.get_serializer_context()) return self.get_paginated_response(serializer.data) serializer = self.get_serializer_class()(shop_products_qs, many=True, context=self.get_serializer_context()) return Response(serializer.data)