<commit_before>from django.shortcuts import render_to_response
from django.http import Http404, QueryDict

from pure_pagination import Paginator, InvalidPage

from haystack.views import SearchView as BaseSearchView

from pybb.helpers import (lookup_users, lookup_post_topics,
                          lookup_post_attachments)
from pybb.contrib.search.fields import TreeModelMultipleChoiceField
from pybb.contrib.search.utils import SearchQuerySetSafeIterator
from pybb.models import Forum


class SearchView(BaseSearchView):
    def build_form(self, *args, **kwargs):
        '''
        Override to change the form and the searchqueryset according to the user.
        Be carefull this is not thread safe ! Use haystack.views.search_view_factory
        to build this view.
        '''
        form = super(SearchView, self).build_form(*args, **kwargs)

        user = self.request.user

        if not user.is_authenticated():
            form.searchqueryset = form.searchqueryset.exclude(hidden=True)
        elif not user.is_staff:
            form.searchqueryset = form.searchqueryset.exclude(staff=True)

        form.fields['forums'] = TreeModelMultipleChoiceField(
            queryset=Forum.objects.filter_by_user(user),
            join_field='forum_id',
            required=False
        )

        return form

    def build_page(self):
        """
        override to use pure_pagination
        """
        try:
            page_no = int(self.request.GET.get('page', 1))
        except (TypeError, ValueError):
            raise Http404("Not a valid number for page.")

        if page_no < 1:
            raise Http404("Pages should be 1 or greater.")

        start_offset = (page_no - 1) * self.results_per_page

        self.results[start_offset:start_offset + self.results_per_page]
        it = SearchQuerySetSafeIterator(self.results)

        paginator = Paginator(it, self.results_per_page)

        try:
            page = paginator.page(page_no)
        except InvalidPage:
            raise Http404("No such page!")

        return (paginator, page)

    def normalize_results(self, result_list):
        """
        Adapt results to Post interface in order to display them the same way
        in templates
        """
        lookup_post_topics(result_list)
        lookup_users(result_list)
        lookup_post_attachments(result_list)

        for result in result_list:
            result.id = result.pk
            result.get_body_html = False
            result.get_attachments = result._attachments

    def create_response(self):
        """
        Generates the actual HttpResponse to send back to the user.
        """
        (paginator, page) = self.build_page()

        # build query_string<commit_msg>prevent updating date to be displayed if it's equal to creation date<commit_after>from django.shortcuts import render_to_response
from django.http import Http404, QueryDict

from pure_pagination import Paginator, InvalidPage

from haystack.views import SearchView as BaseSearchView

from pybb.helpers import (lookup_users, lookup_post_topics,
                          lookup_post_attachments)
from pybb.contrib.search.fields import TreeModelMultipleChoiceField
from pybb.contrib.search.utils import SearchQuerySetSafeIterator
from pybb.models import Forum


class SearchView(BaseSearchView):
    def build_form(self, *args, **kwargs):
        '''
        Override to change the form and the searchqueryset according to the user.
        Be carefull this is not thread safe ! Use haystack.views.search_view_factory
        to build this view.
        '''
        form = super(SearchView, self).build_form(*args, **kwargs)

        user = self.request.user

        if not user.is_authenticated():
            form.searchqueryset = form.searchqueryset.exclude(hidden=True)
        elif not user.is_staff:
            form.searchqueryset = form.searchqueryset.exclude(staff=True)

        form.fields['forums'] = TreeModelMultipleChoiceField(
            queryset=Forum.objects.filter_by_user(user),
            join_field='forum_id',
            required=False
        )

        return form

    def build_page(self):
        """
        override to use pure_pagination
        """
        try:
            page_no = int(self.request.GET.get('page', 1))
        except (TypeError, ValueError):
            raise Http404("Not a valid number for page.")

        if page_no < 1:
            raise Http404("Pages should be 1 or greater.")

        start_offset = (page_no - 1) * self.results_per_page

        self.results[start_offset:start_offset + self.results_per_page]
        it = SearchQuerySetSafeIterator(self.results)

        paginator = Paginator(it, self.results_per_page)

        try:
            page = paginator.page(page_no)
        except InvalidPage:
            raise Http404("No such page!")

        return (paginator, page)

    def normalize_results(self, result_list):
        """
        Adapt results to Post interface in order to display them the same way
        in templates
        """
        lookup_post_topics(result_list)
        lookup_users(result_list)
        lookup_post_attachments(result_list)

        for result in result_list:
            result.id = result.pk
            result.get_body_html = False
            result.get_attachments = result._attachments
            if result.created == result.updated:
                result.updated = None

    def create_response(self):
        """
        Generates the actual HttpResponse to send back to the user.
        """
        (paginator, page) = self.build_page()

        # build query_string
{'fraction_non_alphanumeric': 0.07596734114305999, 'fraction_numerical': 0.0056798012069577564, 'mean_word_length': 2.4528186274509802, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1190832', 'n_tokens_mistral': 1565, 'n_tokens_neox': 1492, 'n_words': 401}