Пример #1
0
    def _search_review_requests(self,
                                request,
                                max_results,
                                local_site=None,
                                q=None,
                                id_q=None,
                                *args,
                                **kwargs):
        """Search for a review request and return the results.

        If indexed search is enabled, this will use the search index. Otherwise
        it will query against the database.

        Args:
            local_site (reviewboard.site.models.LocalSite, optional):
                The current local site.

            max_results (int):
                The maximum number of results to return.

            q (unicode, optional):
                The search text.

            id_q (int, optional):
                An optional ID to search against review request IDs.

            *args (tuple):
                Ignored positional arguments.

            **kwargs (dict):
                Ignored keyword arguments.

        Returns:
            django.db.models.query.QuerySet or haystack.query.SearchQuerySet:
            A query for review requests matching the given arguments.
        """
        if (search_backend_registry.search_enabled
                and search_backend_registry.on_the_fly_indexing_enabled):
            # If search is enabled, we will use the index to perform the query.
            form = RBSearchForm(user=request.user,
                                local_site=local_site,
                                data={
                                    'q':
                                    q,
                                    'id':
                                    id_q,
                                    'model_filter':
                                    [RBSearchForm.FILTER_REVIEW_REQUESTS],
                                })

            return [
                {
                    'id': result.review_request_id,
                    'public': True,  # Drafts are not indexed.
                    'summary': result.summary,
                } for result in form.search()[:max_results]
            ]

        # If search is disabled, we will fall back to using database queries.
        review_requests = ReviewRequest.objects.public(
            filter_private=True,
            user=request.user,
            local_site=local_site,
            status=None,
        )

        query = Q()

        if q:
            if local_site:
                query |= Q(local_id__istartswith=q)
            else:
                query |= Q(id__startswith=q)

            if len(q) >= self.MIN_SUMMARY_LEN:
                query |= Q(summary__istartswith=q)

        if id_q:
            if local_site:
                query |= Q(local_id__startswith=id_q)
            else:
                query |= Q(id__startswith=id_q)

        return review_requests.filter(query)[:max_results]
Пример #2
0
    def _search_review_requests(self, request, max_results, local_site=None,
                                q=None, id_q=None, *args, **kwargs):
        """Search for a review request and return the results.

        If indexed search is enabled, this will use the search index. Otherwise
        it will query against the database.

        Args:
            local_site (reviewboard.site.models.LocalSite, optional):
                The current local site.

            max_results (int):
                The maximum number of results to return.

            q (unicode, optional):
                The search text.

            id_q (int, optional):
                An optional ID to search against review request IDs.

            *args (tuple):
                Ignored positional arguments.

            **kwargs (dict):
                Ignored keyword arguments.

        Returns:
            django.db.models.query.QuerySet or haystack.query.SearchQuerySet:
            A query for review requests matching the given arguments.
        """
        if (search_backend_registry.search_enabled and
            search_backend_registry.on_the_fly_indexing_enabled):
            # If search is enabled, we will use the index to perform the query.
            form = RBSearchForm(
                user=request.user,
                local_site=local_site,
                data={
                    'q': q,
                    'id': id_q,
                    'model_filter': [RBSearchForm.FILTER_REVIEW_REQUESTS],
                }
            )

            return [
                {
                    'id': result.review_request_id,
                    'public': True,  # Drafts are not indexed.
                    'summary': result.summary,
                }
                for result in form.search()[:max_results]
            ]

        # If search is disabled, we will fall back to using database queries.
        review_requests = ReviewRequest.objects.public(
            filter_private=True,
            user=request.user,
            local_site=local_site,
            status=None,
        )

        query = Q()

        if q:
            if local_site:
                query |= Q(local_id__istartswith=q)
            else:
                query |= Q(id__startswith=q)

            if len(q) >= self.MIN_SUMMARY_LEN:
                query |= Q(summary__istartswith=q)

        if id_q:
            if local_site:
                query |= Q(local_id__startswith=id_q)
            else:
                query |= Q(id__startswith=id_q)

        return review_requests.filter(query)[:max_results]
