diff --git a/onadata/apps/api/tests/models/test_organization_profile.py b/onadata/apps/api/tests/models/test_organization_profile.py index 5a1f36fd6e..d2d8e4d7b9 100644 --- a/onadata/apps/api/tests/models/test_organization_profile.py +++ b/onadata/apps/api/tests/models/test_organization_profile.py @@ -6,6 +6,7 @@ from django.test import override_settings from django.core.cache import cache from onadata.libs.utils.cache_tools import IS_ORG, safe_delete +from onadata.libs.permissions import OwnerRole class TestOrganizationProfile(TestBase): @@ -31,6 +32,10 @@ def test_create_organization_creates_team_and_perms(self): self.assertIn(team.group_ptr, self.user.groups.all()) self.assertTrue(self.user.has_perm('api.is_org_owner')) + # Assert that the user has the OwnerRole for the Organization + self.assertTrue( + OwnerRole.user_has_role(self.user, organization_profile)) + def test_disallow_same_username_with_different_cases(self): tools.create_organization("modilabs", self.user) with self.assertRaises(IntegrityError): diff --git a/onadata/apps/api/tools.py b/onadata/apps/api/tools.py index 9f2b8cba02..43c26da3a8 100644 --- a/onadata/apps/api/tools.py +++ b/onadata/apps/api/tools.py @@ -104,7 +104,7 @@ def create_organization(name, creator): - Team(name='Owners', organization=organization).save() """ - organization, _created = User.objects.get_or_create(username__iexact=name) + organization, _created = User.objects.get_or_create(username=name) organization_profile = OrganizationProfile.objects.create( user=organization, creator=creator) return organization_profile @@ -261,6 +261,12 @@ def get_organization_members(organization): return team.user_set.all() +def get_organization_owners(organization): + """Get owners team user queryset""" + team = get_organization_owners_team(organization) + return team.user_set.all() + + def _get_owners(organization): # Get users with owners perms and not the org itself diff --git a/onadata/libs/serializers/organization_serializer.py b/onadata/libs/serializers/organization_serializer.py index 864b07e133..74a7206317 100644 --- a/onadata/libs/serializers/organization_serializer.py +++ b/onadata/libs/serializers/organization_serializer.py @@ -13,7 +13,8 @@ from onadata.apps.api import tools from onadata.apps.api.models import OrganizationProfile from onadata.apps.api.tools import (_get_first_last_names, - get_organization_members) + get_organization_members, + get_organization_owners) from onadata.apps.main.forms import RegistrationFormUserProfile from onadata.libs.permissions import get_role_in_org from onadata.libs.serializers.fields.json_field import JsonField @@ -113,12 +114,24 @@ def get_users(self, obj): # pylint: disable=no-self-use """ Return organization members. """ + def create_user_list(user_list): + return [{ + 'user': u.username, + 'role': get_role_in_org(u, obj), + 'first_name': u.first_name, + 'last_name': u.last_name, + 'gravatar': u.profile.gravatar + } for u in user_list] + members = get_organization_members(obj) if obj else [] + owners = get_organization_owners(obj) if obj else [] + + repeated_users = set(owners) & set(members) + + for user in repeated_users: + members.exclude(username=user.username) + + members_list = create_user_list(members) + owners_list = create_user_list(owners) - return [{ - 'user': u.username, - 'role': get_role_in_org(u, obj), - 'first_name': u.first_name, - 'last_name': u.last_name, - 'gravatar': u.profile.gravatar, - } for u in members] + return owners_list + members_list