From b320b64afd19e74ccb61183fa253213aa99a1570 Mon Sep 17 00:00:00 2001 From: Davis Raymond Muro Date: Thu, 30 Apr 2020 11:19:20 +0300 Subject: [PATCH] Rename function get_organization_owners_team Rename function get_organization_owners_team to get_or_create_organization_owners_team --- .../test_organization_profile_viewset.py | 6 +++--- .../tests/viewsets/test_project_viewset.py | 6 +++--- .../api/tests/viewsets/test_team_viewset.py | 4 ++-- .../api/tests/viewsets/test_widget_viewset.py | 4 ++-- onadata/apps/api/tools.py | 20 +++++++++---------- .../organization_member_serializer.py | 4 ++-- .../libs/serializers/project_serializer.py | 4 ++-- 7 files changed, 24 insertions(+), 24 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 2c98d774fc..fae9c63dc1 100644 --- a/onadata/apps/api/tests/viewsets/test_organization_profile_viewset.py +++ b/onadata/apps/api/tests/viewsets/test_organization_profile_viewset.py @@ -11,7 +11,7 @@ from onadata.apps.api.viewsets.user_profile_viewset import UserProfileViewSet from onadata.apps.api.viewsets.project_viewset import ProjectViewSet from onadata.libs.permissions import OwnerRole -from onadata.apps.api.tools import (get_organization_owners_team, +from onadata.apps.api.tools import (get_or_create_organization_owners_team, add_user_to_organization) from onadata.apps.api.models.organization_profile import OrganizationProfile from onadata.apps.main.models import UserProfile @@ -759,7 +759,7 @@ def test_add_members_to_owner_role(self): self.assertEqual(response.data['users'][1]['user'], 'aboy') self.assertEqual(response.data['users'][1]['role'], 'owner') - owner_team = get_organization_owners_team(self.organization) + owner_team = get_or_create_organization_owners_team(self.organization) self.assertIn(aboy, owner_team.user_set.all()) @@ -772,7 +772,7 @@ def test_add_members_to_owner_role(self): response = view(request, user='denoinc') self.assertEqual(response.status_code, 200) - owner_team = get_organization_owners_team(self.organization) + owner_team = get_or_create_organization_owners_team(self.organization) self.assertNotIn(aboy, owner_team.user_set.all()) diff --git a/onadata/apps/api/tests/viewsets/test_project_viewset.py b/onadata/apps/api/tests/viewsets/test_project_viewset.py index ba26f28821..3ff91d0e61 100644 --- a/onadata/apps/api/tests/viewsets/test_project_viewset.py +++ b/onadata/apps/api/tests/viewsets/test_project_viewset.py @@ -17,7 +17,7 @@ from onadata.apps.api import tools from onadata.apps.api.tests.viewsets.test_abstract_viewset import \ TestAbstractViewSet -from onadata.apps.api.tools import get_organization_owners_team +from onadata.apps.api.tools import get_or_create_organization_owners_team from onadata.apps.api.viewsets.organization_profile_viewset import \ OrganizationProfileViewSet from onadata.apps.api.viewsets.project_viewset import ProjectViewSet @@ -774,7 +774,7 @@ def test_form_transfer_when_org_creator_creates_project( response = view(request, user=self.organization.user.username) self.assertEqual(response.status_code, 201) - owners_team = get_organization_owners_team(self.organization) + owners_team = get_or_create_organization_owners_team(self.organization) self.assertIn(alice_profile.user, owners_team.user_set.all()) # let bob create a project in org @@ -853,7 +853,7 @@ def test_form_transfer_when_org_admin_not_creator_creates_project( response = view(request, user=self.organization.user.username) self.assertEqual(response.status_code, 201) - owners_team = get_organization_owners_team(self.organization) + owners_team = get_or_create_organization_owners_team(self.organization) self.assertIn(alice_profile.user, owners_team.user_set.all()) # let alice create a project in org diff --git a/onadata/apps/api/tests/viewsets/test_team_viewset.py b/onadata/apps/api/tests/viewsets/test_team_viewset.py index 5abb909804..2505c42be9 100644 --- a/onadata/apps/api/tests/viewsets/test_team_viewset.py +++ b/onadata/apps/api/tests/viewsets/test_team_viewset.py @@ -7,7 +7,7 @@ from onadata.apps.api.tests.viewsets.test_abstract_viewset import \ TestAbstractViewSet from onadata.apps.api.tools import add_user_to_team -from onadata.apps.api.tools import get_organization_owners_team, \ +from onadata.apps.api.tools import get_or_create_organization_owners_team, \ get_organization_members_team from onadata.apps.api.viewsets.metadata_viewset import MetaDataViewSet from onadata.apps.api.viewsets.organization_profile_viewset import \ @@ -399,7 +399,7 @@ def test_non_owners_should_be_able_to_change_member_permissions(self): self.assertEqual(response.status_code, 201) - owners_team = get_organization_owners_team(self.organization) + owners_team = get_or_create_organization_owners_team(self.organization) self.assertIn(chuck_profile.user, owners_team.user_set.all()) alice_data = {'username': 'alice', 'email': 'alice@localhost.com'} diff --git a/onadata/apps/api/tests/viewsets/test_widget_viewset.py b/onadata/apps/api/tests/viewsets/test_widget_viewset.py index 4427554910..f12be5164f 100644 --- a/onadata/apps/api/tests/viewsets/test_widget_viewset.py +++ b/onadata/apps/api/tests/viewsets/test_widget_viewset.py @@ -11,7 +11,7 @@ from onadata.libs.permissions import ReadOnlyRole from onadata.libs.permissions import DataEntryOnlyRole from onadata.libs.permissions import OwnerRole -from onadata.apps.api.tools import get_organization_owners_team +from onadata.apps.api.tools import get_or_create_organization_owners_team from onadata.apps.api.viewsets.organization_profile_viewset import\ OrganizationProfileViewSet @@ -717,7 +717,7 @@ def test_widget_create_by_org_admin(self): self.assertEqual(response.status_code, 201) - owners_team = get_organization_owners_team(self.organization) + owners_team = get_or_create_organization_owners_team(self.organization) self.assertIn(chuck_profile.user, owners_team.user_set.all()) extra = { diff --git a/onadata/apps/api/tools.py b/onadata/apps/api/tools.py index 2c33c08bd4..d551e3babf 100644 --- a/onadata/apps/api/tools.py +++ b/onadata/apps/api/tools.py @@ -183,7 +183,8 @@ def get_organization_members_team(organization): return team -def get_organization_owners_team(org): +# pylint: disable=invalid-name +def get_or_create_organization_owners_team(org): """ Get the owners team of an organization :param org: organization @@ -192,23 +193,22 @@ def get_organization_owners_team(org): team_name = f'{org.user.username}#{Team.OWNER_TEAM_NAME}' try: team = Team.objects.get(name=team_name, organization=org.user) - return team except Team.DoesNotExist: from multidb.pinning import use_master # pylint: disable=import-error with use_master: queryset = Team.objects.filter( name=team_name, organization=org.user) if queryset.count() > 0: - return queryset.first() - else: - return create_owner_team_and_assign_permissions(org) + return queryset.first() # pylint: disable=no-member + return create_owner_team_and_assign_permissions(org) + return team def remove_user_from_organization(organization, user): """Remove a user from an organization""" team = get_organization_members_team(organization) remove_user_from_team(team, user) - owners_team = get_organization_owners_team(organization) + owners_team = get_or_create_organization_owners_team(organization) remove_user_from_team(owners_team, user) role = get_role_in_org(user, organization) @@ -236,7 +236,7 @@ def remove_user_from_team(team, user): # if team is owners team remove more perms if team.name.find(Team.OWNER_TEAM_NAME) > 0: - owners_team = get_organization_owners_team(team.organization.profile) + owners_team = get_or_create_organization_owners_team(team.organization.profile) members_team = get_organization_members_team(team.organization.profile) for perm in get_perms_for_model(Team): remove_perm(perm.codename, user, owners_team) @@ -265,7 +265,7 @@ def add_user_to_team(team, user): def _assign_organization_team_perms(organization, user): - owners_team = get_organization_owners_team(organization.profile) + owners_team = get_or_create_organization_owners_team(organization.profile) members_team = get_organization_members_team(organization.profile) for perm in get_perms_for_model(Team): assign_perm(perm.codename, user, owners_team) @@ -281,7 +281,7 @@ def get_organization_members(organization): def get_organization_owners(organization): """Get owners team user queryset""" - team = get_organization_owners_team(organization) + team = get_or_create_organization_owners_team(organization) return team.user_set.all() @@ -290,7 +290,7 @@ def _get_owners(organization): return [ user - for user in get_organization_owners_team(organization).user_set.all() + for user in get_or_create_organization_owners_team(organization).user_set.all() if get_role_in_org(user, organization) == 'owner' and organization.user != user ] diff --git a/onadata/libs/serializers/organization_member_serializer.py b/onadata/libs/serializers/organization_member_serializer.py index 5c8c3a3775..4a0fb0baad 100644 --- a/onadata/libs/serializers/organization_member_serializer.py +++ b/onadata/libs/serializers/organization_member_serializer.py @@ -10,7 +10,7 @@ from onadata.libs.permissions import OwnerRole from onadata.libs.permissions import is_organization from onadata.apps.api.tools import add_user_to_organization -from onadata.apps.api.tools import get_organization_owners_team +from onadata.apps.api.tools import get_or_create_organization_owners_team from onadata.apps.api.tools import add_user_to_team from onadata.apps.api.tools import remove_user_from_team from onadata.apps.api.tools import _get_owners @@ -37,7 +37,7 @@ def _set_organization_role_to_user(organization, user, role): role_cls = ROLES.get(role) role_cls.add(user, organization) - owners_team = get_organization_owners_team(organization) + owners_team = get_or_create_organization_owners_team(organization) # add the owner to owners team if role == OwnerRole.name: diff --git a/onadata/libs/serializers/project_serializer.py b/onadata/libs/serializers/project_serializer.py index cc9cd526e6..2dc901239d 100644 --- a/onadata/libs/serializers/project_serializer.py +++ b/onadata/libs/serializers/project_serializer.py @@ -14,7 +14,7 @@ from onadata.apps.api.models import OrganizationProfile from onadata.apps.api.tools import (get_organization_members_team, - get_organization_owners_team) + get_or_create_organization_owners_team) from onadata.apps.logger.models import Project, XForm from onadata.libs.permissions import (OwnerRole, ReadOnlyRole, get_role, is_organization) @@ -401,7 +401,7 @@ def update(self, instance, validated_data): set_owners_permission(owner, instance) if is_organization(owner.profile): - owners_team = get_organization_owners_team(owner.profile) + owners_team = get_or_create_organization_owners_team(owner.profile) members_team = get_organization_members_team(owner.profile) OwnerRole.add(owners_team, instance) ReadOnlyRole.add(members_team, instance)