From 3853a7c4d5755cfe5413d535454a484e69bc8c78 Mon Sep 17 00:00:00 2001 From: Puneeth Chaganti Date: Fri, 17 Feb 2017 12:31:01 +0530 Subject: [PATCH] [#2540] Prefetch budget_items for projects And optimize check for multiple currencies in a project --- akvo/rsr/models/project.py | 10 +++------- akvo/rsr/views/project.py | 3 ++- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/akvo/rsr/models/project.py b/akvo/rsr/models/project.py index ad2b9d5481..2b5a6abfc4 100644 --- a/akvo/rsr/models/project.py +++ b/akvo/rsr/models/project.py @@ -979,13 +979,9 @@ def budget_total(self): return Project.objects.budget_total().get(pk=self.pk).budget_total def has_multiple_budget_currencies(self): - budget_items = BudgetItem.objects.filter(project__id=self.pk) - num_currencies = len(set([self.currency] + [c.currency if c.currency else self.currency for c in budget_items])) - - if num_currencies > 1: - return True - else: - return False + budget_items = self.budget_items.all() + num_currencies = len(set([self.currency] + [c.currency for c in budget_items if c.currency])) + return num_currencies > 1 def budget_currency_totals(self): budget_items = BudgetItem.objects.filter(project__id=self.pk) diff --git a/akvo/rsr/views/project.py b/akvo/rsr/views/project.py index 9d3d284777..ea53c61a78 100644 --- a/akvo/rsr/views/project.py +++ b/akvo/rsr/views/project.py @@ -112,7 +112,8 @@ def directory(request): page.object_list = page.object_list.prefetch_related( 'publishingstatus', 'recipient_countries', - 'sectors' + 'sectors', + 'budget_items', ).select_related( 'primary_organisation', 'last_update'