Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#176] Country filtering with improved performance #563

Merged
merged 2 commits into from
May 20, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions akvo/rsr/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions akvo/rsr/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down