Esempio n. 1
0
 def test_search_snippet_form_valid_submission(self):
     form_data = {'query': 'loop'}
     form = SearchSnippetForm(form_data)
     self.assertTrue(form.is_valid())
     self.assertFalse(form.errors)
     self.assertTrue(form.cleaned_data)
     cleaned_data = form.cleaned_data
     self.assertEquals(cleaned_data.get('query'), form_data['query'])
Esempio n. 2
0
def view_search_snippets(request, search_snippets_template):
    from quest.forms import SearchSnippetForm
    if request.method == 'POST':
        form = SearchSnippetForm({'query': request.POST.get('query')})
        if form.is_valid():
            query = form.cleaned_data.get('query')
            #TODO:Should also support tagbased search
            paginator = Paginator(Snippet.objects.filter(title__icontains=query).values('id', 'title', 'active', 'slug'), settings.PAGINATION_ITEMS_SIZE)
            try:
                page = int(request.GET.get('page', 1))
            except ValueError:
                page = 1
            try:
                snippets = paginator.page(page)
            except (EmptyPage, InvalidPage):
                snippets = paginator.page(paginator.num_pages)
            return response(request, search_snippets_template, {'form': form, 'snippets': snippets})
    else:
        form = SearchSnippetForm()
    return response(request, search_snippets_template, {'form': form, 'snippets': []})
Esempio n. 3
0
 def test_search_snippet_form_invalid_submission(self):
     form_data = {'query': ''}
     form = SearchSnippetForm(form_data)
     self.assertFalse(form.is_valid())
     self.assertTrue(form.errors)
     self.assertTrue(form.errors.get('query'))