def get_search_results(self, request, query): shop = request.shop minimum_query_length = 3 skus_seen = set() if len(query) >= minimum_query_length: pk_counter = Counter() pk_counter.update( Product.objects.filter(sku__startswith=query).values_list( "pk", flat=True)) name_q = Q() for part in split_query(query, minimum_query_length): name_q &= Q(name__icontains=part) pk_counter.update( Product._parler_meta.root_model.objects.filter( name_q).values_list("master_id", flat=True)) pks = [pk for (pk, count) in pk_counter.most_common(10)] for product in Product.objects.filter( pk__in=pks, shop_products__shop_id=shop.id): relevance = 100 - pk_counter.get(product.pk, 0) skus_seen.add(product.sku.lower()) yield SearchResult( text=force_text(product), url=get_model_url(product, shop=request.shop), category=_("Products"), relevance=relevance, ) if len(query) >= minimum_query_length: url = reverse("shuup_admin:shop_product.new") if " " in query: yield SearchResult( text=_('Create Product Called "%s"') % query, url=manipulate_query_string(url, name=query), is_action=True, ) else: if query.lower() not in skus_seen: yield SearchResult( text=_('Create Product with SKU "%s"') % query, url=manipulate_query_string(url, sku=query), is_action=True, )
def get_search_results(self, request, query): shop = request.shop minimum_query_length = 3 skus_seen = set() if len(query) >= minimum_query_length: pk_counter = Counter() pk_counter.update(Product.objects.filter(sku__startswith=query).values_list("pk", flat=True)) name_q = Q() for part in split_query(query, minimum_query_length): name_q &= Q(name__icontains=part) pk_counter.update( Product._parler_meta.root_model.objects.filter(name_q).values_list("master_id", flat=True) ) pks = [pk for (pk, count) in pk_counter.most_common(10)] for product in Product.objects.filter(pk__in=pks, shop_products__shop_id=shop.id): relevance = 100 - pk_counter.get(product.pk, 0) skus_seen.add(product.sku.lower()) yield SearchResult( text=force_text(product), url=get_model_url(product, shop=request.shop), category=_("Products"), relevance=relevance ) if len(query) >= minimum_query_length: url = reverse("shuup_admin:shop_product.new") if " " in query: yield SearchResult( text=_("Create Product Called \"%s\"") % query, url=manipulate_query_string(url, name=query), is_action=True ) else: if query.lower() not in skus_seen: yield SearchResult( text=_("Create Product with SKU \"%s\"") % query, url=manipulate_query_string(url, sku=query), is_action=True )