Skip to content

Commit

Permalink
Return org owner team under 'users' on /org
Browse files Browse the repository at this point in the history
- Update tests
  • Loading branch information
DavisRayM committed Dec 2, 2019
1 parent 3b07b8c commit 1bf60ed
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
5 changes: 5 additions & 0 deletions onadata/apps/api/tests/models/test_organization_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down
8 changes: 7 additions & 1 deletion onadata/apps/api/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
29 changes: 21 additions & 8 deletions onadata/libs/serializers/organization_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 1bf60ed

Please sign in to comment.