Skip to content

Commit

Permalink
feat(organization): backfill Asset search field for owner label TASK-…
Browse files Browse the repository at this point in the history
…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
noliveleger authored Dec 20, 2024
1 parent d776b89 commit 6f8c46a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
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'])
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),
]

0 comments on commit 6f8c46a

Please sign in to comment.