def test_search_form(self): form = PackageSearchForm(data={ 'query': 'unique_package' }) self.failUnless(form.is_valid()) results = form.search() self.assertEquals(results.count(), 1) self.assertEquals(results[0].name, 'unique_package')
def search(request, query=''): if request.method == 'GET' and request.GET.has_key('query'): form = PackageSearchForm(request.GET) # If there are any errors in the forum, render the template and exit if not form.is_valid(): return render_to_response( 'aur/search.html', { 'form': form, 'user': request.user, 'is_moderator': _user_is_moderator(request.user), }) else: form = PackageSearchForm() # Execute the search results = form.search() # Get sorting variables from query string or fallback on defaults if request.GET.has_key('sortby'): sortby = request.GET['sortby'] if sortby == 'maintainer': sortby = 'name' else: sortby = 'name' if request.GET.has_key('order') and request.GET['order'] == 'desc': sortby = "".join(('-', sortby)) # Sort the results results = results.order_by(sortby, 'repository', 'name') # If we only got one hit, just go to the package's detail page if form.is_bound and results.count() == 1: return HttpResponseRedirect( reverse('aur-package_detail', args=[ results[0].name, ])) # Initialise the pagination paginator = Paginator(results, int(form.get_or_default('limit'))) # Use last page if page number is out of range try: page = paginator.page(int(request.GET.get('page', '1'))) except (EmptyPage, InvalidPage): page = paginator.page(paginator.num_pages) return render_to_response( 'aur/search.html', { 'form': form, 'packages': page.object_list, 'page': page, 'user': request.user, 'request': request, 'is_moderator': _user_is_moderator(request.user), })
def search(request, query = ''): if request.method == 'GET' and request.GET.has_key('query'): form = PackageSearchForm(request.GET) # If there are any errors in the forum, render the template and exit if not form.is_valid(): return render_to_response('aur/search.html', { 'form': form, 'user': request.user, 'is_moderator': _user_is_moderator(request.user), }) else: form = PackageSearchForm() # Execute the search results = form.search() # Get sorting variables from query string or fallback on defaults if request.GET.has_key('sortby'): sortby = request.GET['sortby'] if sortby == 'maintainer': sortby = 'name' else: sortby = 'name' if request.GET.has_key('order') and request.GET['order'] == 'desc': sortby = "".join(('-', sortby)) # Sort the results results = results.order_by(sortby, 'repository', 'name') # If we only got one hit, just go to the package's detail page if form.is_bound and results.count() == 1: return HttpResponseRedirect(reverse('aur-package_detail', args=[results[0].name,])) # Initialise the pagination paginator = Paginator(results, int(form.get_or_default('limit'))) # Use last page if page number is out of range try: page = paginator.page(int(request.GET.get('page', '1'))) except (EmptyPage, InvalidPage): page = paginator.page(paginator.num_pages) return render_to_response('aur/search.html', { 'form': form, 'packages': page.object_list, 'page': page, 'user': request.user, 'request': request, 'is_moderator': _user_is_moderator(request.user), })
def test_search_form(self): form = PackageSearchForm(data={'query': 'unique_package'}) self.failUnless(form.is_valid()) results = form.search() self.assertEquals(results.count(), 1) self.assertEquals(results[0].name, 'unique_package')