Ejemplo n.º 1
0
def search(request):
    """
    Searches pages across the entire site.
    """
    search_form = SearchForm(request.GET)
    pagetypes = []
    results = None
    results_paginated = None

    if search_form.is_valid():
        search_query = search_form.cleaned_data['s']
        search_model = search_form.cleaned_data['t']

        # get all codered models
        pagemodels = sorted(get_page_models(), key=lambda k: k.search_name)
        # get filterable models
        for model in pagemodels:
            if model.search_filterable:
                pagetypes.append(model)

        # get backend
        backend = get_search_backend()

        # DB search. Since this backend can't handle inheritance or scoring,
        # search specified page types in the desired order and chain the results together.
        # This provides better search results than simply searching limited fields on CoderedPage.
        db_models = []
        if backend.__class__ == db.SearchBackend:
            for model in get_page_models():
                if model.search_db_include:
                    db_models.append(model)
            db_models = sorted(db_models, reverse=True, key=lambda k: k.search_db_boost)

        if backend.__class__ == db.SearchBackend and db_models:
            for model in db_models:
                # if search_model is provided, only search on that model
                if not search_model or search_model == ContentType.objects.get_for_model(model).model:  # noqa
                    curr_results = model.objects.live().search(search_query)
                    if results:
                        results = list(chain(results, curr_results))
                    else:
                        results = curr_results

        # Fallback for any other search backend
        else:
            if search_model:
                try:
                    model = ContentType.objects.get(model=search_model).model_class()
                    results = model.objects.live().search(search_query)
                except search_model.DoesNotExist:
                    results = None
            else:
                results = CoderedPage.objects.live().order_by('-last_published_at').search(search_query)  # noqa

        # paginate results
        if results:
            paginator = Paginator(results, GeneralSettings.for_request(request).search_num_results)
            page = request.GET.get('p', 1)
            try:
                results_paginated = paginator.page(page)
            except PageNotAnInteger:
                results_paginated = paginator.page(1)
            except EmptyPage:
                results_paginated = paginator.page(1)
            except InvalidPage:
                results_paginated = paginator.page(1)

        # Log the query so Wagtail can suggest promoted results
        Query.get(search_query).add_hit()

    # Render template
    return render(request, 'coderedcms/pages/search.html', {
        'request': request,
        'pagetypes': pagetypes,
        'form': search_form,
        'results': results,
        'results_paginated': results_paginated
    })
Ejemplo n.º 2
0
def get_searchform(request=None):
    if request:
        return SearchForm(request.GET)
    return SearchForm()
Ejemplo n.º 3
0
def search_view(request, public):
    """
    Searches pages across the entire site.
    Replaces the codered search view
    Also used for staff search if public=False
    """
    search_form = SearchForm(request.GET)
    pagetypes = []
    results = None
    results_paginated = None

    if search_form.is_valid():
        search_query = search_form.cleaned_data["s"]
        search_model = search_form.cleaned_data["t"]

        # # get all codered models
        # pagemodels = sorted(get_page_models(), key=lambda k: k.search_name)
        # # get filterable models
        # for model in pagemodels:
        #     if model.search_filterable:
        #         pagetypes.append(model)

        # get backend
        backend = get_search_backend()

        # Custom search code
        items_1 = Item.objects.filter(image__isnull=False, archive=False)
        items_3 = Item.objects.filter(image__isnull=False, archive=True)
        items_2 = Item.objects.filter(image__isnull=True, archive=False)
        items_4 = Item.objects.filter(image__isnull=True, archive=True)
        if public:
            items_1 = items_1.filter(visible=True)
            items_2 = items_2.filter(visible=True)
            items_3 = items_3.filter(visible=True)
            items_4 = items_4.filter(visible=True)
        results_1 = backend.search(search_query, items_1)
        results_2 = backend.search(search_query, items_2)
        results_3 = backend.search(search_query, items_3)
        results_4 = backend.search(search_query, items_4)
        results_page = backend.search(search_query, Page)
        results = list(
            chain(results_1, results_2, results_3, results_4, results_page))
        # paginate results
        if results:
            paginator = Paginator(
                results,
                GeneralSettings.for_request(request).search_num_results)
            page = request.GET.get("p", 1)
            try:
                results_paginated = paginator.page(page)
            except PageNotAnInteger:
                results_paginated = paginator.page(1)
            except EmptyPage:
                results_paginated = paginator.page(1)
            except InvalidPage:
                results_paginated = paginator.page(1)

        # Log the query so Wagtail can suggest promoted results
        Query.get(search_query).add_hit()

    # Render template
    template = "shop/public/search.html" if public else "shop/search.html"

    return render(
        request,
        template,
        {
            "request": request,
            "pagetypes": pagetypes,
            "form": search_form,
            "results": results,
            "results_paginated": results_paginated,
            "public": public,
        },
    )