-
-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5204 from kobotoolbox/task-1077-update-organizati…
…on-detail-in-serializer-to-return-is_mmo Task 1077 update organization detail in serializer to return is mmo
- Loading branch information
Showing
23 changed files
with
501 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
ADMIN_ORG_ROLE = 'admin' | ||
EXTERNAL_ORG_ROLE = 'external' | ||
MEMBER_ORG_ROLE = 'member' | ||
OWNER_ORG_ROLE = 'owner' |
50 changes: 50 additions & 0 deletions
50
kobo/apps/organizations/migrations/0006_update_organization_name.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Generated by Django 4.2.15 on 2024-10-25 16:08 | ||
|
||
from django.db import migrations | ||
from django.core.paginator import Paginator | ||
|
||
|
||
def update_organization_names(apps, schema_editor): | ||
Organization = apps.get_model('organizations', 'Organization') | ||
OrganizationUser = apps.get_model('organizations', 'OrganizationUser') | ||
|
||
page_size = 2000 | ||
paginator = Paginator( | ||
OrganizationUser.objects.filter(organizationowner__isnull=False) | ||
.select_related('user', 'organization', 'user__extra_details') | ||
.order_by('pk'), | ||
page_size, | ||
) | ||
for page in paginator.page_range: | ||
organization_users = paginator.page(page).object_list | ||
organizations = [] | ||
for organization_user in organization_users: | ||
user = organization_user.user | ||
organization = organization_user.organization | ||
if ( | ||
organization.name | ||
and organization.name.strip() != '' | ||
and organization.name.startswith(user.username) | ||
): | ||
try: | ||
organization_name = user.extra_details.data['organization'].strip() | ||
except (KeyError, AttributeError): | ||
continue | ||
|
||
organization.name = organization_name | ||
organizations.append(organization) | ||
|
||
Organization.objects.bulk_update(organizations, ['name']) | ||
|
||
|
||
def noop(apps, schema_editor): | ||
pass | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('organizations', '0005_add_mmo_override_field_to_organization'), | ||
] | ||
|
||
operations = [migrations.RunPython(update_organization_names, noop)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
from django.test import TestCase | ||
|
||
from kobo.apps.kobo_auth.shortcuts import User | ||
from kobo.apps.organizations.models import Organization | ||
from kobo.apps.organizations.constants import ( | ||
ADMIN_ORG_ROLE, | ||
EXTERNAL_ORG_ROLE, | ||
MEMBER_ORG_ROLE, | ||
OWNER_ORG_ROLE, | ||
) | ||
|
||
|
||
class OrganizationTestCase(TestCase): | ||
|
||
fixtures = ['test_data'] | ||
|
||
def setUp(self): | ||
self.someuser = User.objects.get(username='someuser') | ||
|
||
# someuser is the only member their organization, and the owner as well. | ||
self.organization = self.someuser.organization | ||
|
||
def test_owner_user_object_property(self): | ||
anotheruser = User.objects.get(username='anotheruser') | ||
self.organization.add_user(anotheruser) | ||
assert self.organization.owner_user_object == self.someuser | ||
|
||
def test_get_user_role(self): | ||
anotheruser = User.objects.get(username='anotheruser') | ||
alice = User.objects.create(username='alice', email='[email protected]') | ||
external = User.objects.create( | ||
username='external', email='[email protected]' | ||
) | ||
self.organization.add_user(anotheruser, is_admin=True) | ||
self.organization.add_user(alice) | ||
assert self.organization.get_user_role(self.someuser) == OWNER_ORG_ROLE | ||
assert self.organization.get_user_role(anotheruser) == ADMIN_ORG_ROLE | ||
assert self.organization.get_user_role(alice) == MEMBER_ORG_ROLE | ||
assert self.organization.get_user_role(external) == EXTERNAL_ORG_ROLE | ||
|
||
def test_create_organization_on_user_creation(self): | ||
assert not Organization.objects.filter(name__startswith='alice').exists() | ||
organization_count = Organization.objects.all().count() | ||
User.objects.create_user( | ||
username='alice', password='alice', email='[email protected]' | ||
) | ||
assert Organization.objects.filter(name__startswith='alice').exists() | ||
assert Organization.objects.all().count() == organization_count + 1 | ||
|
||
def test_sync_org_name_on_save(self): | ||
""" | ||
Tests the synchronization of the organization name with the metadata in | ||
ExtraUserDetail upon saving. | ||
This synchronization should only occur if the user is the owner of the | ||
organization. | ||
""" | ||
# someuser is the owner | ||
assert self.organization.name == 'someuser’s organization' | ||
someuser_extra_details = self.someuser.extra_details | ||
someuser_extra_details.data['organization'] = 'SomeUser Technologies' | ||
someuser_extra_details.save() | ||
self.organization.refresh_from_db() | ||
assert self.organization.name == 'SomeUser Technologies' | ||
|
||
# another is an admin | ||
anotheruser = User.objects.get(username='anotheruser') | ||
|
||
self.organization.add_user(anotheruser, is_admin=True) | ||
anotheruser_extra_details = anotheruser.extra_details | ||
anotheruser_extra_details.data['organization'] = 'AnotherUser Enterprises' | ||
anotheruser_extra_details.save() | ||
self.organization.refresh_from_db() | ||
assert self.organization.name == 'SomeUser Technologies' |
Oops, something went wrong.