Python wagtail.search.models.Query.get() Examples

The following are 29 code examples of wagtail.search.models.Query.get(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module wagtail.search.models.Query , or try the search function .
Example #1
Source File: views.py    From WF-website with GNU Affero General Public License v3.0 7 votes vote down vote up
def search(request):
    search_query = request.GET.get('query', None)
    page = request.GET.get('page', 1)

    # Search
    if search_query:
        search_results = Page.objects.live().search(search_query)
        query = Query.get(search_query)

        # Record hit
        query.add_hit()
    else:
        search_results = Page.objects.none()

    # Pagination
    paginator = Paginator(search_results, 10)
    try:
        search_results = paginator.page(page)
    except PageNotAnInteger:
        search_results = paginator.page(1)
    except EmptyPage:
        search_results = paginator.page(paginator.num_pages)

    return render(request, 'search/search.html', {
        'search_query': search_query,
        'search_results': search_results,
    }) 
Example #2
Source File: tests.py    From wagtail with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_get_search_promotions_template_tag(self):
        # Create a search pick to the root page
        pick = SearchPromotion.objects.create(
            query=Query.get("root page"),
            page_id=1,
            sort_order=0,
            description="First search pick",
        )

        # Create another search pick against a different query
        SearchPromotion.objects.create(
            query=Query.get("root page again"),
            page_id=1,
            sort_order=0,
            description="Second search pick",
        )

        # Check
        search_picks = list(get_search_promotions("root page"))
        self.assertEqual(search_picks, [pick]) 
Example #3
Source File: tests.py    From wagtail with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_post(self):
        # Submit
        post_data = {
            'query_string': "Hello",
            'editors_picks-TOTAL_FORMS': 2,
            'editors_picks-INITIAL_FORMS': 2,
            'editors_picks-MAX_NUM_FORMS': 1000,
            'editors_picks-0-id': self.search_pick.id,
            'editors_picks-0-DELETE': '',
            'editors_picks-0-ORDER': 0,
            'editors_picks-0-page': 1,
            'editors_picks-0-description': "Description has changed",  # Change
            'editors_picks-1-id': self.search_pick_2.id,
            'editors_picks-1-DELETE': '',
            'editors_picks-1-ORDER': 1,
            'editors_picks-1-page': 2,
            'editors_picks-1-description': "Homepage",
        }
        response = self.client.post(reverse('wagtailsearchpromotions:edit', args=(self.query.id, )), post_data)

        # User should be redirected back to the index
        self.assertRedirects(response, reverse('wagtailsearchpromotions:index'))

        # Check that the search pick description was edited
        self.assertEqual(SearchPromotion.objects.get(id=self.search_pick.id).description, "Description has changed") 
Example #4
Source File: tests.py    From wagtail with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_post(self):
        # Submit
        post_data = {
            'query_string': "test",
            'editors_picks-TOTAL_FORMS': 1,
            'editors_picks-INITIAL_FORMS': 0,
            'editors_picks-MAX_NUM_FORMS': 1000,
            'editors_picks-0-DELETE': '',
            'editors_picks-0-ORDER': 0,
            'editors_picks-0-page': 1,
            'editors_picks-0-description': "Hello",
        }
        response = self.client.post(reverse('wagtailsearchpromotions:add'), post_data)

        # User should be redirected back to the index
        self.assertRedirects(response, reverse('wagtailsearchpromotions:index'))

        # Check that the search pick was created
        self.assertTrue(Query.get('test').editors_picks.filter(page_id=1).exists()) 
Example #5
Source File: tests.py    From wagtail with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_results_are_ordered_alphabetically(self):
        self.make_search_picks()
        SearchPromotion.objects.create(
            query=Query.get("aaargh snake"),
            page_id=1,
            sort_order=0,
            description="ooh, it's a snake",
        )

        response = self.client.get(reverse('wagtailsearchpromotions:index'))

        # Check response
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'wagtailsearchpromotions/index.html')

        # "aargh snake" should be the first result alphabetically
        self.assertEqual(response.context['queries'][0].query_string, "aaargh snake") 
