Skip to content

Commit 8f064f7

Browse files
committed
fixed empty user search
1 parent 51c9512 commit 8f064f7

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

anthill/people/forms.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ class SearchForm(forms.Form):
1919
location_range = forms.ChoiceField(choices=DISTANCE_CHOICES, initial='50',
2020
required=False)
2121

22+
def clean(self):
23+
cleaned_data = self.cleaned_data
24+
if not (cleaned_data['location'] or cleaned_data['skills'] or
25+
cleaned_data['name'] or cleaned_data['position']):
26+
raise forms.ValidationError("No search parameters specified")
27+
return cleaned_data
28+
29+
2230
class ProfileForm(forms.Form):
2331
name = forms.CharField(label='Name', required=False)
2432
email = forms.CharField(label='Email')

anthill/people/views.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,29 @@
1010
from anthill.people.forms import SearchForm, ProfileForm, PasswordForm, UserContactForm
1111

1212
def search(request):
13+
context = { 'form': SearchForm() }
14+
1315
if request.GET:
1416
form = SearchForm(request.GET)
15-
form.is_valid()
16-
location = form.cleaned_data['location']
17-
skills = form.cleaned_data['skills']
18-
name = form.cleaned_data['name']
19-
position = form.cleaned_data['position']
20-
location_range = form.cleaned_data['location_range']
17+
if form.is_valid():
18+
location = form.cleaned_data['location']
19+
skills = form.cleaned_data['skills']
20+
name = form.cleaned_data['name']
21+
position = form.cleaned_data['position']
22+
location_range = form.cleaned_data['location_range']
2123

22-
users = Profile.objects.all().select_related().exclude(user__id=request.user.id)
23-
if skills:
24-
tags = [t.strip() for t in skills.split(',')]
25-
for tag in tags:
26-
users = users.filter(skills__icontains=tag)
27-
if position:
28-
users = users.filter(role=position)
29-
if name:
30-
users = users.filter(user__first_name__icontains=name)
31-
if location:
32-
users = users.search_by_distance(location, location_range)
33-
context = { 'form': form, 'searched': True, 'search_results': users }
34-
else:
35-
context = { 'form': SearchForm() }
24+
users = Profile.objects.all().select_related().exclude(user__id=request.user.id)
25+
if skills:
26+
tags = [t.strip() for t in skills.split(',')]
27+
for tag in tags:
28+
users = users.filter(skills__icontains=tag)
29+
if position:
30+
users = users.filter(role=position)
31+
if name:
32+
users = users.filter(user__first_name__icontains=name)
33+
if location:
34+
users = users.search_by_distance(location, location_range)
35+
context = { 'form': form, 'searched': True, 'search_results': users }
3636

3737
return render_to_response('people/search.html', context,
3838
context_instance=RequestContext(request))

0 commit comments

Comments
 (0)