def _haystack_search(self, content, queryset, max_results, partial, using): from haystack.query import RelatedSearchQuerySet from haystack.inputs import AutoQuery if content.strip() == '': return RelatedSearchQuerySet().none() # Limit to the model bound to this manager, e.g. DataConcept. # `load_all` ensures a single database hit when loading the objects # that match sqs = RelatedSearchQuerySet().models(self.model).load_all() # If a non-default backend is being used, set which backend is to # be used. if using is not None: sqs = sqs.using(using) if partial: # Autocomplete only works with N-gram fields. sqs = sqs.autocomplete(text_auto=content) else: # Automatically handles advanced search syntax for negations # and quoted strings. sqs = sqs.filter(text=AutoQuery(content)) if queryset is not None: sqs = sqs.load_all_queryset(self.model, queryset) if max_results: return sqs[:max_results] return sqs
def _haystack_search(self, content, queryset, max_results, partial, using): from haystack.query import RelatedSearchQuerySet from haystack.inputs import AutoQuery if content.strip() == '': return RelatedSearchQuerySet().none() # Limit to the model bound to this manager, e.g. DataConcept. # `load_all` ensures a single database hit when loading the objects # that match sqs = RelatedSearchQuerySet().models(self.model).load_all() # If a non-default backend is being used, set which backend is to # be used. if using is not None: sqs = sqs.using(using) if partial: # Autocomplete only works with N-gram fields. sqs = sqs.autocomplete(text_auto=content) else: # Automatically handles advanced search syntax for negations # and quoted strings. sqs = sqs.filter(text=AutoQuery(content)) if queryset is not None: sqs = sqs.load_all_queryset(self.model, queryset) if max_results: return sqs[:max_results] return sqs
def search(request): """ Returns messages corresponding to a query """ mlist_fqdn = request.GET.get("mlist") if mlist_fqdn is None: mlist = None else: try: mlist = MailingList.objects.get(name=mlist_fqdn) except MailingList.DoesNotExist: raise Http404("No archived mailing-list by that name.") if not is_mlist_authorized(request, mlist): return render(request, "hyperkitty/errors/private.html", { "mlist": mlist, }, status=403) query = '' results = EmptySearchQuerySet() sqs = RelatedSearchQuerySet() # Remove private non-subscribed lists if mlist is not None: sqs = sqs.filter(mailinglist__exact=mlist.name) else: excluded_mlists = MailingList.objects.filter( archive_policy=ArchivePolicy.private.value) if request.user.is_authenticated(): subscriptions = request.user.hyperkitty_profile.get_subscriptions() excluded_mlists = excluded_mlists.exclude( list_id__in=subscriptions.keys()) excluded_mlists = excluded_mlists.values_list("name", flat=True) sqs = sqs.exclude(mailinglist__in=excluded_mlists) # Sorting sort_mode = request.GET.get('sort') if sort_mode == "date-asc": sqs = sqs.order_by("date") elif sort_mode == "date-desc": sqs = sqs.order_by("-date") # Handle data if request.GET.get('q'): form = SearchForm( request.GET, searchqueryset=sqs, load_all=True) if form.is_valid(): query = form.cleaned_data['q'] results = form.search() else: form = SearchForm(searchqueryset=sqs, load_all=True) emails = paginate(results, page_num=request.GET.get('page')) for email in emails: if request.user.is_authenticated(): email.object.myvote = email.object.votes.filter(user=request.user).first() else: email.object.myvote = None context = { 'mlist' : mlist, 'form': form, 'emails': emails, 'query': query, 'sort_mode': sort_mode, 'suggestion': None, } if results.query.backend.include_spelling: context['suggestion'] = form.get_suggestion() return render(request, "hyperkitty/search_results.html", context)
def search(request): """ Returns messages corresponding to a query """ mlist_fqdn = request.GET.get("mlist") if mlist_fqdn is None: mlist = None else: try: mlist = MailingList.objects.get(name=mlist_fqdn) except MailingList.DoesNotExist: raise Http404("No archived mailing-list by that name.") if not is_mlist_authorized(request, mlist): return render(request, "hyperkitty/errors/private.html", { "mlist": mlist, }, status=403) query = '' results = EmptySearchQuerySet() sqs = RelatedSearchQuerySet() # Remove private non-subscribed lists if mlist is not None: sqs = sqs.filter(mailinglist__exact=mlist.name) else: excluded_mlists = MailingList.objects.filter( archive_policy=ArchivePolicy.private.value) if request.user.is_authenticated: subscriptions = get_subscriptions(request.user) excluded_mlists = excluded_mlists.exclude( list_id__in=subscriptions.keys()) excluded_mlists = excluded_mlists.values_list("name", flat=True) sqs = sqs.exclude(mailinglist__in=excluded_mlists) # Sorting sort_mode = request.GET.get('sort') if sort_mode == "date-asc": sqs = sqs.order_by("date") elif sort_mode == "date-desc": sqs = sqs.order_by("-date") # Handle data if request.GET.get('q'): form = SearchForm(request.GET, searchqueryset=sqs, load_all=True) if form.is_valid(): query = form.cleaned_data['q'] results = form.search() else: form = SearchForm(searchqueryset=sqs, load_all=True) try: emails = paginate( results, request.GET.get('page'), request.GET.get('count'), ) except Exception as e: backend = settings.HAYSTACK_CONNECTIONS[DEFAULT_ALIAS]["ENGINE"] if backend == "haystack.backends.whoosh_backend.WhooshEngine": from whoosh.qparser.common import QueryParserError search_exception = QueryParserError if backend == "xapian_backend.XapianEngine": from xapian import QueryParserError search_exception = QueryParserError if not isinstance(e, search_exception): raise emails = paginate([]) form.add_error( "q", ValidationError( _('Parsing error: %(error)s'), params={"error": e}, code="parse", )) for email in emails: if request.user.is_authenticated: email.object.myvote = email.object.votes.filter( user=request.user).first() else: email.object.myvote = None context = { 'mlist': mlist, 'form': form, 'emails': emails, 'query': query, 'sort_mode': sort_mode, 'suggestion': None, } if results.query.backend.include_spelling: context['suggestion'] = form.get_suggestion() return render(request, "hyperkitty/search_results.html", context)
def get_context_data(self, **kwargs): context = super(SAQuestionIndex, self).get_context_data(**kwargs) context['ministers'] = [] ministers = Section.objects.filter(parent__heading='Questions') ministers = sorted(ministers, key=questions_section_sort_key) for minister in ministers: context['ministers'].append({ 'title': minister.title.replace('Questions asked to the ', ''), 'slug': minister.slug }) context['orderby'] = 'recentanswers' context['minister'] = 'all' context['q'] = '' #set filter values for key in ('orderby', 'minister', 'q'): if key in self.request.GET: context[key] = self.request.GET[key] if not context['orderby'] in ['recentquestions', 'recentanswers']: context['orderby'] = 'recentquestions' search_result_sections = [] if context['q'] != '': #using a RelatedSearchQuerySet seems to result in fewer #queries, although the same results can be achieved with a #SearchQuerySet query = RelatedSearchQuerySet().models(Speech) query = query.filter( tags__name__in=['question', 'answer'], content=AutoQuery(context['q']), ).load_all() all_speeches = Speech.objects.all().filter( tags__name__in=['question', 'answer']) if context['minister'] != 'all': all_speeches = all_speeches.filter( section__parent__slug=context['minister']) query = query.load_all_queryset(Speech, all_speeches) for result in query: search_result_sections.append(result.object.section.id) sections = Section \ .objects \ .filter( parent__parent__heading='Questions' ) \ .select_related('parent') \ .prefetch_related('speech_set') \ .annotate( earliest_date=Min('speech__start_date'), smallest_id=Min('speech__id'), number_speeches=Count('speech'), latest_date=Max('speech__start_date'), ) if context['minister'] != 'all': sections = sections.filter(parent__slug=context['minister']) if len(search_result_sections) > 0: sections = sections.filter(id__in=search_result_sections) if context['orderby'] == 'recentanswers': sections = sections.filter(number_speeches__gt=1).order_by( '-latest_date', '-smallest_id') else: sections = sections.order_by('-earliest_date', '-smallest_id') paginator = Paginator(sections, 10) page = self.request.GET.get('page') try: sections = paginator.page(page) except PageNotAnInteger: sections = paginator.page(1) except EmptyPage: sections = paginator.page(paginator.num_pages) context['paginator'] = sections #format questions and answers for the template questions = [] for section in sections: question = section.speech_set.all()[0] question.questionto = section.parent.heading.replace( 'Questions asked to the ', '') question.questionto_slug = section.parent.slug #assume if there is more than one speech that the question is answered if len(section.speech_set.all()) > 1: question.answer = section \ .speech_set \ .all()[len(section.speech_set.all()) - 1] #extract the actual reply from the reply text (replies #often include the original question and other text, #such as a letterhead) text = question.answer.text.split('REPLY') if len(text) > 1: question.answer.text = text[len(text) - 1] if question.answer.text[0] == ':': question.answer.text = question.answer.text[1:] questions.append(question) context['speeches'] = questions return context
def get_context_data(self, **kwargs): context = super(SAQuestionIndex, self).get_context_data(**kwargs) context['ministers'] = [] ministers = Section.objects.filter(parent__heading='Questions') ministers = sorted(ministers, key=questions_section_sort_key) for minister in ministers: context['ministers'].append({ 'title': minister.title.replace('Questions asked to the ', ''), 'slug': minister.slug }) context['orderby'] = 'recentanswers' context['minister'] = 'all' context['q'] = '' #set filter values for key in ('orderby', 'minister', 'q'): if key in self.request.GET: context[key] = self.request.GET[key] if not context['orderby'] in ['recentquestions', 'recentanswers']: context['orderby'] = 'recentquestions' search_result_sections = [] if context['q'] != '': #using a RelatedSearchQuerySet seems to result in fewer #queries, although the same results can be achieved with a #SearchQuerySet query = RelatedSearchQuerySet().models(Speech) query = query.filter( tags__name__in=['question', 'answer'], content=AutoQuery(context['q']), ).load_all() all_speeches = Speech.objects.all().filter( tags__name__in=['question', 'answer']) if context['minister'] != 'all': all_speeches = all_speeches.filter( section__parent__slug=context['minister'] ) query = query.load_all_queryset(Speech, all_speeches) for result in query: search_result_sections.append(result.object.section.id) sections = Section \ .objects \ .filter( parent__parent__heading='Questions' ) \ .select_related('parent__heading') \ .prefetch_related('speech_set') \ .annotate( earliest_date=Min('speech__start_date'), smallest_id=Min('speech__id'), number_speeches=Count('speech'), latest_date=Max('speech__start_date'), ) if context['minister'] != 'all': sections = sections.filter(parent__slug=context['minister']) if len(search_result_sections) > 0: sections = sections.filter(id__in=search_result_sections) if context['orderby'] == 'recentanswers': sections = sections.filter(number_speeches__gt=1).order_by( '-latest_date', '-smallest_id' ) else: sections = sections.order_by( '-earliest_date', '-smallest_id' ) paginator = Paginator(sections, 10) page = self.request.GET.get('page') try: sections = paginator.page(page) except PageNotAnInteger: sections = paginator.page(1) except EmptyPage: sections = paginator.page(paginator.num_pages) context['paginator'] = sections #format questions and answers for the template questions = [] for section in sections: question = section.speech_set.all()[0] question.questionto = section.parent.heading.replace( 'Questions asked to the ', '') question.questionto_slug = section.parent.slug #assume if there is more than one speech that the question is answered if len(section.speech_set.all()) > 1: question.answer = section \ .speech_set \ .all()[len(section.speech_set.all()) - 1] #extract the actual reply from the reply text (replies #often include the original question and other text, #such as a letterhead) text = question.answer.text.split('REPLY') if len(text) > 1: question.answer.text = text[len(text) - 1] if question.answer.text[0] == ':': question.answer.text = question.answer.text[1:] questions.append(question) context['speeches'] = questions return context
def search(request): """ Returns messages corresponding to a query """ mlist_fqdn = request.GET.get("mlist") if mlist_fqdn is None: mlist = None else: try: mlist = MailingList.objects.get(name=mlist_fqdn) except MailingList.DoesNotExist: raise Http404("No archived mailing-list by that name.") if not is_mlist_authorized(request, mlist): return render(request, "hyperkitty/errors/private.html", { "mlist": mlist, }, status=403) query = '' results = EmptySearchQuerySet() sqs = RelatedSearchQuerySet() # Remove private non-subscribed lists if mlist is not None: sqs = sqs.filter(mailinglist__exact=mlist.name) else: excluded_mlists = MailingList.objects.filter( archive_policy=ArchivePolicy.private.value) if request.user.is_authenticated(): subscriptions = request.user.hyperkitty_profile.get_subscriptions() excluded_mlists = excluded_mlists.exclude( name__in=subscriptions.keys()) excluded_mlists = excluded_mlists.values_list("name", flat=True) sqs = sqs.exclude(mailinglist__in=excluded_mlists) # Sorting sort_mode = request.GET.get('sort') if sort_mode == "date-asc": sqs = sqs.order_by("date") elif sort_mode == "date-desc": sqs = sqs.order_by("-date") # Handle data if request.GET.get('q'): form = SearchForm( request.GET, searchqueryset=sqs, load_all=True) if form.is_valid(): query = form.cleaned_data['q'] results = form.search() else: form = SearchForm(searchqueryset=sqs, load_all=True) messages = paginate(results, page_num=request.GET.get('page')) for message in messages: if request.user.is_authenticated(): message.object.myvote = message.object.votes.filter(user=request.user).first() else: message.object.myvote = None context = { 'mlist' : mlist, 'form': form, 'messages': messages, 'query': query, 'sort_mode': sort_mode, 'suggestion': None, } if results.query.backend.include_spelling: context['suggestion'] = form.get_suggestion() return render(request, "hyperkitty/search_results.html", context)