Example #6
Source File: views.py    From digihel with MIT License 5 votes vote down vote up
def search(request):
    search_query = request.GET.get('query', None)
    page = request.GET.get('page', 1)

    # Search
    if search_query:
        search_results = Page.objects.live().search(search_query)
        query = Query.get(search_query)

        # Record hit
        query.add_hit()
    else:
        search_results = Page.objects.none()

    # Pagination
    paginator = Paginator(search_results, 10)
    try:
        search_results = paginator.page(page)
    except PageNotAnInteger:
        search_results = paginator.page(1)
    except EmptyPage:
        search_results = paginator.page(paginator.num_pages)

    return render(request, 'search/search.html', {
        'search_query': search_query,
        'search_results': search_results,
    }) 
Example #7
Source File: routes.py    From puput with MIT License 5 votes vote down vote up
def entries_search(self, request, *args, **kwargs):
        search_query = request.GET.get('q', None)
        self.entries = self.get_entries()
        if search_query:
            self.entries = self.entries.search(search_query)
            self.search_term = search_query
            self.search_type = _('search')
            Query.get(search_query).add_hit()
        return Page.serve(self, request, *args, **kwargs) 
Example #8
Source File: views.py    From hypha with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def search(request):
    site = Site.find_for_request(request)
    if site != HomePage.objects.first().get_site():
        raise Http404

    search_query = request.GET.get('query', None)
    page = request.GET.get('page', 1)

    # Search
    if search_query:
        # Allow only word characters and spaces in search query.
        words = re.findall('\w+', search_query.strip())
        search_query = ' '.join(words)

        public_site = site.root_page

        search_results = Page.objects.live().descendant_of(
            public_site,
            inclusive=True,
        ).specific().search(search_query, operator='and')
        query = Query.get(search_query)

        # Record hit
        query.add_hit()
    else:
        search_results = Page.objects.none()

    # Pagination
    paginator = Paginator(search_results, settings.DEFAULT_PER_PAGE)
    try:
        search_results = paginator.page(page)
    except PageNotAnInteger:
        search_results = paginator.page(1)
    except EmptyPage:
        search_results = paginator.page(paginator.num_pages)

    return render(request, 'search/search.html', {
        'search_query': search_query,
        'search_results': search_results,
    }) 
Example #9
Source File: views.py    From longclaw_demo with MIT License 5 votes vote down vote up
def search(request):
    search_query = request.GET.get('query', None)
    page = request.GET.get('page', 1)

    # Search
    if search_query:
        search_results = Page.objects.live().search(search_query)
        query = Query.get(search_query)

        # Record hit
        query.add_hit()
    else:
        search_results = Page.objects.none()

    # Pagination
    paginator = Paginator(search_results, 10)
    try:
        search_results = paginator.page(page)
    except PageNotAnInteger:
        search_results = paginator.page(1)
    except EmptyPage:
        search_results = paginator.page(paginator.num_pages)

    return render(request, 'search/search.html', {
        'search_query': search_query,
        'search_results': search_results,
    }) 
Example #10
Source File: wagtailsearchpromotions_tags.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_search_promotions(search_query):
    if search_query:
        return Query.get(search_query).editors_picks.all()
    else:
        return SearchPromotion.objects.none() 
Example #11
Source File: views.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def edit(request, query_id):
    query = get_object_or_404(Query, id=query_id)

    if request.method == 'POST':
        # Get query
        query_form = search_forms.QueryForm(request.POST)
        # and the recommendations
        searchpicks_formset = forms.SearchPromotionsFormSet(request.POST, instance=query)

        if query_form.is_valid():
            new_query = Query.get(query_form['query_string'].value())

            # Save search picks
            if save_searchpicks(query, new_query, searchpicks_formset):
                messages.success(request, _("Editor's picks for '{0}' updated.").format(new_query), buttons=[
                    messages.button(reverse('wagtailsearchpromotions:edit', args=(query.id,)), _('Edit'))
                ])
                return redirect('wagtailsearchpromotions:index')
            else:
                if len(searchpicks_formset.non_form_errors()):
                    messages.error(request, " ".join(error for error in searchpicks_formset.non_form_errors()))
                    # formset level error (e.g. no forms submitted)
                else:
                    messages.error(request, _("Recommendations have not been saved due to errors"))
                    # specific errors will be displayed within form fields

    else:
        query_form = search_forms.QueryForm(initial=dict(query_string=query.query_string))
        searchpicks_formset = forms.SearchPromotionsFormSet(instance=query)

    return TemplateResponse(request, 'wagtailsearchpromotions/edit.html', {
        'query_form': query_form,
        'searchpicks_formset': searchpicks_formset,
        'query': query,
        'form_media': query_form.media + searchpicks_formset.media,
    }) 
