kolibri icon indicating copy to clipboard operation
kolibri copied to clipboard

Migrate LearnerCourseViewset to serializer-derived values pattern

Open rtibbles opened this issue 9 months ago • 4 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 LearnerCourseViewset from explicit values/field_map to serializer-derived pattern as part of the BaseValuesViewset refactor (#14036).

Complexity: Medium Target branch: develop

Context

LearnerCourseViewset in kolibri/plugins/learn/viewsets.py has values (8 fields including 3 collection__* traversals), a field_map with "classroom" as an inline lambda that pops the collection fields and returns a nested dict, and a consolidate() that delegates to _consolidate_courses_data. It has no serializer_class or annotate_queryset(). The viewset also has a resume action.

The Change

Create a serializer for LearnerCourseViewset that defines all read-path fields including the nested classroom structure. Remove the explicit values tuple and field_map. The consolidate() method will remain for course data enrichment. The resume action remains unchanged.

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 LearnerCourse domain into kolibri/plugins/learn/viewsets/course.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 last issue touching kolibri/plugins/learn/viewsets.py, and the learn moves all land here because the module cannot coexist with the package: also extract LearnerClassroomViewset (migrated in #14301) into kolibri/plugins/learn/viewsets/classroom.py and LearnerLessonViewset (migrated in #14309) into kolibri/plugins/learn/viewsets/lesson.py, move the remaining non-migrating code (LearnStateView, LearnHomePageHydrationView, and the module helpers _map_contentnodes/_consolidate_lessons_data/_consolidate_courses_data/_resumable_resources/_map_lesson_classroom) into the package, and delete viewsets.py (the file→package swap is atomic within the move commit).

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
  • Changes to the _consolidate_courses_data helper or the resume action

Acceptance Criteria

  • [ ] Explicit values tuple and field_map removed from LearnerCourseViewset

  • [ ] Serializer defines all read-path fields including nested classroom

  • [ ] consolidate() continues to work correctly

  • [ ] resume action unaffected

  • [ ] Existing API tests pass unchanged

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

  • [ ] The migrated domain lives in kolibri/plugins/learn/viewsets/{classroom,lesson,course}.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/plugins/learn/viewsets.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.plugins.learn.viewsets.LearnerCourseViewset \
        --inherit-kolibri-home -o baseline.json
    
    # ... make changes ...
    
    python integration_testing/scripts/viewset_serialization_benchmark.py \
        kolibri.plugins.learn.viewsets.LearnerCourseViewset \
        --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 and generate user data:

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

References

  • Tracking issue: #14036
  • Benchmark script: integration_testing/scripts/viewset_serialization_benchmark.py
  • Example migration: FacilityUserViewSet in kolibri/core/auth/api.py

AI usage

This issue was drafted with Claude Code assistance, reviewed and directed by @rtibbles.

  • After the move, the benchmark comparison run uses the new dotted path (e.g. kolibri.plugins.learn.viewsets.course.LearnerCourseViewset).

rtibbles avatar Mar 02 '26 05:03 rtibbles

Watching for resolution on behalf of learningequality/kolibri#14302.

rtibblesbot avatar Jun 09 '26 15:06 rtibblesbot

Plan: Migrate LearnerCourseViewset from explicit values/field_map to the serializer-derived pattern, then atomically refactor kolibri/plugins/learn/viewsets.py into a package with one file per viewset domain.

  • [ ] Write a failing-turned-green test for classroom field
  • [ ] Replace values/field_map with a serializer
  • [ ] Review git blame and annotate non-obvious hunks
  • [ ] Create viewsets/__init__.py
  • [ ] Create viewsets/classroom.py
  • [ ] Create viewsets/lesson.py
  • [ ] Create viewsets/course.py
  • [ ] Update api_urls.py and delete viewsets.py
  • [ ] Re-run benchmark with new dotted path

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 13 '26 23:06 rtibblesbot

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

rtibblesbot avatar Jun 14 '26 03:06 rtibblesbot

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

rtibblesbot avatar Jun 17 '26 05:06 rtibblesbot