def search_view(request): search_query = request.GET.get('q', '') if search_query: search_results = SearchQuerySet().filter(content=search_query) else: search_results = [] data = {} data['q'] = search_query form = SearchForm(initial=data) paginator = Paginator(search_results, settings.ORB_PAGINATOR_DEFAULT) # Make sure page request is an int. If not, deliver first page. try: page = int(request.GET.get('page', '1')) except ValueError: page = 1 try: results = paginator.page(page) except (EmptyPage, InvalidPage): results = paginator.page(paginator.num_pages) if search_query: search.send(sender=search_results, query=search_query, no_results=search_results.count(), request=request, page=page) return render(request, 'orb/search.html', { 'form': form, 'query': search_query, 'page': results, 'total_results': paginator.count, })
def get_search(self, request, **kwargs): self.method_check(request, allowed=['get']) self.is_authenticated(request) self.throttle_check(request) q = request.GET.get('q', '') page_no = int(request.GET.get('page', 1)) if q == '': raise ORBAPIBadRequest(ERROR_CODE_SEARCH_NO_QUERY) # Allow basic search without Solr based on local configuration if getattr(settings, 'SEARCH_DB', False): sqs = Resource.resources.approved().text_search(q) else: sqs = SearchQuerySet().models(Resource).load_all().auto_query(q) paginator = Paginator(sqs, 20) try: page = paginator.page(page_no) except InvalidPage: raise Http404("Sorry, no results on that page.") objects = [] for result in page.object_list: if result: if getattr(settings, 'SEARCH_DB', False): # Search performed directly against database bundle = self.build_bundle(obj=result, request=request) else: # Search performed against search engine bundle = self.build_bundle(obj=result.object, request=request) bundle = self.full_dehydrate(bundle) objects.append(bundle) object_list = { 'objects': objects, } search.send(sender=sqs, query=q, no_results=sqs.count(), request=request, page=page_no, type=SearchTracker.SEARCH_API) self.log_throttled_access(request) return self.create_response(request, object_list)
def search_advanced_results_view(request): form = AdvancedSearchForm(request.GET) if form.is_valid(): q = form.cleaned_data.get('q') results, filter_tags = form.search() if q: search_results = SearchQuerySet().filter( content=q).models(Resource).values_list('pk', flat=True) results = results.filter(pk__in=search_results) paginator = Paginator(results, settings.ORB_PAGINATOR_DEFAULT) try: page = int(request.GET.get('page', 1)) except ValueError: page = 1 try: resources = paginator.page(page) except (EmptyPage, InvalidPage): resources = paginator.page(paginator.num_pages) search.send(sender=results, query=q, no_results=results.count(), request=request, type=SearchTracker.SEARCH_ADV, page=page) license_tags = form.cleaned_data['license'] else: filter_tags = Tag.objects.filter(pk=None) license_tags = [] resources = Resource.objects.filter(pk=None) paginator = Paginator(resources, settings.ORB_PAGINATOR_DEFAULT) return render( request, 'orb/search_advanced_results.html', { 'filter_tags': filter_tags, 'license_tags': license_tags, 'q': form.cleaned_data.get('q'), 'page': resources, 'total_results': paginator.count, })
def search_view(request): search_query = request.GET.get('q', '') if search_query: search_results = SearchQuerySet().filter( content=search_query).models(Resource).values_list('pk', flat=True) results = Resource.objects.filter(pk__in=search_results) else: search_results = [] results = Resource.objects.none() data = {} data['q'] = search_query form = SearchForm(initial=data) paginator = Paginator(results, settings.ORB_PAGINATOR_DEFAULT) # Make sure page request is an int. If not, deliver first page. try: page = int(request.GET.get('page', '1')) except ValueError: page = 1 try: results = paginator.page(page) except (EmptyPage, InvalidPage): results = paginator.page(paginator.num_pages) if search_query: search.send(sender=search_results, query=search_query, no_results=search_results.count(), request=request, page=page) return render( request, 'orb/search.html', { 'form': form, 'query': search_query, 'page': results, 'total_results': paginator.count, })
def search_advanced_results_view(request): form = AdvancedSearchForm(request.GET) if form.is_valid(): q = form.cleaned_data.get('q') results, filter_tags = form.search() if q: search_results = SearchQuerySet().filter(content=q).models(Resource).values_list('pk', flat=True) results = results.filter(pk__in=search_results) paginator = Paginator(results, settings.ORB_PAGINATOR_DEFAULT) try: page = int(request.GET.get('page', 1)) except ValueError: page = 1 try: resources = paginator.page(page) except (EmptyPage, InvalidPage): resources = paginator.page(paginator.num_pages) search.send(sender=results, query=q, no_results=results.count(), request=request, type=SearchTracker.SEARCH_ADV, page=page) license_tags = form.cleaned_data['license'] else: filter_tags = Tag.objects.filter(pk=None) license_tags = [] resources = Resource.objects.filter(pk=None) paginator = Paginator(resources, settings.ORB_PAGINATOR_DEFAULT) return render(request, 'orb/search_advanced_results.html', { 'filter_tags': filter_tags, 'license_tags': license_tags, 'q': form.cleaned_data.get('q'), 'page': resources, 'total_results': paginator.count, })