Skip to content

Commit

Permalink
[#176] Country and continent filters on Pages
Browse files Browse the repository at this point in the history
  • Loading branch information
KasperBrandt committed Apr 14, 2014
1 parent bdd356c commit de10d9a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
16 changes: 15 additions & 1 deletion akvo/rsr/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from akvo.rsr.iso3166 import CONTINENTS

from akvo.rsr.models import (
Organisation, Project, STATUSES, CURRENCY_CHOICES,
Organisation, Project, STATUSES, CURRENCY_CHOICES, Country
)

class CheckboxMultipleChoiceField(MultipleChoiceField):
Expand Down Expand Up @@ -84,6 +84,7 @@ def filter_by_budget_range(qs, what):

def __init__(self, *args, **kwargs):
organisation_id = kwargs.pop('organisation_id', None)
filtered_countries = kwargs.pop('filtered_countries', False)
super(ProjectFilterSet, self).__init__(*args, **kwargs)

self.filters['title'].field.widget.input_type = 'search'
Expand All @@ -95,6 +96,19 @@ def __init__(self, *args, **kwargs):

self.filters['locations__country'].extra.update({'empty_label': _(u'All countries')})

if filtered_countries:
countries = kwargs['queryset'].countries()

continents = [('', _(u'All continents'))]
choices = []
for country in countries:
if not country.continent_code in [code[0] for code in choices]:
choices.append((country.continent_code, _(country.continent)))
continents.extend(sorted(choices))

self.filters['continent'].extra.update({'choices': continents})
self.filters['locations__country'].extra.update({'queryset': kwargs['queryset'].countries()})

if organisation_id:
qs = Organisation.objects.get(pk=organisation_id).partners()
self.filters['organisation'].extra.update({'empty_label': _(u'All partners')})
Expand Down
5 changes: 5 additions & 0 deletions akvo/rsr/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,11 @@ def support_partners(self):
def all_partners(self):
return self._partners()

#returns a country queryset

This comment has been minimized.

Copy link
@zzgvh

zzgvh May 5, 2014

Contributor

Non-informative comment 😉 Better would be: "Returns a queryset of countries for this project" Also it can be added as a docsting in the method rather than a comment.

def countries(self):
return Country.objects.filter(id__in=ProjectLocation.objects.filter(project__in=self).
values_list('country', flat=True))

def __unicode__(self):
return u'%s' % self.title

Expand Down
3 changes: 2 additions & 1 deletion akvo/rsr/views_partner_sites/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,6 @@ def get_queryset(self):
return ProjectFilterSet(
self.request.GET.copy() or None,
queryset=projects,
organisation_id=self.request.organisation_id
organisation_id=self.request.organisation_id,
filtered_countries=True
)

0 comments on commit de10d9a

Please sign in to comment.