def results(request, template_name="search/results.html"): # get current search phrase q = request.GET.get('q', '') slug = request.GET.get('slug', '') print(slug) # get current page number. Set to 1 is missing or invalid try: page = int(request.GET.get('page', 1)) except ValueError: page = 1 # retrieve the matching products matching = search.products(q).get('products') if len(slug) > 0: cat_id = Category.objects.get(slug=slug).id matching = search.products2(q, cat_id).get('products') print(matching) # generate the pagintor object paginator = Paginator(matching, settings.PRODUCTS_PER_PAGE) try: results = paginator.page(page).object_list except (InvalidPage, EmptyPage): results = paginator.page(1).object_list # store the search search.store(request, q) # the usual... page_title = 'Search Results for: ' + q return render(request, template_name, locals(), RequestContext(request))
def order_products(request, products): """ :param request: :param products: lista de productos de tipo QuerySet :return: productos ordenados, y la instancia del form de ordanamiento """ if not products: # si no hay productos es pq este metodo es llamado desde search/views.py # obtenemos la cadena de busqueda de la session donde se guardo # buscamos en la db para esa cadena, q resultados(productos) arrojo. # si arroja la excep es pq el usuario entro una cadena de len 1 y eso no lo almacenamos en la db # No puede haber ninguna categoria vacia, si esto ocurre, al entrar en una url por ej category/categ3/ # y categ3 esta vacia (sin productos) entonces entramos a este if, pero no queremos este comportamiento # y se lanza un excep KeyError pq no se va a encontrar la cookie de busqueda search_key = request.session['search_key'] try: found_products = SearchTerm.objects.filter(q=search_key).order_by('-search_date')[0].found_products.all() except IndexError: # por si found_products es [] found_products = search.products(search_key) products = found_products order_dict = {'created_at': 'created_at', 'name': 'name', 'brand': 'brand', 'price': 'price'} if request.method == "POST": # ordenar order_by_form = OrderByForm(request.POST, label_suffix=" ") option = request.POST.get('order_by', 'created_at') order_by = order_dict[option] if 'submit_up.x' in request.POST: # si se quiere ordenar ascendente order_by = "-" + order_by products = products.order_by(order_by) else: products = products.order_by('created_at') order_by_form = OrderByForm(label_suffix=" ") return products, order_by_form
def recommended_from_search(request): # get the common words from the stored searches common_words = frequest_search_words(request) matching = [] for word in common_words: results = search.products(word).get('products',[]) for result in results: if len(matching) < PRODUCTS_PER_ROW and not result in matching: matching.append(result)
def recommended_from_search(request): """ get the common words from the stored searches """ common_words = frequent_search_words(request) from search import search matching = [] for word in common_words: results = search.products(word).get('products', []) for r in results: if len(matching) < PRODUCTS_PER_ROW and r not in matching: matching.append(r) return matching
def recommended_from_search(request): common_words = frequent_search_words(request) from search import search matching = [] for word in common_words: results = search.products(word).get("products", []) for result in results: if len(matching) < PRODUCTS_PER_ROW and not result in matching: matching.append(result) return matching
def autosearch(request): q = request.GET.get('q', '') slug = request.GET.get('slug', '') if len(slug) > 0: cat_id = Category.objects.get(slug=slug).id data = list( search.products2(q, cat_id).get('products').values( 'name', 'description', 'sku', 'brand', 'meta_description', 'categories__name', 'categories__slug', 'meta_keywords', 'categories__description', 'categories__meta_keywords', 'categories__meta_description', 'slug', 'price', 'thumbnail', 'image_caption')) else: data = list( search.products(q).get('products').values( 'name', 'description', 'sku', 'brand', 'meta_description', 'categories__name', 'categories__slug', 'meta_keywords', 'categories__description', 'categories__meta_keywords', 'categories__meta_description', 'slug', 'price', 'thumbnail', 'image_caption')) # loop through the items and check for redundant products. # Some products are redundant because they belong to more than one category. # note: This only works if redundant data follow the other immediately in the list data2 = [] count = 0 for d in data: # Check if the list data2 has any item in it. It should be empty in the first loop if len(data2) > 0: # if it has an item then check if the item we want to add in it shares a name with the existing one if len(data2) > 0 and d['name'] != data2[count]['name']: # if it does not then add the new item to the list and then increment count by 1 data2.append(d) count += 1 # if data2 list is empty. first time in the loop, just add the data but don't increment count. else: data2.append(d) testdata = data2.copy() # html = '<ul id="slist" class="sf" style="list-style: none;">' html = '' for p in testdata: html += '<li id="' + p[ 'name'] + '" class="pitem"><img src="' + settings.MEDIA_URL + p[ 'thumbnail'] + '" id="' + p[ 'name'] + '" class="simage" /><label id="' + p[ 'name'] + '">' + p['name'] + '</label ></li>' # html += '</ul>' data = data2 return HttpResponse(html)
def results(request, template_name="search/results.html"): # get current search phrase q = request.GET.get("q", "") # get current page number. Set to 1 is missing or invalid try: page = int(request.GET.get("page", 1)) except ValueError: page = 1 # retrieve the matching products matching = search.products(q).get("products") # generate the pagintor object paginator = Paginator(matching, settings.PRODUCTS_PER_PAGE) try: results = paginator.page(page).object_list except (InvalidPage, EmptyPage): results = paginator.page(1).object_list # store the search search.store(request, q) # the usual... page_title = "Search Results for: " + q return render(request, template_name, locals())
def results(request, template_name="search/results.html"): # get current search phrase q = request.GET.get('q', '') # get current page number. Set to 1 is missing or invalid try: page = int(request.GET.get('page', 1)) except ValueError: page = 1 # retrieve the matching products matching = search.products(q).get('products') # generate the pagintor object paginator = Paginator(matching, settings.PRODUCTS_PER_PAGE) try: results = paginator.page(page).object_list except (InvalidPage, EmptyPage): results = paginator.page(1).object_list # store the search search.store(request, q) # the usual... page_title = 'Search Results for: ' + q return render(request, template_name, locals())
def results(request): q = request.GET.get('q', '') # get current page number. Set to 1 is missing or invalid try: page = int(request.GET.get('page', 1)) except ValueError: page=1 matching = search.products(q).get('products') # generate the pagintor object paginator = Paginator(matching,settings.PRODUCTS_PER_PAGE) try: results = paginator.page(page).object_list #list of matching products except (InvalidPage, EmptyPage): results = paginator.page(1).object_list search.store(request, q) page_title = 'Search Results for: ' + q return render_to_response("search/results.html", locals(),context_instance=RequestContext(request))
def recommended_from_search(request): if recommended_from_all_views(request) is None: all_views = [] else: all_views = recommended_from_all_views(request).values('name') if recommended_from_views(request) is None: view_rec = [] else: view_rec = recommended_from_views(request).values('name') view_recent = get_recently_viewed(request).values('name') featured = Product.featured.all().values('name') # get the common words from the stored searches common_words = frequent_search_words(request) from search import search matching = [] for word in common_words: results = search.products(word).get('products', []).exclude( name__in=featured).exclude(name__in=view_recent).exclude( name__in=view_rec).exclude(name__in=all_views) for r in results: if len(matching) < PRODUCTS_PER_ROW and not r in matching: matching.append(r) return matching
def results(request, template_name="results.html"): """ template for displaying settings.PRODUCTS_PER_PAGE paginated product results """ # get current search phrase q = request.GET.get('q', '') matching = search.products(q).get('books', []) #---------------------pagination------------------------------------------------# try: page = int(request.GET.get('page', 1)) ## page number goes here except ValueError: page = 1 paginator = Paginator(matching, settings.BOOKS_PER_PAGE) try: results = paginator.page(page).object_list except (InvalidPage, EmptyPage): results = paginator.page(1).object_list #-----------------------------------------------------------------------------# search.store(request, q) page_title = 'Search Results for: ' + q context = locals() return render(request, template_name, context)
def results(request, template_name = "search/results.html"): q = request.GET.get('q', '') try: page = int(request.GET.get('page', 1)) except ValueError: page = 1 matching = search.products(q).get('products') json_products = serializers.serialize('json', matching) paginator = Paginator(matching, 9) try: results = paginator.page(page).object_list except (InvalidPage, EmptyPage): results = paginator.page(1).object_list search.store(request, q) page_title = 'Search Results for: '+ q return render_to_response(template_name, locals(), context_instance = RequestContext(request))
def results(request): # get current search phrase q = request.GET.get('q', '') try: page = int(request.GET.get('page', 1)) except ValueError: page = 1 # retrieve the matching products # matching = search.products(q).get('products', []) # print(matching) # retrieve the matching products matching = search.products(q).get('products') print(matching) # generate the pagintor object paginator = Paginator(matching, 12) try: results = paginator.page(page).object_list except (InvalidPage, EmptyPage): results = paginator.page(1).object_list # store the search search.store(request, q) page_title = 'Search Results for: ' + q return render(request, "search/results.html", {'q': q}) # def results(request): # """ template for displaying settings.PRODUCTS_PER_PAGE paginated product results """ # # get current search phrase # q = request.GET.get('q', '') # # get current page number. Set to 1 is missing or invalid # try: # page = int(request.GET.get('page', 1)) # except ValueError: # page = 1 # matching = search.products(q).get('products', []) # # generate the pagintor object # paginator = Paginator(matching, # settings.PRODUCTS_PER_PAGE) # try: # results = paginator.page(page).object_list # except (InvalidPage, EmptyPage): # results = paginator.page(1).object_list # search.store(request, q) # page_title = 'Search Results for: ' + q # return render_to_response(template_name, locals(), context_instance=RequestContext(request)) # from django.core.paginator import Paginator # PAGE_SIZE = 20 # def all_babysitters(request): # p = request.query_params.get("page", 1) # babysitters = Babysitter.objects.all() # paginator = Paginator(babysitters, PAGE_SIZE) # # page = paginator.page(p) # ~ Django 1.11 # # The following line was added, see comments below for why # page = paginator.get_page(p) # Django 2.0 + # nextpage = page.next_page_number() if page.has_next() else None # data = BabySitterSerializer([i for i in page.object_list], many=True) # pages = { # "current": p, # "next": nextpage, # "total_pages": paginator.num_pages # "total_results": paginator.count # } # return { # "page_data": pages # "data": data # }