Skip to content

Commit

Permalink
[#224] Fix missing benchmarks bug
Browse files Browse the repository at this point in the history
Some benchmarks are not created because of identical Benchmark names in
several categories and code relying on the uniqeness of same.

Fix by using the business_unit_categories lookup dict to assign the
correct category to a benchmark.
  • Loading branch information
zzgvh committed Aug 1, 2013
1 parent a57bf36 commit 4fe43ce
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions akvo/api/resources/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,6 @@ def put_detail(self, request, **kwargs):
super(IATIProjectResource, self).put_detail(request, **kwargs)

def alter_deserialized_detail_data(self, request, data):
# prepare the benchmarks, looking up the Benchmarkname and the Category objects.
# if we don't find a value drop that benchmark
benchmarks = []
for benchmark in data['benchmarks']:
if benchmark.get('value'):
new_benchmark = dict(value=benchmark['value'])
benchmarkname = Benchmarkname.objects.get(name=benchmark['name'])
new_benchmark['name'] = benchmarkname.pk
new_benchmark['category'] = benchmarkname.category_set.all()[0].pk # assumes there's only one category
benchmarks.append(new_benchmark)
data['benchmarks'] = benchmarks

# Figure out the category for the project from the business unit
business_unit_categories = {
"K6020": dict(cat_name="Children & Education", fa="Education"),
Expand All @@ -133,6 +121,18 @@ def alter_deserialized_detail_data(self, request, data):
project_category = Category.objects.get(name=business_unit['cat_name'], focus_area__name=business_unit['fa'])
data['categories'] = ['/api/v1/iati_category/{pk}/'.format(pk=project_category.pk),]

# prepare the benchmarks, looking up the Benchmarkname and set the Category to project_category
benchmarks = []
for benchmark in data['benchmarks']:
# if we don't find a value drop that benchmark
if benchmark.get('value'):
new_benchmark = dict(value=benchmark['value'])
benchmarkname = Benchmarkname.objects.get(name=benchmark['name'])
new_benchmark['name'] = benchmarkname.pk
new_benchmark['category'] = project_category.pk
benchmarks.append(new_benchmark)
data['benchmarks'] = benchmarks

# hack to set the first location as primary
if data.get('locations'):
data['locations'][0]['primary'] = True
Expand Down

0 comments on commit 4fe43ce

Please sign in to comment.