Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow empty search queries r=jsocol

- Fix bug 746688
- form still needs to be valid to see users. In other words, going to
  search/ with no args shoes the "enter a query" screen
  • Loading branch information...
commit 2ecfe12b4e85aec25e620e5d7eb2f91d2395674c 1 parent 005e22e
@tallowen authored
View
2  apps/phonebook/forms.py
@@ -20,7 +20,7 @@
class SearchForm(happyforms.Form):
- q = forms.CharField(widget=forms.HiddenInput, required=True)
+ q = forms.CharField(widget=forms.HiddenInput, required=False)
limit = forms.CharField(widget=forms.HiddenInput, required=False)
nonvouched_only = forms.BooleanField(label=_lazy(u'Non Vouched Only'),
required=False)
View
16 apps/phonebook/tests/test_search.py
@@ -3,11 +3,14 @@
from django.conf import settings
from nose.tools import eq_
+from pyquery import PyQuery as pq
from common.tests import ESTestCase
from elasticutils import get_es
from funfactory.urlresolvers import reverse
+from users.models import UserProfile
+
class TestSearch(ESTestCase):
def test_search_with_space(self):
@@ -134,3 +137,16 @@ def test_mozillian_search_pagination(self):
limit='-3'))
peeps = r.context['people']
self.assertEqual(len(peeps), 2)
+
+ def test_empty_query_search(self):
+ """Make sure the search method works with an empty query"""
+ assert UserProfile.search('').count()
+
+ def test_proper_url_arg_handling(self):
+ search_url = reverse('search')
+ r = self.mozillian_client.get(search_url)
+ assert not pq(r.content)('.result')
+
+ r = self.mozillian_client.get(search_url,
+ dict(q=u'', nonvouched_only=1))
+ assert pq(r.content)('.result')
View
26 apps/phonebook/views.py
@@ -142,26 +142,28 @@ def search(request):
form = forms.SearchForm(request.GET)
if form.is_valid():
- query = form.cleaned_data.get('q', '')
+ query = form.cleaned_data.get('q', u'')
limit = form.cleaned_data['limit']
vouched = False if form.cleaned_data['nonvouched_only'] else None
profilepic = True if form.cleaned_data['picture_only'] else None
page = request.GET.get('page', 1)
- profiles = UserProfile.search(query, vouched=vouched, photo=profilepic)
+ # if nothing has been entered don't load any searches.
+ if not (not query and vouched is None and profilepic is None):
+ profiles = UserProfile.search(query, vouched=vouched, photo=profilepic)
- paginator = Paginator(profiles, limit)
+ paginator = Paginator(profiles, limit)
- try:
- people = paginator.page(page)
- except PageNotAnInteger:
- people = paginator.page(1)
- except EmptyPage:
- people = paginator.page(paginator.num_pages)
+ try:
+ people = paginator.page(page)
+ except PageNotAnInteger:
+ people = paginator.page(1)
+ except EmptyPage:
+ people = paginator.page(paginator.num_pages)
- if paginator.count > forms.PAGINATION_LIMIT:
- show_pagination = True
- num_pages = len(people.paginator.page_range)
+ if paginator.count > forms.PAGINATION_LIMIT:
+ show_pagination = True
+ num_pages = len(people.paginator.page_range)
d = dict(people=people,
form=form,
View
13 apps/users/models.py
@@ -152,10 +152,9 @@ def fields(self):
attrs = ('username', 'first_name', 'last_name', 'email', 'last_login',
'date_joined')
d.update(dict((a, getattr(self.user, a)) for a in attrs))
- # Index group ids... for fun.
- groups = list(self.groups.values_list('name', flat=True))
- d.update(dict(groups=groups))
d.update(dict(has_photo=bool(self.photo)))
+ # Index groups and skills ... for fun.
+ d.update(dict(groups=list(self.groups.values_list('name', flat=True))))
return d
@classmethod
@@ -166,8 +165,12 @@ def search(cls, query, vouched=None, photo=None):
'username__text', 'bio__text', 'website__text',
'email__text', 'groups__text', 'first_name__startswith',
'last_name__startswith', 'ircname')
- q = dict((field, query) for field in fields)
- s = S(cls).query(or_=q)
+ if query:
+ q = dict((field, query) for field in fields)
+ s = S(cls).query(or_=q)
+ else:
+ s = S(cls)
+
if vouched is not None:
s = s.filter(is_vouched=vouched)
if photo is not None:
Please sign in to comment.
Something went wrong with that request. Please try again.