def handle(self, *args, **options): if not User.objects.filter(username=options['username']).exists(): self.stdout.write( 'User \'%s\' does not exist, cannot create suborg' % options['username'], ending='\n') exit(1) else: u = User.objects.get(username=options['username']) if not Organization.objects.filter( name=options['parent_org']).exists(): self.stdout.write( 'Parent organization \'%s\' does not exist, cannot create suborg' % options['parent_org'], ending='\n') exit(1) else: org = Organization.objects.get(name=options['parent_org']) suborg_role = ROLE_OWNER if options['suborg_role'] == 'owner': suborg_role = ROLE_OWNER elif options['suborg_role'] == 'member': suborg_role = ROLE_MEMBER elif options['suborg_role'] == 'viewer': suborg_role = ROLE_VIEWER else: raise Exception( "Invalid role for suborg user. Expecting owner, member, or viewer." ) create_suborganization(u, org, options['suborg'], suborg_role) self.stdout.write('Sub-organization created!', ending='\n')
def sub_org(self, request, pk=None): """ Creates a child org of a parent org. """ body = request.data org = Organization.objects.get(pk=pk) email = body['sub_org_owner_email'].lower() try: user = User.objects.get(username=email) except User.DoesNotExist: return JsonResponse( { 'status': 'error', 'message': 'User with email address (%s) does not exist' % email }, status=status.HTTP_400_BAD_REQUEST) created, mess_or_org, _ = create_suborganization( user, org, body['sub_org_name'], ROLE_OWNER) if created: return JsonResponse({ 'status': 'success', 'organization_id': mess_or_org.pk }) else: return JsonResponse({ 'status': 'error', 'message': mess_or_org }, status=status.HTTP_409_CONFLICT)
def sub_org(self, request, pk=None): """ Creates a child org of a parent org. --- parameter_strategy: replace parameters: - name: pk description: Organization ID (Primary key) type: integer required: true paramType: path - name: sub_org_name description: Name of the new sub organization type: string required: true - name: sub_org_owner_email description: Email of the owner of the sub organization, which must already exist type: string required: true type: status: type: string description: success or error required: true message: type: string description: error message, if any required: true organization_id: type: integer description: ID of newly-created org required: true """ body = request.data org = Organization.objects.get(pk=pk) email = body['sub_org_owner_email'].lower() try: user = User.objects.get(username=email) except User.DoesNotExist: return JsonResponse({ 'status': 'error', 'message': 'User with email address (%s) does not exist' % email }, status=status.HTTP_400_BAD_REQUEST) created, mess_or_org, _ = create_suborganization(user, org, body['sub_org_name'], ROLE_OWNER) if created: return JsonResponse({ 'status': 'success', 'organization_id': mess_or_org.pk }) else: return JsonResponse({ 'status': 'error', 'message': mess_or_org }, status=status.HTTP_409_CONFLICT)
def test_too_many_nested(self): user_details_2 = { 'username': '******', 'password': '******', 'email': '*****@*****.**' } user2 = User.objects.create(**user_details_2) user_details_3 = { 'username': '******', 'password': '******', 'email': '*****@*****.**' } user3 = User.objects.create(**user_details_3) created, suborg, org_user = create_suborganization( user2, self.fake_org, 'sub org name') self.assertTrue(created) created, message, org_user = create_suborganization( user3, suborg, 'sub org of sub org') self.assertFalse(created) self.assertEqual(message, 'Tried to create child of a child organization.')
def test_suborgs_parent(self): user_details_2 = { 'username': '******', 'password': '******', 'email': '*****@*****.**' } user2 = User.objects.create(**user_details_2) created, suborg, org_user = create_suborganization( user2, self.fake_org, 'sub org name', ROLE_VIEWER) self.assertTrue(created) self.assertEqual(suborg.name, 'sub org name') self.assertFalse(suborg.is_parent) self.assertTrue(suborg.is_member(user2)) self.assertFalse(suborg.is_member(self.fake_user)) self.assertEqual(org_user.role_level, ROLE_VIEWER)