Пример #1
0
    def filter_queryset(self, request, queryset, view):

        query = request.query_params.get('document_q', None)
        query_lookup = request.query_params.get('q_lookup', None)
        lookup_expr = 'ft'
        if query_lookup:
            if query_lookup == 'exact':
                lookup_expr = 'ft'
            if query_lookup == 'startswith':
                lookup_expr = 'ft_startswith'

        if query:

            query_fields = request.query_params.get('q_fields')
            dict_query_list = [TSConfig('simple'), query]

            model_prefix = ''

            # when SearchFilter is used on other endpoints than activities,
            # add activity__ to the filter name
            if Document is not queryset.model:
                model_prefix = 'document__'

            # if root organisations set, only query searchable activities
            # if settings.ROOT_ORGANISATIONS:
            #     queryset = queryset.filter(**{'{0}is_searchable'.format(model_prefix): True})

            return queryset.filter(
                **{'{0}documentsearch__text__{1}'.format(model_prefix, lookup_expr): dict_query_list})

        return queryset
Пример #2
0
    def test_alternative_config(self):
        from djorm_pgfulltext.fields import TSConfig

        p = Person.objects.filter(
            search_index__ft_startswith=[TSConfig('names'), 'progra'])[0]

        self.assertEquals(p.pk, self.p1.pk)
Пример #3
0
    def test_alternative_config(self):
        from djorm_pgfulltext.fields import TSConfig

        pq = Person.objects.filter(
            search_index__ft_startswith=[TSConfig('names'), 'progra'])
        p = pq[0]

        self.assertEquals(p.pk, self.p1.pk)

        # make sure it is preserved after re-query
        self.assertEquals(pq.all()[0].pk, self.p1.pk)
Пример #4
0
    def filter_queryset(self, request, queryset, view):

        query = request.query_params.get('q', None)
        query_lookup = request.query_params.get('q_lookup', None)
        lookup_expr = 'ft'
        if query_lookup:
            if query_lookup == 'exact':
                lookup_expr = 'ft'
            if query_lookup == 'startswith':
                lookup_expr = 'ft_startswith'

        if query:

            query_fields = request.query_params.get('q_fields')
            dict_query_list = [TSConfig('simple'), query]

            model_prefix = ''

            # when SearchFilter is used on other endpoints than activities,
            # add activity__ to the filter name
            if Activity is not queryset.model:
                model_prefix = 'activity__'

            # if root organisations set, only query searchable activities
            if settings.ROOT_ORGANISATIONS:
                queryset = queryset.filter(
                    **{'{0}is_searchable'.format(model_prefix): True})

            if query_fields:
                query_fields = query_fields.split(',')

                if isinstance(query_fields, list):
                    filters = combine_filters([
                        Q(
                            **{
                                '{0}activitysearch__{1}__{2}'.format(
                                    model_prefix, field, lookup_expr):
                                dict_query_list
                            }) for field in query_fields
                    ])
                    return queryset.filter(filters)
            else:
                return queryset.filter(
                    **{
                        '{0}activitysearch__text__{1}'.format(
                            model_prefix, lookup_expr):
                        dict_query_list
                    })

        return queryset