Skip to content

Commit

Permalink
Merge pull request #1937 from akvo/#1936-fix-primary-org
Browse files Browse the repository at this point in the history
[#1936] Change order for primary organisations; reporting org first

Code reviewed
  • Loading branch information
zzgvh committed Nov 30, 2015
2 parents 25fd359 + 96e731b commit 0f9e8bf
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
47 changes: 47 additions & 0 deletions akvo/rsr/migrations/0042_auto_20153011_1450.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# -*- coding: utf-8 -*-

from __future__ import unicode_literals

from django.db import models, migrations

from ..models.project import Project

def change_primary_organisations(apps, schema_editor):
Project = apps.get_model("rsr", "Project")

for project in Project.objects.all():
primary_organisation = None

# Pick the reporting org first
if project.partnerships.filter(iati_organisation_role=101):
primary_organisation = project.partnerships.filter(
iati_organisation_role=101)[0].organisation

# Otherwise, pick the partner that can publish the project
elif project.partners.filter(can_create_projects=True):
primary_organisation = project.partners.filter(can_create_projects=True)[0]

# Otherwise, grab the first accountable partner we find
elif project.partnerships.filter(iati_organisation_role=2):
primary_organisation = project.partnerships.filter(
iati_organisation_role=2)[0].organisation

# Panic mode: grab the first partner we find
elif project.partners.all():
primary_organisation = project.partners.all()[0]

project.primary_organisation = primary_organisation
project.save(update_fields=['primary_organisation'])


class Migration(migrations.Migration):

dependencies = [
('rsr', '0041_auto_20151116_1250'),
]

operations = [
migrations.RunPython(
change_primary_organisations,
),
]
8 changes: 4 additions & 4 deletions akvo/rsr/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -881,12 +881,12 @@ def find_primary_organisation(self):
"""
This method tries to return the "managing" partner organisation.
"""
# Pick the partner that can publish the project first
if self.publishing_orgs:
return self.publishing_orgs[0]
# Otherwise, if we have a reporting-org then we choose that
# Pick the reporting org first
if self.reporting_org:
return self.reporting_org
# Otherwise, pick the partner that can publish the project
if self.publishing_orgs:
return self.publishing_orgs[0]
# Otherwise, grab the first accountable partner we find
elif self.support_partners():
return self.support_partners()[0]
Expand Down

0 comments on commit 0f9e8bf

Please sign in to comment.