kolibri icon indicating copy to clipboard operation
kolibri copied to clipboard

Migrate BookmarksViewSet to serializer-derived values pattern

Open rtibbles opened this issue 9 months ago • 2 comments

❌ This issue is not open for contribution. Visit Contributing guidelines to learn about the contributing process and how to find suitable issues.

Overview

Migrate BookmarksViewSet from explicit values tuple to serializer-derived pattern as part of the BaseValuesViewset refactor (#14036).

Complexity: Low Target branch: develop

Context

BookmarksViewSet in kolibri/core/bookmarks/api.py currently has both serializer_class = BookmarksSerializer and an explicit values = ("channel_id", "contentnode_id", "id", "content_id") tuple. It has no field_map, consolidate(), or annotate_queryset() — making it a straightforward migration.

The Change

Remove the explicit values tuple from BookmarksViewSet so that values and field mappings are derived from BookmarksSerializer. Update the serializer if any read-path fields are missing or need source kwargs.

See #14036 for the overall pattern and FacilityUserViewSet in kolibri/core/auth/api.py for a concrete migration example.

Relocation

After the migration commits, extract the Bookmarks domain into kolibri/core/bookmarks/viewsets/bookmark.py as a pure-move commit: the viewset(s), their serializers (inline above the viewsets), and any domain-specific filters and permission classes. Update imports (api_urls.py, tests, cross-references).

Moving code breaks GitHub blame (local git blame -C -C still works), so digest the history the move obscures:

  1. Run blame over the code being moved; flag hunks whose purpose isn't evident from the code or tests (odd guards, magic orderings, special cases).
  2. Chase each flagged hunk to its originating PR/issue; where the rationale is non-obvious and load-bearing, carry it forward as a code comment citing the reference.
  3. Where the rationale turns out to be obsolete, remove the code as part of the migration commits rather than annotating it.

This is the only issue touching kolibri/core/bookmarks/api.py; nothing else lives there, so delete api.py after the move.

Out of Scope

  • Changes to the API response shape — the API contract must not change
  • Modifications to existing tests — if tests need to change, the migration is wrong
  • Write-path changes — only the read serialization path is affected

Acceptance Criteria

  • [ ] Explicit values tuple removed from BookmarksViewSet

  • [ ] BookmarksSerializer has all read-path fields with correct source kwargs

  • [ ] Existing API tests pass unchanged

  • [ ] Benchmark comparison passes (data hash match + no performance regression)

  • [ ] The migrated domain lives in kolibri/core/bookmarks/viewsets/bookmark.py, moved as a pure-move commit separate from import updates

  • [ ] Comments citing the originating PR/issue added where blame digestion found non-obvious, load-bearing rationale

  • [ ] Remaining non-migrating code moved; kolibri/core/bookmarks/api.py deleted

Testing

  • Before migrating: verify that existing test coverage is adequate. If the viewset has insufficient API tests, write them first to capture the current behavior. Tests must pass before and after migration without modification.
  • Benchmark: run the benchmark script before and after the migration using the same data:
    python integration_testing/scripts/viewset_serialization_benchmark.py \
        kolibri.core.bookmarks.api.BookmarksViewSet \
        --inherit-kolibri-home -o baseline.json
    
    # ... make changes ...
    
    python integration_testing/scripts/viewset_serialization_benchmark.py \
        kolibri.core.bookmarks.api.BookmarksViewSet \
        --inherit-kolibri-home --compare baseline.json
    
  • The benchmark comparison must pass — this validates that the data output is identical and there is no performance regression.
  • Include the benchmark before/after comparison in the pull request description.

Benchmark data setup: import the Kolibri QA channel first, then create bookmark data via kolibri manage shell:

kolibri manage importchannel network 95a52b386f2c485cb97dd60901674a98
kolibri manage importcontent network 95a52b386f2c485cb97dd60901674a98

Then in kolibri manage shell:

from kolibri.core.auth.models import Facility, FacilityUser
from kolibri.core.bookmarks.models import Bookmark
from kolibri.core.content.models import ContentNode

facility = Facility.objects.first()
user = FacilityUser.objects.filter(facility=facility).first()
for node in ContentNode.objects.all()[:100]:
    Bookmark.objects.get_or_create(
        user=user,
        contentnode_id=node.id,
        defaults={"content_id": node.content_id, "channel_id": node.channel_id},
    )
  • After the move, the benchmark comparison run uses the new dotted path (e.g. kolibri.core.bookmarks.viewsets.bookmark.BookmarksViewSet).

rtibbles avatar Mar 02 '26 04:03 rtibbles

Plan: Remove the explicit values tuple from BookmarksViewSet, update BookmarksSerializer so all read-path fields are derived from it, then relocate the domain into kolibri/core/bookmarks/viewsets/bookmark.py as a pure-move commit.

  • [ ] Add response-shape regression test
  • [ ] Capture baseline benchmark (before any code changes)
  • [ ] Update BookmarksSerializer and remove explicit values tuple
  • [ ] Post-migration benchmark comparison
  • [ ] Digest git blame and carry forward load-bearing rationale
  • [ ] Create the viewsets package
  • [ ] Pure-move commit — copy code to viewsets/bookmark.py
  • [ ] Post-move benchmark comparison

status


@rtibblesbot's comments are generated by an LLM, and should be evaluated accordingly

How was this generated?
  • Implemented code following the pre-approved plan using test-driven development (red-green-refactor)
  • Ran the project's full test suite and linter before committing
  • For UI changes: captured live screenshots to verify rendering
  • For backend changes: verified with example API/CLI output

rtibblesbot avatar Jun 09 '26 15:06 rtibblesbot

The bot has been waiting for a response for 2 hour(s). It will resume automatically when someone replies.

rtibblesbot avatar Jun 09 '26 18:06 rtibblesbot