-
-
Notifications
You must be signed in to change notification settings - Fork 183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(organizations): add owner_label
field to AssetSerializer
TASK-1181
#5312
feat(organizations): add owner_label
field to AssetSerializer
TASK-1181
#5312
Conversation
916e01c
to
aa796c9
Compare
kpi/tests/api/v2/test_api_assets.py
Outdated
@@ -1,5 +1,6 @@ | |||
import copy | |||
import json | |||
import mock |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is deprecated, please use from unitest import mock
instead
kpi/tests/api/v2/test_api_assets.py
Outdated
# Fetch the assets list and verify the initial owner_label | ||
list_response = self.client.get(self.list_url) | ||
self.assertEqual( | ||
list_response.data['results'][0]['owner_label'], | ||
asset_owner_username | ||
) | ||
|
||
# Mock the is_mmo property to simulate a multi-member organization | ||
with mock.patch.object(Organization, 'is_mmo', return_value=True): | ||
list_response = self.client.get(self.list_url) | ||
|
||
# Verify the owner_label now reflects the organization's name | ||
self.assertEqual( | ||
list_response.data['results'][0]['owner_label'], | ||
asset_owner.organization.name | ||
) | ||
|
||
# Assign a new user as the organization owner | ||
another_user = User.objects.get(username='anotheruser') | ||
another_org_user = OrganizationUser.objects.create( | ||
organization=asset_owner.organization, | ||
user=another_user, | ||
is_admin=True | ||
) | ||
org_owner = OrganizationOwner.objects.get( | ||
organization=asset_owner.organization | ||
) | ||
org_owner.organization_user = another_org_user | ||
org_owner.save() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the test should be different.
- Make
asset_owner
organization a MMO (setmmo_override
to True) - Add another user (anotheruser) to
asset_owner
organization (asset_owner.organization.add_user(another_user)
) - Share asset with anotheruser (
assign_perm(anotheruser, PERM_VIEW_ASSET)
) - Create another asset with an external user and share it with anotheruser.
- Test the endpoint (with anotheruser logged in) that returns the org' name for first asset and external username for the second asset.
- Test the endpoint (with external user logged in) that returns external user's username
# 5) Get organization per asset | ||
assets = ( | ||
Asset.objects.filter(id__in=asset_ids) | ||
.prefetch_related('owner__organizations_organization') | ||
) | ||
organization_by_asset = defaultdict(dict) | ||
for asset in assets: | ||
organization = getattr(asset.owner, 'organization', None) | ||
organization_by_asset[asset.id] = organization | ||
context_['organization_by_asset'] = organization_by_asset | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have the same optimization for ./api/v2/organizations/<organization_id>/assets/
?
Nevermind, the (asset org) endpoint is always showing the same org. @cache_for_request
should help to avoid making multiple queries.
e6b5d52
to
d4e6c47
Compare
d4e6c47
to
0fe5035
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
#5324 will be add more optimization to this PR. |
… github.com:kobotoolbox/kpi into task-1181-add-owner_label-field-to-asset-serializer
🗒️ Checklist
<type>(<scope>)<!>: <title> TASK-1234
frontend
orbackend
unless it's global📣 Summary
Added
owner_label
field to the Asset API to clarify ownership information, dynamically reflecting either the owner's username or the organization's name.📖 Description
The
owner_label
field in the Asset API now dynamically displays:This enhancement improves clarity by providing contextual ownership details directly in the API response.
👷 Description for instance maintainers
This update introduces a new owner_label field in the Asset serializer:
👀 Preview steps
💭 Notes