Example #12
Source File: views.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def add(request):
    if request.method == 'POST':
        # Get query
        query_form = search_forms.QueryForm(request.POST)
        if query_form.is_valid():
            query = Query.get(query_form['query_string'].value())

            # Save search picks
            searchpicks_formset = forms.SearchPromotionsFormSet(request.POST, instance=query)
            if save_searchpicks(query, query, searchpicks_formset):
                messages.success(request, _("Editor's picks for '{0}' created.").format(query), buttons=[
                    messages.button(reverse('wagtailsearchpromotions:edit', args=(query.id,)), _('Edit'))
                ])
                return redirect('wagtailsearchpromotions:index')
            else:
                if len(searchpicks_formset.non_form_errors()):
                    # formset level error (e.g. no forms submitted)
                    messages.error(request, " ".join(error for error in searchpicks_formset.non_form_errors()))
                else:
                    # specific errors will be displayed within form fields
                    messages.error(request, _("Recommendations have not been created due to errors"))
        else:
            searchpicks_formset = forms.SearchPromotionsFormSet()
    else:
        query_form = search_forms.QueryForm()
        searchpicks_formset = forms.SearchPromotionsFormSet()

    return TemplateResponse(request, 'wagtailsearchpromotions/add.html', {
        'query_form': query_form,
        'searchpicks_formset': searchpicks_formset,
        'form_media': query_form.media + searchpicks_formset.media,
    }) 
Example #13
Source File: tests.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_simple(self):
        response = self.client.get(reverse('wagtailsearchpromotions:delete', args=(self.query.id, )))
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'wagtailsearchpromotions/confirm_delete.html') 
Example #14
Source File: tests.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_post_reorder(self):
        # Check order before reordering
        self.assertEqual(Query.get("Hello").editors_picks.all()[0], self.search_pick)
        self.assertEqual(Query.get("Hello").editors_picks.all()[1], self.search_pick_2)

        # Submit
        post_data = {
            'query_string': "Hello",
            'editors_picks-TOTAL_FORMS': 2,
            'editors_picks-INITIAL_FORMS': 2,
            'editors_picks-MAX_NUM_FORMS': 1000,
            'editors_picks-0-id': self.search_pick.id,
            'editors_picks-0-DELETE': '',
            'editors_picks-0-ORDER': 1,  # Change
            'editors_picks-0-page': 1,
            'editors_picks-0-description': "Root page",
            'editors_picks-1-id': self.search_pick_2.id,
            'editors_picks-1-DELETE': '',
            'editors_picks-1-ORDER': 0,  # Change
            'editors_picks-1-page': 2,
            'editors_picks-1-description': "Homepage",
        }
        response = self.client.post(reverse('wagtailsearchpromotions:edit', args=(self.query.id, )), post_data)

        # User should be redirected back to the index
        self.assertRedirects(response, reverse('wagtailsearchpromotions:index'))

        # Check that the ordering has been saved correctly
        self.assertEqual(SearchPromotion.objects.get(id=self.search_pick.id).sort_order, 1)
        self.assertEqual(SearchPromotion.objects.get(id=self.search_pick_2.id).sort_order, 0)

        # Check that the recommendations were reordered
        self.assertEqual(Query.get("Hello").editors_picks.all()[0], self.search_pick_2)
        self.assertEqual(Query.get("Hello").editors_picks.all()[1], self.search_pick) 
