Skip to content
This repository has been archived by the owner on Jan 19, 2021. It is now read-only.

Commit

Permalink
[Bug 1048844] Implement vouched filter in /betasearch using django-fi…
Browse files Browse the repository at this point in the history
…lter.
  • Loading branch information
chromano authored and glogiotatidis committed Aug 5, 2014
1 parent c670432 commit d660313
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
29 changes: 29 additions & 0 deletions mozillians/phonebook/forms.py
Expand Up @@ -8,6 +8,7 @@
from django.core.files.uploadedfile import UploadedFile
from django.forms.models import BaseInlineFormSet, inlineformset_factory

import django_filters
import happyforms
from PIL import Image
from tower import ugettext as _, ugettext_lazy as _lazy
Expand Down Expand Up @@ -70,6 +71,34 @@ def clean_limit(self):
return limit


def filter_vouched(qs, choice):
if choice == SearchFilter.CHOICE_ONLY_VOUCHED:
return qs.filter(is_vouched=True)
elif choice == SearchFilter.CHOICE_ONLY_UNVOUCHED:
return qs.filter(is_vouched=False)
return qs


class SearchFilter(django_filters.FilterSet):
CHOICE_ONLY_VOUCHED = 'yes'
CHOICE_ONLY_UNVOUCHED = 'no'
CHOICE_ALL = 'all'

CHOICES = (
(CHOICE_ONLY_VOUCHED, _lazy('Vouched')),
(CHOICE_ONLY_UNVOUCHED, _lazy('Unvouched')),
(CHOICE_ALL, _lazy('All')),
)

vouched = django_filters.ChoiceFilter(
name='vouched', label=_lazy('Display only'), required=False,
choices=CHOICES, action=filter_vouched)

class Meta:
model = UserProfile
fields = []


class UserForm(happyforms.ModelForm):
"""Instead of just inhereting form a UserProfile model form, this
base class allows us to also abstract over methods that have to do
Expand Down
9 changes: 6 additions & 3 deletions mozillians/phonebook/views.py
Expand Up @@ -304,20 +304,22 @@ def betasearch(request):
people = []
show_pagination = False
form = forms.SearchForm(request.GET)
filtr = forms.SearchFilter(request.GET)
groups = None
functional_areas = None

if form.is_valid():
query = form.cleaned_data.get('q', u'')
limit = form.cleaned_data['limit']
include_non_vouched = form.cleaned_data['include_non_vouched']
page = request.GET.get('page', 1)
functional_areas = Group.get_functional_areas()
public = not (request.user.is_authenticated()
and request.user.userprofile.is_vouched)

profiles = UserProfile.search(query, public=public,
include_non_vouched=include_non_vouched)
profiles_matching_filter = list(filtr.qs.values_list('id', flat=True))
profiles = UserProfile.search(query, include_non_vouched=True, public=public)
profiles = profiles.filter(id__in=profiles_matching_filter)

if not public:
groups = Group.search(query)

Expand All @@ -337,6 +339,7 @@ def betasearch(request):

d = dict(people=people,
search_form=form,
filtr=filtr,
limit=limit,
show_pagination=show_pagination,
groups=groups,
Expand Down
9 changes: 5 additions & 4 deletions mozillians/templates/phonebook/betasearch.html
Expand Up @@ -41,10 +41,11 @@ <h1>{{ _('Beta Search') }}</h1>
{{ _('Advanced Options') }}
</button>
<div class="search-options">
<div class="field">
{{ search_form.include_non_vouched }}
{{ search_form.include_non_vouched.label_tag() }}
</div>
{% for field in filtr.form %}
<div class="field">
{{ mozillians_field(field) }}
</div>
{% endfor %}
</div>
</form>
{% if not search_form.cleaned_data %}
Expand Down

0 comments on commit d660313

Please sign in to comment.