Repository: akvo/akvo-rsr

Issue:
/related_project/ issue
Attempted to POST the following data:
{"relation":"1","project":"8012","related_iati_id":"23232"}

The response is 400:
`related_project: ["This field is required."]`

However, we want to allow a selection of external project:
<img width="1440" alt="Screenshot 2019-08-16 10 16 11" src="https://user-images.githubusercontent.com/1336477/63150230-effe3400-c00e-11e9-8aff-dc162686751a.png">



Discussion:

Resolving patch:
diff --git a/akvo/rest/serializers/related_project.py b/akvo/rest/serializers/related_project.py
--- a/akvo/rest/serializers/related_project.py
+++ b/akvo/rest/serializers/related_project.py
@@ -5,7 +5,7 @@
 # For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.
 
 
-from akvo.rsr.models import RelatedProject
+from akvo.rsr.models import RelatedProject, Project
 
 from .rsr_serializer import BaseRSRSerializer
 
@@ -14,6 +14,9 @@
 
 class RelatedProjectRawSerializer(BaseRSRSerializer):
 
+    related_project = serializers.PrimaryKeyRelatedField(
+        allow_null=True, queryset=Project.objects.all(), required=False, default=None)
+
     class Meta:
         model = RelatedProject
         fields = '__all__'


Test patch:
diff --git a/akvo/rsr/tests/rest/test_related_project.py b/akvo/rsr/tests/rest/test_related_project.py
new file mode 100644
--- /dev/null
+++ b/akvo/rsr/tests/rest/test_related_project.py
@@ -0,0 +1,92 @@
+# -*- coding: utf-8 -*-
+
+"""
+Akvo RSR is covered by the GNU Affero General Public License.
+
+See more details in the license.txt file located at the root folder of the Akvo RSR module.
+For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.
+"""
+
+from akvo.rsr.tests.base import BaseTestCase
+from akvo.rsr.models import RelatedProject
+
+
+class RelatedProjectTestCase(BaseTestCase):
+
+    def setUp(self):
+        super(RelatedProjectTestCase, self).setUp()
+        self.project = self.create_project('Project')
+        self.email = 'foo@bar.com'
+        user = self.create_user(self.email, self.email)
+        org = self.create_organisation('Bar.com')
+        self.make_employment(user, org, 'Admins')
+        self.make_partner(self.project, org)
+        self.c.login(username=self.email, password=self.email)
+
+    def test_get_related_project(self):
+        # Given
+        project = self.create_project('Project 2')
+        RelatedProject.objects.create(project=self.project, related_project=project)
+
+        url = '/rest/v1/related_project/?format=json&project={}'.format(self.project.id)
+
+        # When
+        response = self.c.get(url)
+
+        # Then
+        self.assertEqual(response.status_code, 200)
+        self.assertEqual(response.data['results'][0]['project'], self.project.id)
+        self.assertEqual(response.data['results'][0]['related_project'], project.id)
+        self.assertEqual(response.data['results'][0]['related_iati_id'], '')
+
+    def test_get_related_iati_id(self):
+        # Given
+        iati_id = 'DUMMY_IATI_ID'
+        RelatedProject.objects.create(project=self.project, related_iati_id=iati_id)
+
+        url = '/rest/v1/related_project/?format=json&project={}'.format(self.project.id)
+
+        # When
+        response = self.c.get(url)
+
+        # Then
+        self.assertEqual(response.status_code, 200)
+        self.assertEqual(response.data['results'][0]['project'], self.project.id)
+        self.assertIsNone(response.data['results'][0]['related_project'])
+        self.assertEqual(response.data['results'][0]['related_iati_id'], iati_id)
+
+    def test_post_related_project(self):
+        # Given
+        project = self.create_project('Project 2')
+        data = {
+            'project': self.project.id,
+            'related_project': project.id
+        }
+        url = '/rest/v1/related_project/?format=json'
+
+        # When
+        response = self.c.post(url, data=data)
+
+        # Then
+        self.assertEqual(response.status_code, 201)
+        self.assertEqual(response.data['project'], self.project.id)
+        self.assertEqual(response.data['related_project'], project.id)
+        self.assertEqual(response.data['related_iati_id'], '')
+
+    def test_post_related_iati_id(self):
+        # Given
+        iati_id = 'DUMMY_IATI_ID'
+        data = {
+            'project': self.project.id,
+            'related_iati_id': iati_id,
+        }
+        url = '/rest/v1/related_project/?format=json'
+
+        # When
+        response = self.c.post(url, data=data)
+
+        # Then
+        self.assertEqual(response.status_code, 201)
+        self.assertEqual(response.data['project'], self.project.id)
+        self.assertIsNone(response.data['related_project'])
+        self.assertEqual(response.data['related_iati_id'], iati_id)