Repository: akvo/akvo-rsr

Issue:
Show OrganisationIndicatorLabels belonging to all partners
The organisation with indicator labels defined, may not always be the primary organisation.  Currently, as an interim solution we display labels defined by all organisations. 

When there are multiple organizations with lots of labels (and possibly some of them very similar), this can turn out to be a problem. 


Discussion:

Resolving patch:
diff --git a/akvo/rsr/models/organisation_indicator_label.py b/akvo/rsr/models/organisation_indicator_label.py
--- a/akvo/rsr/models/organisation_indicator_label.py
+++ b/akvo/rsr/models/organisation_indicator_label.py
@@ -23,6 +23,7 @@ class Meta:
         verbose_name = _(u'organisation indicator label')
         verbose_name_plural = _(u'organisation indicator labels')
         unique_together = ('organisation', 'label')
+        ordering = ('organisation', 'label')
 
     def __unicode__(self):
         return self.label
diff --git a/akvo/rsr/templatetags/project_editor.py b/akvo/rsr/templatetags/project_editor.py
--- a/akvo/rsr/templatetags/project_editor.py
+++ b/akvo/rsr/templatetags/project_editor.py
@@ -259,8 +259,8 @@ def choices_and_ids(model, *fields):
         else:
             project = obj.indicator.result.project
         organisation_indicator_labels = get_model('rsr', 'OrganisationIndicatorLabel').objects.filter(
-            organisation=project.primary_organisation
-        )
+            organisation=project.all_partners()
+        ).distinct()
         return choices_and_ids(organisation_indicator_labels, 'id', 'label')
 
 


Test patch:
diff --git a/akvo/rsr/tests/rest/test_project_editor.py b/akvo/rsr/tests/rest/test_project_editor.py
--- a/akvo/rsr/tests/rest/test_project_editor.py
+++ b/akvo/rsr/tests/rest/test_project_editor.py
@@ -407,8 +407,8 @@ def test_indicator_label_choices(self):
             {(label1.pk, label1.label), (label2.pk, label2.label)}
         )
         self.assertEqual(
-            ids,
-            [1, 2]
+            set(ids),
+            {label1.pk, label2.pk}
         )
 
         # When
@@ -419,7 +419,60 @@ def test_indicator_label_choices(self):
             set(labels),
             {(label1.pk, label1.label), (label2.pk, label2.label)}
         )
+        self.assertEqual(
+            set(ids),
+            {label1.pk, label2.pk}
+        )
+
+    def test_indicator_label_choices_multiple_organisations(self):
+        # Given
+        organisation1 = Organisation.objects.create(
+            name='name1', long_name='long name1', can_create_projects=True
+        )
+        organisation2 = Organisation.objects.create(
+            name='name2', long_name='long name2', can_create_projects=True
+        )
+        Partnership.objects.create(
+            organisation=organisation1, project=self.project,
+            iati_organisation_role=Partnership.IATI_ACCOUNTABLE_PARTNER
+        )
+        Partnership.objects.create(
+            organisation=organisation2, project=self.project,
+            iati_organisation_role=Partnership.IATI_ACCOUNTABLE_PARTNER
+        )
+        label1 = OrganisationIndicatorLabel.objects.create(
+            organisation=organisation1, label=u'label 1'
+        )
+        label2 = OrganisationIndicatorLabel.objects.create(
+            organisation=organisation2, label=u'label 2'
+        )
+
+        result = Result.objects.create(project=self.project, title="Result #1", type="1", )
+        indicator = Indicator.objects.create(result=result, title="Indicator #1", measure="1", )
+        indicator_label = IndicatorLabel.objects.create(indicator=indicator, label=label1)
+
+        # When
+        labels, ids = choices(indicator_label, 'label')
+
+        # Then
+        self.assertEqual(
+            list(labels),
+            [(label1.pk, label1.label), (label2.pk, label2.label)]
+        )
+        self.assertEqual(
+            ids,
+            [label1.pk, label2.pk]
+        )
+
+        # When
+        labels, ids = choices('IndicatorLabel.{}_1234_567_89'.format(self.project.pk), 'label')
+
+        # Then
+        self.assertEqual(
+            list(labels),
+            [(label1.pk, label1.label), (label2.pk, label2.label)]
+        )
         self.assertEqual(
             ids,
-            [1, 2]
+            [label1.pk, label2.pk]
         )