Repository: akvo/akvo-rsr

Issue:
organisations don't show up in dropdown menu in project editor
Created via Reamaze:

Link: https://rsrsupport.reamaze.com/admin/conversations/organisations-dont-show-up-in-dropdown-menu-at-03-partners
Assignee: Anthony Gonzalez

Message:
Hi rsr-team,

Could you look into the following?

Some organisations in the rsr database don’t show up anymore in the dropdown menu at section 03 in the project editor.

For instance AWF:

https://rsr.akvo.org/en/admin/rsr/organisation/?q=awf &lt;https://rsr.akvo.org/en/admin/rsr/organisation/?q=awf&gt; It’s in our database and I can find it when I  look in  https://RSR.akvoapp.org/en/myrsr/project_editor/5994/:

But if I use https://iucn.akvoapp.org/en/myrsr/project_editor/5994/ &lt;https://iucn.akvoapp.org/en/myrsr/project_editor/5994/&gt;, it doesn’t show up (see screen shot below)




Discussion:
This bug was introduced while fixing #2576 

Resolving patch:
diff --git a/akvo/rest/views/typeahead.py b/akvo/rest/views/typeahead.py
--- a/akvo/rest/views/typeahead.py
+++ b/akvo/rest/views/typeahead.py
@@ -42,7 +42,12 @@ def typeahead_country(request):
 @api_view(['GET'])
 def typeahead_organisation(request):
     page = request.rsr_page
-    organisations = page.organisation.partners() if page else Organisation.objects.all()
+    if request.GET.get('partners', '0') == '1' and page:
+        organisations = page.partners()
+    else:
+        # Project editor - all organizations
+        organisations = Organisation.objects.all()
+
     return Response(
         rejig(organisations, TypeaheadOrganisationSerializer(organisations,
                                                              many=True))


Test patch:
diff --git a/akvo/rsr/tests/rest/test_typeahead.py b/akvo/rsr/tests/rest/test_typeahead.py
--- a/akvo/rsr/tests/rest/test_typeahead.py
+++ b/akvo/rsr/tests/rest/test_typeahead.py
@@ -17,8 +17,7 @@ class ProjectTypeaheadTest(TestCase):
 
     def setUp(self):
         super(ProjectTypeaheadTest, self).setUp()
-        organisation_name = 'Akvo'
-        organisation = Organisation.objects.create(name=organisation_name)
+        organisation = self._create_organisation('Akvo')
         PartnerSite.objects.create(
             organisation=organisation,
             piwik_id=1,
@@ -40,12 +39,19 @@ def setUp(self):
                     iati_organisation_role=Partnership.IATI_REPORTING_ORGANISATION
                 )
 
+        # Additional organisation for typeahead/organisations end-point
+        self._create_organisation('UNICEF')
+
     def _create_client(self, host=None):
         """ Create and return a client with the given host."""
         if not host:
             host = settings.RSR_DOMAIN
         return Client(HTTP_HOST=host)
 
+    def _create_organisation(self, name):
+        long_name = '{} organisation'.format(name)
+        return Organisation.objects.create(name=name, long_name=long_name)
+
     def test_published_projects_on_rsr_host(self):
         # Given
         url = '/rest/v1/typeaheads/projects?format=json&published=1'
@@ -95,3 +101,53 @@ def test_all_projects_on_partner_site(self):
         # Then
         self.assertEqual(response.status_code, 200)
         self.assertEqual(len(response.data['results']), 2)
+
+    def test_show_only_partner_orgs_on_partner_site(self):
+        # Given
+        url = '/rest/v1/typeaheads/organisations?format=json&partners=1'
+        host = 'akvo.{}'.format(settings.AKVOAPP_DOMAIN)
+        client = self._create_client(host)
+
+        # When
+        response = client.get(url)
+
+        # Then
+        self.assertEqual(response.status_code, 200)
+        self.assertEqual(len(response.data['results']), 1)
+
+    def test_show_all_orgs_on_partner_site(self):
+        # Given
+        url = '/rest/v1/typeaheads/organisations?format=json'
+        host = 'akvo.{}'.format(settings.AKVOAPP_DOMAIN)
+        client = self._create_client(host)
+
+        # When
+        response = client.get(url)
+
+        # Then
+        self.assertEqual(response.status_code, 200)
+        self.assertEqual(len(response.data['results']), 2)
+
+    def test_shows_all_orgs_on_rsr_site_when_only_partners_requested(self):
+        # Given
+        url = '/rest/v1/typeaheads/organisations?format=json&partners=1'
+        client = self._create_client()
+
+        # When
+        response = client.get(url)
+
+        # Then
+        self.assertEqual(response.status_code, 200)
+        self.assertEqual(len(response.data['results']), 2)
+
+    def test_show_all_orgs_on_rsr_site(self):
+        # Given
+        url = '/rest/v1/typeaheads/organisations?format=json'
+        client = self._create_client()
+
+        # When
+        response = client.get(url)
+
+        # Then
+        self.assertEqual(response.status_code, 200)
+        self.assertEqual(len(response.data['results']), 2)