From de10d9a0498e620931b01266009948680e395fb1 Mon Sep 17 00:00:00 2001 From: Kasper Brandt Date: Mon, 14 Apr 2014 12:06:48 +0200 Subject: [PATCH] [#176] Country and continent filters on Pages --- akvo/rsr/filters.py | 16 +++++++++++++++- akvo/rsr/models.py | 5 +++++ akvo/rsr/views_partner_sites/base.py | 3 ++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/akvo/rsr/filters.py b/akvo/rsr/filters.py index 599352b5ad..aa1143c309 100644 --- a/akvo/rsr/filters.py +++ b/akvo/rsr/filters.py @@ -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): @@ -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' @@ -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')}) diff --git a/akvo/rsr/models.py b/akvo/rsr/models.py index 71db074d04..0001b9ac5f 100644 --- a/akvo/rsr/models.py +++ b/akvo/rsr/models.py @@ -999,6 +999,11 @@ def support_partners(self): def all_partners(self): return self._partners() + #returns a country queryset + 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 diff --git a/akvo/rsr/views_partner_sites/base.py b/akvo/rsr/views_partner_sites/base.py index 43ab954578..fddf2629e4 100644 --- a/akvo/rsr/views_partner_sites/base.py +++ b/akvo/rsr/views_partner_sites/base.py @@ -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 )