Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add organization email to organization profile instead of adding to organization user #2712

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions onadata/apps/api/migrations/0008_org_profile_email.py
Original file line number Diff line number Diff line change
@@ -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"
),
),
]
8 changes: 2 additions & 6 deletions onadata/apps/api/models/organization_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?
Expand All @@ -176,18 +176,14 @@ 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}]"

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.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"]:
Expand All @@ -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"]:
Expand All @@ -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 = {
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"]:
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion onadata/apps/api/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion onadata/libs/serializers/organization_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down