Skip to content

Commit

Permalink
[#2540] Prefetch budget_items for projects
Browse files Browse the repository at this point in the history
And optimize check for multiple currencies in a project
  • Loading branch information
punchagan committed Feb 23, 2017
1 parent 99a6192 commit d8e4271
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
14 changes: 7 additions & 7 deletions akvo/rsr/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -979,13 +979,13 @@ 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
# Using a python loop for iteration, because it's faster when
# budget_items have been pre-fetched
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)
Expand Down
3 changes: 2 additions & 1 deletion akvo/rsr/views/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit d8e4271

Please sign in to comment.