From a58a750fb1ff49294098ed0e11f6ef6fd10f33c1 Mon Sep 17 00:00:00 2001 From: FrankApiyo Date: Sat, 24 Aug 2024 18:06:23 +0300 Subject: [PATCH 1/2] Fix exception thrown when updating organization profile --- .../api/tests/viewsets/test_organization_profile_viewset.py | 4 ++-- onadata/libs/serializers/organization_serializer.py | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/onadata/apps/api/tests/viewsets/test_organization_profile_viewset.py b/onadata/apps/api/tests/viewsets/test_organization_profile_viewset.py index 2925786a03..358a2cc7a1 100644 --- a/onadata/apps/api/tests/viewsets/test_organization_profile_viewset.py +++ b/onadata/apps/api/tests/viewsets/test_organization_profile_viewset.py @@ -1054,8 +1054,8 @@ def test_put_role_user_none_existent(self): def test_update_org_name(self): self._org_create() - # update name - data = {"name": "Dennis2"} + # update name and email + data = {"name": "Dennis2", "email": "dennis@mail.com"} request = self.factory.patch("/", data=data, **self.extra) response = self.view(request, user="denoinc") self.assertEqual(response.data["name"], "Dennis2") diff --git a/onadata/libs/serializers/organization_serializer.py b/onadata/libs/serializers/organization_serializer.py index ca1f0f006f..804b3bdecf 100644 --- a/onadata/libs/serializers/organization_serializer.py +++ b/onadata/libs/serializers/organization_serializer.py @@ -73,6 +73,10 @@ def update(self, instance, validated_data): instance.user.last_name = last_name instance.user.save() + if "email" in validated_data: + instance.user.email = validated_data.pop("email") + instance.user.save() + return super().update(instance, validated_data) def create(self, validated_data): From 734345d01c0e53437537dfaa70f66e9f92814c7f Mon Sep 17 00:00:00 2001 From: FrankApiyo Date: Mon, 26 Aug 2024 16:38:51 +0300 Subject: [PATCH 2/2] Refactor: avoid calling instance.save() twice --- onadata/libs/serializers/organization_serializer.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/onadata/libs/serializers/organization_serializer.py b/onadata/libs/serializers/organization_serializer.py index 804b3bdecf..0d52b64fa9 100644 --- a/onadata/libs/serializers/organization_serializer.py +++ b/onadata/libs/serializers/organization_serializer.py @@ -71,12 +71,11 @@ def update(self, instance, validated_data): first_name, last_name = _get_first_last_names(validated_data.get("name")) instance.user.first_name = first_name instance.user.last_name = last_name - instance.user.save() if "email" in validated_data: instance.user.email = validated_data.pop("email") - instance.user.save() + instance.user.save() return super().update(instance, validated_data) def create(self, validated_data):