-
-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(organization): backfill Asset search field for owner label TASK-…
…1334 (#5391) ### 📣 Summary Populated the asset search field to include the owner username and organization name for improved search functionality. ### 📖 Description The asset search field has been updated to include tthe owner username and organization name, ensuring better usability when searching for asset owner labels. This backfill operation populates the field for existing assets where the owner label was previously missing.
- Loading branch information
1 parent
d776b89
commit 6f8c46a
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
kobo/apps/long_running_migrations/jobs/0004_back_fill_asset_search_field_for_owner_label.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,42 @@ | ||
# Generated on 2024-12-19 15:34 | ||
from django.db.models import Prefetch | ||
from more_itertools import chunked | ||
|
||
from kobo.apps.organizations.models import Organization | ||
from kpi.models.asset import Asset | ||
|
||
|
||
def run(): | ||
""" | ||
Transfers all assets owned by members to their respective organizations. | ||
""" | ||
CHUNK_SIZE = 2000 | ||
|
||
assets = ( | ||
Asset.objects.only('search_field', 'name', 'uid', 'owner') | ||
.exclude(search_field__has_keys='owner_username') | ||
.select_related('owner') | ||
.prefetch_related( | ||
Prefetch( | ||
'owner__organizations_organization', | ||
queryset=Organization.objects.all().order_by( | ||
'-organization_users__created' | ||
), | ||
to_attr='organizations', | ||
) | ||
) | ||
).iterator(CHUNK_SIZE) | ||
|
||
for asset_batch in chunked(assets, CHUNK_SIZE): | ||
for asset in asset_batch: | ||
try: | ||
organization_name = asset.owner.organizations[0].name | ||
except IndexError: | ||
organization_name = f'{asset.owner.username}’s organization' | ||
|
||
asset.update_search_field( | ||
owner_username=asset.owner.username, | ||
organization_name=organization_name, | ||
) | ||
|
||
Asset.objects.bulk_update(asset_batch, fields=['search_field']) |
25 changes: 25 additions & 0 deletions
25
...s/long_running_migrations/migrations/0004_back_fill_asset_search_field_for_owner_label.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,25 @@ | ||
# Generated by Django 4.2.15 on 2024-12-19 14:54 | ||
|
||
from django.db import migrations | ||
|
||
|
||
def add_long_running_migration(apps, schema_editor): | ||
LongRunningMigration = apps.get_model('long_running_migrations', 'LongRunningMigration') # noqa | ||
LongRunningMigration.objects.create( | ||
name='0004_back_fill_asset_search_field_for_owner_label' | ||
) | ||
|
||
|
||
def noop(*args, **kwargs): | ||
pass | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('long_running_migrations', '0003_transfer_members_data_ownership_to_org'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(add_long_running_migration, noop), | ||
] |