Example #15
Source File: tests.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_simple(self):
        response = self.client.get(reverse('wagtailsearchpromotions:edit', args=(self.query.id, )))
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'wagtailsearchpromotions/edit.html') 
Example #16
Source File: tests.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def setUp(self):
        self.login()

        # Create an search pick to edit
        self.query = Query.get("Hello")
        self.search_pick = self.query.editors_picks.create(page_id=1, description="Root page")
        self.search_pick_2 = self.query.editors_picks.create(page_id=2, description="Homepage") 
Example #17
Source File: tests.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_pagination_out_of_range(self):
        self.make_search_picks()

        response = self.client.get(reverse('wagtailsearchpromotions:index'), {'p': 99999})

        # Check response
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'wagtailsearchpromotions/index.html')

        # Check that we got the last page
        self.assertEqual(response.context['queries'].number, response.context['queries'].paginator.num_pages) 
Example #18
Source File: tests.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_pagination_invalid(self):
        self.make_search_picks()

        response = self.client.get(reverse('wagtailsearchpromotions:index'), {'p': 'Hello World!'})

        # Check response
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'wagtailsearchpromotions/index.html')

        # Check that we got page one
        self.assertEqual(response.context['queries'].number, 1) 
Example #19
Source File: tests.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_pagination(self):
        self.make_search_picks()

        response = self.client.get(reverse('wagtailsearchpromotions:index'), {'p': 2})

        # Check response
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'wagtailsearchpromotions/index.html')

        # Check that we got the correct page
        self.assertEqual(response.context['queries'].number, 2) 
Example #20
Source File: tests.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def make_search_picks(self):
        for i in range(50):
            SearchPromotion.objects.create(
                query=Query.get("query " + str(i)),
                page_id=1,
                sort_order=0,
                description="First search pick",
            ) 
Example #21
Source File: tests.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_simple(self):
        response = self.client.get(reverse('wagtailsearchpromotions:index'))
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'wagtailsearchpromotions/index.html') 
Example #22
Source File: tests.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_search_pick_ordering(self):
        # Add 3 search picks in a different order to their sort_order values
        # They should be ordered by their sort order values and not their insertion order
        SearchPromotion.objects.create(
            query=Query.get("root page"),
            page_id=1,
            sort_order=0,
            description="First search pick",
        )
        SearchPromotion.objects.create(
            query=Query.get("root page"),
            page_id=1,
            sort_order=2,
            description="Last search pick",
        )
        SearchPromotion.objects.create(
            query=Query.get("root page"),
            page_id=1,
            sort_order=1,
            description="Middle search pick",
        )

        # Check
        self.assertEqual(Query.get("root page").editors_picks.count(), 3)
        self.assertEqual(Query.get("root page").editors_picks.first().description, "First search pick")
        self.assertEqual(Query.get("root page").editors_picks.last().description, "Last search pick") 
Example #23
Source File: tests.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_search_pick_create(self):
        # Create a search pick to the root page
        SearchPromotion.objects.create(
            query=Query.get("root page"),
            page_id=1,
            sort_order=0,
            description="First search promotion",
        )

        # Check
        self.assertEqual(Query.get("root page").editors_picks.count(), 1)
        self.assertEqual(Query.get("root page").editors_picks.first().page_id, 1) 
Example #24
Source File: views.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def search(request):
    search_query = request.GET.get('query', None)
    page = request.GET.get('page', 1)

    # Search
    if search_query:
        search_results = Page.objects.live().search(search_query)
        query = Query.get(search_query)

        # Record hit
        query.add_hit()
    else:
        search_results = Page.objects.none()

    # Pagination
    paginator = Paginator(search_results, 10)
    try:
        search_results = paginator.page(page)
    except PageNotAnInteger:
        search_results = paginator.page(1)
    except EmptyPage:
        search_results = paginator.page(paginator.num_pages)

    return TemplateResponse(request, 'search/search.html', {
        'search_query': search_query,
        'search_results': search_results,
    }) 
