diff --git a/onadata/apps/api/migrations/0008_org_profile_email.py b/onadata/apps/api/migrations/0008_org_profile_email.py new file mode 100644 index 0000000000..28f51b4bec --- /dev/null +++ b/onadata/apps/api/migrations/0008_org_profile_email.py @@ -0,0 +1,25 @@ +# Generated by Django 4.2.14 on 2024-10-01 08:01 +""" +Add email field to the organization profile model +""" +from django.db import migrations, models + + +class Migration(migrations.Migration): + """ + Add email field to the organization profile model + """ + + dependencies = [ + ("api", "0007_odktoken_expires"), + ] + + operations = [ + migrations.AddField( + model_name="organizationprofile", + name="email", + field=models.EmailField( + blank=True, max_length=254, verbose_name="email address" + ), + ), + ] diff --git a/onadata/apps/api/models/organization_profile.py b/onadata/apps/api/models/organization_profile.py index 24a81dbf1a..146e112ea2 100644 --- a/onadata/apps/api/models/organization_profile.py +++ b/onadata/apps/api/models/organization_profile.py @@ -7,6 +7,7 @@ from django.contrib.contenttypes.models import ContentType from django.db import models from django.db.models.signals import post_delete, post_save +from django.utils.translation import gettext_lazy as _ from guardian.models import GroupObjectPermissionBase, UserObjectPermissionBase from guardian.shortcuts import assign_perm, get_perms_for_model @@ -154,7 +155,6 @@ def _post_save_create_owner_team(sender, instance, created, **kwargs): class OrganizationProfile(UserProfile): - """Organization: Extends the user profile for organization specific info * What does this do? @@ -176,6 +176,7 @@ class Meta: is_organization = models.BooleanField(default=True) # Other fields here creator = models.ForeignKey(User, on_delete=models.CASCADE) + email = models.EmailField(_("email address"), blank=True) def __str__(self): return f"{self.name}[{self.user.username}]" @@ -183,11 +184,6 @@ def __str__(self): def save(self, *args, **kwargs): # pylint: disable=arguments-differ super().save(*args, **kwargs) - @property - def email(self): - "organization email" - return self.user.email - def remove_user_from_organization(self, user): """Removes a user from all teams/groups in the organization. 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 07fe2d4dd2..d86e2966f5 100644 --- a/onadata/apps/api/tests/viewsets/test_organization_profile_viewset.py +++ b/onadata/apps/api/tests/viewsets/test_organization_profile_viewset.py @@ -196,8 +196,8 @@ def test_orgs_get_not_creator(self): response = view(request, user="denoinc") self.assertNotEqual(response.get("Cache-Control"), None) self.assertEqual(response.status_code, 200) - del self.company_data['email'] - del self.company_data['metadata'] + del self.company_data["email"] + del self.company_data["metadata"] self.assertEqual(response.data, self.company_data) self.assertIn("users", list(response.data)) for user in response.data["users"]: @@ -212,7 +212,7 @@ def test_orgs_get_anon(self): self.assertNotEqual(response.get("Cache-Control"), None) self.assertEqual(response.status_code, 200) del self.company_data["email"] - del self.company_data['metadata'] + del self.company_data["metadata"] self.assertEqual(response.data, self.company_data) self.assertIn("users", list(response.data)) for user in response.data["users"]: @@ -222,7 +222,7 @@ def test_orgs_get_anon(self): def test_orgs_create(self): self._org_create() self.assertTrue(self.organization.user.is_active) - self.assertEqual(self.organization.user.email, "mail@mail-server.org") + self.assertEqual(self.organization.email, "mail@mail-server.org") def test_orgs_create_without_name(self): data = { @@ -264,7 +264,7 @@ def test_org_create_and_fetch_by_admin_user(self): request.user = self.user response = self.view(request) self.assertEqual(response.status_code, 201) - self.assertEqual(response.data['email'], org_email) + self.assertEqual(response.data["email"], org_email) def test_org_create_with_anonymous_user(self): data = { @@ -420,7 +420,7 @@ def test_member_sees_orgs_added_to(self): } ) del expected_data["metadata"] - del expected_data['email'] + del expected_data["email"] request = self.factory.get("/", **self.extra) response = view(request) @@ -452,8 +452,7 @@ def test_role_for_org_non_owner(self): request = self.factory.get("/", **self.extra) response = view(request, user="denoinc") self.assertEqual(response.status_code, 200) - self.assertTrue('email' in response.data) - self.assertEqual(response.data['email'], 'mail@mail-server.org') + self.assertEqual(response.data["email"], "mail@mail-server.org") self.assertIn("users", list(response.data)) for user in response.data["users"]: @@ -476,10 +475,10 @@ def test_role_for_org_non_owner(self): request = self.factory.get("/", **self.extra) request.user = AnonymousUser() request.headers = None - request.META['HTTP_AUTHORIZATION'] = "" + request.META["HTTP_AUTHORIZATION"] = "" response = view(request, user="denoinc") self.assertEqual(response.status_code, 200) - self.assertFalse('email' in response.data) + self.assertFalse("email" in response.data) def test_add_members_to_org_with_anonymous_user(self): self._org_create() diff --git a/onadata/apps/api/tools.py b/onadata/apps/api/tools.py index 164ef12a4d..46835451c4 100644 --- a/onadata/apps/api/tools.py +++ b/onadata/apps/api/tools.py @@ -172,7 +172,6 @@ def create_organization_object(org_name, creator, attrs=None): username=org_name, first_name=first_name, last_name=last_name, - email=email, is_active=getattr(settings, "ORG_ON_CREATE_IS_ACTIVE", True), ) new_user.save() @@ -190,6 +189,7 @@ def create_organization_object(org_name, creator, attrs=None): organization=attrs.get("organization", ""), home_page=attrs.get("home_page", ""), twitter=attrs.get("twitter", ""), + email=email, ) return profile diff --git a/onadata/libs/serializers/organization_serializer.py b/onadata/libs/serializers/organization_serializer.py index 0d52b64fa9..69b9e6845c 100644 --- a/onadata/libs/serializers/organization_serializer.py +++ b/onadata/libs/serializers/organization_serializer.py @@ -73,7 +73,7 @@ def update(self, instance, validated_data): instance.user.last_name = last_name if "email" in validated_data: - instance.user.email = validated_data.pop("email") + instance.email = validated_data.pop("email") instance.user.save() return super().update(instance, validated_data)