Repository: akvo/akvo-rsr

Issue:
Adding organisation via PE
https://trello.com/c/zII0ZO0m/27-adding-organisation-via-pe


Discussion:

Resolving patch:
diff --git a/akvo/rest/views/organisation.py b/akvo/rest/views/organisation.py
--- a/akvo/rest/views/organisation.py
+++ b/akvo/rest/views/organisation.py
@@ -17,7 +17,7 @@
 from akvo.rest.views.utils import int_or_none, get_qs_elements_for_page
 from akvo.rsr.filters import location_choices, get_m49_filter
 from akvo.rsr.models import Organisation, Country
-from ..serializers import OrganisationSerializer, OrganisationDirectorySerializer
+from ..serializers import OrganisationSerializer, OrganisationDirectorySerializer, OrganisationLocationSerializer
 from ..viewsets import BaseRSRViewSet
 
 
@@ -75,6 +75,23 @@ class OrganisationViewSet(BaseRSRViewSet):
     serializer_class = OrganisationSerializer
     parser_classes = [AkvoOrganisationParser] + api_settings.DEFAULT_PARSER_CLASSES
 
+    def create(self, request, *args, **kwargs):
+        response = super(OrganisationViewSet, self).create(request, *args, **kwargs)
+        if 'latitude' in request.data and 'longitude' in request.data:
+            data = request.data.copy()
+            organisation_id = response.data['id']
+            data['location_target'] = organisation_id
+            serializer = OrganisationLocationSerializer(data=data)
+            try:
+                serializer.is_valid(raise_exception=True)
+            except Exception:
+                # Delete organisation if location could not be created
+                Organisation.objects.get(id=organisation_id).delete()
+                raise
+            else:
+                self.perform_create(serializer)
+        return response
+
 
 @api_view(['GET'])
 def organisation_directory(request):


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
@@ -1041,7 +1041,14 @@ def test_create_new_organisation(self):
         self.c.login(username=self.username, password=self.password)
         url = '/rest/v1/organisation/?format=json'
         content_type = 'application/x-www-form-urlencoded'
-        data = {'name': 'Test Org', 'long_name': 'Test Organisation'}
+        data = {'name': 'Test Org',
+                'long_name': 'Test Organisation',
+                'new_organisation_type': 10,
+                'url': 'http://example.com',
+                'latitude': 12.9,
+                'longitude': 77.5,
+                'city': 'Bangalore',
+                'iati_country': 'IN'}
 
         # When
         response = self.c.post(url, data=urlencode(data), content_type=content_type)
@@ -1049,8 +1056,34 @@ def test_create_new_organisation(self):
         # Then
         self.assertEqual(response.status_code, 201)
         self.assertIn('id', response.data)
-        for key in data:
+        for key in {'name', 'long_name', 'url', 'new_organisation_type', 'url'}:
             self.assertEqual(response.data[key], data[key])
+        org = Organisation.objects.get(id=response.data['id'])
+        self.assertEqual(org.primary_location.latitude, data['latitude'])
+        self.assertEqual(org.primary_location.longitude, data['longitude'])
+        self.assertEqual(org.primary_location.city, data['city'])
+
+    def test_create_new_organisation_without_primary_location(self):
+        # Given
+        self.c.login(username=self.username, password=self.password)
+        url = '/rest/v1/organisation/?format=json'
+        content_type = 'application/x-www-form-urlencoded'
+        data = {'name': 'Test Org',
+                'long_name': 'Test Organisation',
+                'new_organisation_type': 10,
+                'url': 'http://example.com',
+                'iati_country': 'IN'}
+
+        # When
+        response = self.c.post(url, data=urlencode(data), content_type=content_type)
+
+        # Then
+        self.assertEqual(response.status_code, 201)
+        self.assertIn('id', response.data)
+        for key in {'name', 'long_name', 'url', 'new_organisation_type', 'url'}:
+            self.assertEqual(response.data[key], data[key])
+        org = Organisation.objects.get(id=response.data['id'])
+        self.assertIsNone(org.primary_location)
 
 
 class ProjectUpdateTestCase(BaseTestCase):