Example #25
Source File: views.py    From wagtail-cookiecutter-foundation with MIT License 5 votes vote down vote up
def search(request):
    search_query = request.GET.get('q', None)
    page = request.GET.get('page', 1)

    # Search
    if search_query:
        search_results = Page.objects.live().search(search_query)
        query = Query.get(search_query)

        # Record hit
        query.add_hit()
    else:
        search_results = Page.objects.none()

    # Pagination
    paginator = Paginator(search_results, 10)
    try:
        search_results = paginator.page(page)
    except PageNotAnInteger:
        search_results = paginator.page(1)
    except EmptyPage:
        search_results = paginator.page(paginator.num_pages)

    return render(request, 'search/search.html', {
        'search_query': search_query,
        'search_results': search_results,
    }) 
Example #26
Source File: views.py    From securethenews with GNU Affero General Public License v3.0 5 votes vote down vote up
def search(request):
    search_query = request.GET.get('query', None)
    page = request.GET.get('page', 1)

    # Search
    if search_query:
        search_results = Page.objects.live().search(search_query)
        query = Query.get(search_query)

        # Record hit
        query.add_hit()
    else:
        search_results = Page.objects.none()

    # Pagination
    paginator = Paginator(search_results, 10)
    try:
        search_results = paginator.page(page)
    except PageNotAnInteger:
        search_results = paginator.page(1)
    except EmptyPage:
        search_results = paginator.page(paginator.num_pages)

    return render(request, 'search/search.html', {
        'search_query': search_query,
        'search_results': search_results,
    }) 
Example #27
Source File: views.py    From wagtail-personalisation with MIT License 5 votes vote down vote up
def search(request):
    search_query = request.GET.get('query', None)
    page = request.GET.get('page', 1)

    # Search
    if search_query:
        search_results = Page.objects.live().search(search_query)
        query = Query.get(search_query)

        # Record hit
        query.add_hit()
    else:
        search_results = Page.objects.none()

    # Pagination
    paginator = Paginator(search_results, 10)
    try:
        search_results = paginator.page(page)
    except PageNotAnInteger:
        search_results = paginator.page(1)
    except EmptyPage:
        search_results = paginator.page(paginator.num_pages)

    return render(request, 'search/search.html', {
        'search_query': search_query,
        'search_results': search_results,
    }) 
Example #28
Source File: views.py    From longclaw with MIT License 5 votes vote down vote up
def search(request):
    search_query = request.GET.get('query', None)
    page = request.GET.get('page', 1)

    # Search
    if search_query:
        search_results = Page.objects.live().search(search_query)
        query = Query.get(search_query)

        # Record hit
        query.add_hit()
    else:
        search_results = Page.objects.none()

    # Pagination
    paginator = Paginator(search_results, 10)
    try:
        search_results = paginator.page(page)
    except PageNotAnInteger:
        search_results = paginator.page(1)
    except EmptyPage:
        search_results = paginator.page(paginator.num_pages)

    return render(request, 'search/search.html', {
        'search_query': search_query,
        'search_results': search_results,
    }) 
Example #29
Source File: views.py    From wagtail with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def index(request):
    # Ordering
    valid_ordering = ['query_string', '-query_string', 'views', '-views']
    ordering = valid_ordering[0]

    if 'ordering' in request.GET and request.GET['ordering'] in valid_ordering:
        ordering = request.GET['ordering']

    # Query
    queries = Query.objects.filter(editors_picks__isnull=False).distinct()

    if 'views' in ordering:
        queries = queries.annotate(views=functions.Coalesce(Sum('daily_hits__hits'), 0))

    queries = queries.order_by(ordering)

    # Search
    is_searching = False
    query_string = request.GET.get('q', '')

    if query_string:
        queries = queries.filter(query_string__icontains=query_string)
        is_searching = True

    # Paginate
    paginator = Paginator(queries, per_page=20)
    queries = paginator.get_page(request.GET.get('p'))

    if request.is_ajax():
        return TemplateResponse(request, "wagtailsearchpromotions/results.html", {
            'is_searching': is_searching,
            'ordering': ordering,
            'queries': queries,
            'query_string': query_string,
        })
    else:
        return TemplateResponse(request, 'wagtailsearchpromotions/index.html', {
            'is_searching': is_searching,
            'ordering': ordering,
            'queries': queries,
            'query_string': query_string,
            'search_form': SearchForm(
                data=dict(q=query_string) if query_string else None, placeholder=_("Search promoted results")
            ),
        })