Exemple #1
0
    def setUp(self):
        self.backend = get_search_backend('elasticsearch')
        self.backend.rebuilder_class = self.backend.atomic_rebuilder_class
        self.es = self.backend.es
        self.rebuilder = self.backend.get_rebuilder()

        self.backend.reset_index()
Exemple #2
0
    def filter_queryset(self, request, queryset, view):
        """
        This performs a full-text search on the result set
        Eg: ?search=James Joyce
        """
        search_enabled = getattr(settings, 'TUIUIUAPI_SEARCH_ENABLED', True)

        if 'search' in request.GET:
            if not search_enabled:
                raise BadRequestError("search is disabled")

            # Searching and filtering by tag at the same time is not supported
            if getattr(queryset, '_filtered_by_tag', False):
                raise BadRequestError(
                    "filtering by tag with a search query is not supported")

            search_query = request.GET['search']
            search_operator = request.GET.get('search_operator', None)
            order_by_relevance = 'order' not in request.GET

            sb = get_search_backend()
            queryset = sb.search(search_query,
                                 queryset,
                                 operator=search_operator,
                                 order_by_relevance=order_by_relevance)

        return queryset
Exemple #3
0
 def search(self, query_string, fields=None,
            operator=None, order_by_relevance=True, backend='default'):
     """
     This runs a search query on all the items in the QuerySet
     """
     search_backend = get_search_backend(backend)
     return search_backend.search(query_string, self, fields=fields,
                                  operator=operator, order_by_relevance=order_by_relevance)
Exemple #4
0
def choose(request, app_label, model_name):
    model = get_snippet_model_from_url_params(app_label, model_name)

    items = model.objects.all()

    # Preserve the snippet's model-level ordering if specified, but fall back on PK if not
    # (to ensure pagination is consistent)
    if not items.ordered:
        items = items.order_by('pk')

    # Search
    is_searchable = class_is_indexed(model)
    is_searching = False
    search_query = None
    if is_searchable and 'q' in request.GET:
        search_form = SearchForm(
            request.GET,
            placeholder=_("Search %(snippet_type_name)s") %
            {'snippet_type_name': model._meta.verbose_name})

        if search_form.is_valid():
            search_query = search_form.cleaned_data['q']

            search_backend = get_search_backend()
            items = search_backend.search(search_query, items)
            is_searching = True

    else:
        search_form = SearchForm(
            placeholder=_("Search %(snippet_type_name)s") %
            {'snippet_type_name': model._meta.verbose_name})

    # Pagination
    paginator, paginated_items = paginate(request, items, per_page=25)

    # If paginating or searching, render "results.html"
    if request.GET.get('results', None) == 'true':
        return render(
            request, "tuiuiusnippets/chooser/results.html", {
                'model_opts': model._meta,
                'items': paginated_items,
                'query_string': search_query,
                'is_searching': is_searching,
            })

    return render_modal_workflow(
        request, 'tuiuiusnippets/chooser/choose.html',
        'tuiuiusnippets/chooser/choose.js', {
            'model_opts': model._meta,
            'items': paginated_items,
            'is_searchable': is_searchable,
            'search_form': search_form,
            'query_string': search_query,
            'is_searching': is_searching,
        })
    def update_backend(self, backend_name, schema_only=False):
        self.stdout.write("Updating backend: " + backend_name)

        backend = get_search_backend(backend_name)

        if not backend.rebuilder_class:
            self.stdout.write("Backend '%s' doesn't require rebuilding" %
                              backend_name)
            return

        models_grouped_by_index = group_models_by_index(
            backend, get_indexed_models()).items()
        if not models_grouped_by_index:
            self.stdout.write(backend_name + ": No indices to rebuild")

        for index, models in models_grouped_by_index:
            self.stdout.write(backend_name +
                              ": Rebuilding index %s" % index.name)

            # Start rebuild
            rebuilder = backend.rebuilder_class(index)
            index = rebuilder.start()

            # Add models
            for model in models:
                index.add_model(model)

            # Add objects
            object_count = 0
            if not schema_only:
                for model in models:
                    self.stdout.write('{}: {}.{} '.format(
                        backend_name, model._meta.app_label,
                        model.__name__).ljust(35),
                                      ending='')

                    # Add items (1000 at a time)
                    for chunk in self.print_iter_progress(
                            self.queryset_chunks(
                                model.get_indexed_objects().order_by('pk'))):
                        index.add_items(model, chunk)
                        object_count += len(chunk)

                    self.print_newline()

            # Finish rebuild
            rebuilder.finish()

            self.stdout.write(backend_name +
                              ": indexed %d objects" % object_count)
            self.print_newline()
