Skip to content

Commit

Permalink
[#725, #630] Fix breaking of partner sites
Browse files Browse the repository at this point in the history
Work around the SQL query that hangs by assembling the list of countries
in python instead.

Use prefetch_related() to (hopefully) minimize the number of queries
needed.
  • Loading branch information
zzgvh committed Aug 28, 2014
1 parent 2a9ccd5 commit 1ab17c9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
8 changes: 7 additions & 1 deletion akvo/rsr/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,13 @@ def all_partners(self):

def countries(self):
"""Returns a Country queryset of the countries of these projects"""
return Country.objects.filter(projectlocation__project__in=self).distinct()
country_ids = []
for project in self:
for location in project.locations.all():
country_ids.append(location.country.id)

country_ids = list(set(country_ids))
return Country.objects.filter(id__in=country_ids).distinct()


def __unicode__(self):
Expand Down
7 changes: 4 additions & 3 deletions akvo/rsr/views_partner_sites/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ def get_context_data(self, **kwargs):

def get_queryset(self):
partner_site = self.request.partner_site

# Check if only projects of the partner should be shown or all projects
if partner_site.partner_projects:
projects = get_object_or_404(Organisation, pk=self.request.organisation_id).published_projects()
projects = get_object_or_404(
Organisation, pk=self.request.organisation_id
).published_projects().prefetch_related('locations')
else:
projects = Project.objects.all().published()
projects = Project.objects.all().published().prefetch_related('locations')

# Check if keywords have been specified for the partner site and filter projects based on keywords if so
if partner_site.keywords.all():
Expand Down

0 comments on commit 1ab17c9

Please sign in to comment.