Пример #3
0
    def _search_users(self,
                      request,
                      max_results,
                      local_site=None,
                      fullname=None,
                      q=None,
                      id_q=None,
                      *args,
                      **kwargs):
        """Search for users and return the results.

        Args:
            request (django.http.HttpRequest):
                The current request.

            max_results (int):
                The maximum number of results to return.

            local_site (reviewboard.site.models.LocalSite, optional):
                The current local site.

            fullname (bool, optional):
                Whether or not to perform a search against the users' full
                names.

            q (unicode, optional):
                The search text.

            id_q (int, optional):
                An optional ID to search against user IDs.

            *args (tuple):
                Ignored positional arguments.

            **kwargs (dict):
                Ignored keyword arguments.

        Returns:
            django.db.models.query.QuerySet:
            A query set for users matching the given arguments.
        """
        if (search_backend_registry.search_enabled
                and search_backend_registry.on_the_fly_indexing_enabled):
            # If search is enabled, we will use the index to perform the query.
            form = RBSearchForm(user=request.user,
                                local_site=local_site,
                                data={
                                    'q': q,
                                    'id_q': id_q,
                                    'model_filter':
                                    [RBSearchForm.FILTER_USERS],
                                })

            return [{
                'id': result.pk,
                'username': result.username,
                'fullname': result.full_name,
                'url': result.url,
            } for result in form.search()[:max_results]]

        # If search is disabled, we will fall back to using database queries.
        if local_site:
            users = local_site.users.filter(is_active=True)
        else:
            users = self.model.objects.filter(is_active=True)

        if q:
            parts = q.split(' ', 1)

            if len(parts) > 1:
                query = ((Q(first_name__istartswith=parts[0])
                          & Q(last_name__istartswith=parts[1])) |
                         (Q(first_name__istartswith=parts[1])
                          & Q(last_name__istartswith=parts[0])))

                if fullname:
                    query |= (Q(first_name__istartswith=q)
                              | Q(last_name__istartswith=q))
            else:
                query = (Q(username__istartswith=q)
                         | Q(first_name__istartswith=q)
                         | Q(last_name__istartswith=q))

            users = users.filter(query)

        return users[:max_results]
Пример #4
0
    def _search_users(self, request, max_results, local_site=None,
                      fullname=None, q=None, id_q=None, *args, **kwargs):
        """Search for users and return the results.

        Args:
            request (django.http.HttpRequest):
                The current request.

            max_results (int):
                The maximum number of results to return.

            local_site (reviewboard.site.models.LocalSite, optional):
                The current local site.

            fullname (bool, optional):
                Whether or not to perform a search against the users' full
                names.

            q (unicode, optional):
                The search text.

            id_q (int, optional):
                An optional ID to search against user IDs.

            *args (tuple):
                Ignored positional arguments.

            **kwargs (dict):
                Ignored keyword arguments.

        Returns:
            django.db.models.query.QuerySet or list:
            A query set for users matching the given arguments.
        """
        if (search_backend_registry.search_enabled and
            search_backend_registry.on_the_fly_indexing_enabled):
            # If search is enabled, we will use the index to perform the query.
            form = RBSearchForm(
                user=request.user,
                local_site=local_site,
                data={
                    'q': q,
                    'id_q': id_q,
                    'model_filter': [RBSearchForm.FILTER_USERS],
                }
            )

            results = []

            for result in form.search()[:max_results]:
                raw_user = {
                    'id': result.pk,
                    'username': result.username,
                    'url': result.url,
                }

                if not result.is_profile_private:
                    raw_user['fullname'] = result.full_name

                results.append(raw_user)

            return results

        # If search is disabled, we will fall back to using database queries.
        if local_site:
            users = local_site.users.filter(is_active=True)
        else:
            users = self.model.objects.filter(is_active=True)

        if q:
            parts = q.split(' ', 1)

            if len(parts) > 1:
                query = (
                    (Q(first_name__istartswith=parts[0]) &
                     Q(last_name__istartswith=parts[1])) |
                    (Q(first_name__istartswith=parts[1]) &
                     Q(last_name__istartswith=parts[0]))
                )

                if fullname:
                    query |= (Q(first_name__istartswith=q) |
                              Q(last_name__istartswith=q))

                query &= Q(profile__is_private=False)
            else:
                query = (Q(username__istartswith=q) |
                         (Q(profile__is_private=False) &
                          (Q(first_name__istartswith=q) |
                           Q(last_name__istartswith=q))))

            users = users.filter(query)

        return users[:max_results]