diff --git a/akvo/rsr/filters.py b/akvo/rsr/filters.py index 599352b5ad..e87485eb78 100644 --- a/akvo/rsr/filters.py +++ b/akvo/rsr/filters.py @@ -14,9 +14,7 @@ from akvo.rsr.iso3166 import CONTINENTS -from akvo.rsr.models import ( - Organisation, Project, STATUSES, CURRENCY_CHOICES, -) +from akvo.rsr.models import Organisation, Project, STATUSES, CURRENCY_CHOICES class CheckboxMultipleChoiceField(MultipleChoiceField): widget = widgets.CheckboxSelectMultiple @@ -96,7 +94,21 @@ def __init__(self, *args, **kwargs): self.filters['locations__country'].extra.update({'empty_label': _(u'All countries')}) if organisation_id: - qs = Organisation.objects.get(pk=organisation_id).partners() + org = Organisation.objects.get(pk=organisation_id) + + countries = org.countries_where_active() + + 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': countries}) + + qs = org.partners() self.filters['organisation'].extra.update({'empty_label': _(u'All partners')}) else: qs = Organisation.objects.all() diff --git a/akvo/rsr/models.py b/akvo/rsr/models.py index 53140daecd..786174a7ec 100644 --- a/akvo/rsr/models.py +++ b/akvo/rsr/models.py @@ -461,6 +461,13 @@ def partners(self): "returns a queryset of all organisations that self has at least one project in common with, excluding self" return self.published_projects().all_partners().exclude(id__exact=self.id) + def countries_where_active(self): + """Returns a Country queryset of countries where this organisation has published projects.""" + return Country.objects.filter( + projectlocation__project__partnerships__organisation=self, + projectlocation__project__publishingstatus__status='published' + ).distinct() + # New API def euros_pledged(self):