Exemple #6
0
    def setUp(self):
        # Search TUIUIUSEARCH_BACKENDS for an entry that uses the given backend path
        for backend_name, backend_conf in settings.TUIUIUSEARCH_BACKENDS.items(
        ):
            if backend_conf['BACKEND'] == self.backend_path:
                self.backend = get_search_backend(backend_name)
                self.backend_name = backend_name
                break
        else:
            # no conf entry found - skip tests for this backend
            raise unittest.SkipTest(
                "No TUIUIUSEARCH_BACKENDS entry for the backend %s" %
                self.backend_path)

        self.load_test_data()
Exemple #7
0
    def get_elasticsearch_backend(self):
        from django.conf import settings
        from tuiuiu.tuiuiusearch.backends import get_search_backend

        backend_path = 'tuiuiu.tuiuiusearch.backends.elasticsearch'

        # Search TUIUIUSEARCH_BACKENDS for an entry that uses the given backend path
        for backend_name, backend_conf in settings.TUIUIUSEARCH_BACKENDS.items(
        ):
            if backend_conf['BACKEND'] == backend_path:
                return get_search_backend(backend_name)
        else:
            # no conf entry found - skip tests for this backend
            raise unittest.SkipTest(
                "No TUIUIUSEARCH_BACKENDS entry for the backend %s" %
                backend_path)
Exemple #8
0
def list(request, app_label, model_name):
    model = get_snippet_model_from_url_params(app_label, model_name)

    permissions = [
        get_permission_name(action, model)
        for action in ['add', 'change', 'delete']
    ]
    if not any([request.user.has_perm(perm) for perm in permissions]):
        return permission_denied(request)

    items = model.objects.all()

    # Preserve the snippet's model-level ordering if specified, but fall back on PK if not
    # (to ensure pagination is consistent)
    if not items.ordered:
        items = items.order_by('pk')

    # Search
    is_searchable = class_is_indexed(model)
    is_searching = False
    search_query = None
    if is_searchable and 'q' in request.GET:
        search_form = SearchForm(
            request.GET,
            placeholder=_("Search %(snippet_type_name)s") %
            {'snippet_type_name': model._meta.verbose_name_plural})

        if search_form.is_valid():
            search_query = search_form.cleaned_data['q']

            search_backend = get_search_backend()
            items = search_backend.search(search_query, items)
            is_searching = True

    else:
        search_form = SearchForm(
            placeholder=_("Search %(snippet_type_name)s") %
            {'snippet_type_name': model._meta.verbose_name_plural})

    paginator, paginated_items = paginate(request, items)

    # Template
    if request.is_ajax():
        template = 'tuiuiusnippets/snippets/results.html'
    else:
        template = 'tuiuiusnippets/snippets/type_index.html'

    return render(
        request, template, {
            'model_opts':
            model._meta,
            'items':
            paginated_items,
            'can_add_snippet':
            request.user.has_perm(get_permission_name('add', model)),
            'is_searchable':
            is_searchable,
            'search_form':
            search_form,
            'is_searching':
            is_searching,
            'query_string':
            search_query,
        })
Exemple #9
0
 def test_import_by_full_path(self):
     db = get_search_backend(
         backend='tuiuiu.tuiuiusearch.backends.db.DatabaseSearchBackend')
     self.assertIsInstance(db, DatabaseSearchBackend)
Exemple #10
0
 def test_import_by_name(self):
     db = get_search_backend(backend='default')
     self.assertIsInstance(db, DatabaseSearchBackend)