kolibri icon indicating copy to clipboard operation
kolibri copied to clipboard

Migrate LearnerClassroomViewset 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 LearnerClassroomViewset from explicit values to serializer-derived pattern as part of the BaseValuesViewset refactor (#14036).

Complexity: High Target branch: develop

Context

LearnerClassroomViewset in kolibri/plugins/learn/viewsets.py has a minimal values = ("id", "name") tuple — the real work is entirely in consolidate(), which fetches Lesson, Exam, ContentNode, and CourseSession objects via separate queries and assembles exams, lessons, and courses lists onto each classroom. The consolidate method uses inline values() calls and module-level helper functions (_consolidate_lessons_data, _consolidate_courses_data, _map_contentnodes, exam_assignment_lookup).

There is also a module-level singleton learner_classroom_viewset = LearnerClassroomViewset() used by LearnHomePageHydrationView.

The Change

Create a serializer for LearnerClassroomViewset that defines the read-path fields (id, name, and the deferred exams, lessons, courses fields). Remove the explicit values tuple. The consolidate() method will remain — the complex data assembly across multiple models is inherently custom logic that deferred_fields is designed for.

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

Relocation

None in this issue: the learn plugin's viewsets.py cannot coexist with the viewsets/ package, so all learn moves happen together in #14310. Migrate in place.

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

  • Restructuring the consolidate() logic or helper functions

  • Changes to the LearnHomePageHydrationView integration

  • Moving code into the viewsets/ package — all learn moves happen in #14310

Acceptance Criteria

  • [ ] Explicit values tuple removed from LearnerClassroomViewset
  • [ ] Serializer defines all read-path fields with deferred fields for nested data
  • [ ] consolidate() continues to produce identical exams, lessons, courses structures
  • [ ] Module-level singleton usage by LearnHomePageHydrationView unaffected
  • [ ] Existing API tests pass unchanged
  • [ ] Benchmark comparison passes (data hash match + no performance regression)

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.LearnerClassroomViewset \
        --inherit-kolibri-home -o baseline.json
    
    # ... make changes ...
    
    python integration_testing/scripts/viewset_serialization_benchmark.py \
        kolibri.plugins.learn.viewsets.LearnerClassroomViewset \
        --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 before benchmarking:

kolibri manage importchannel network 95a52b386f2c485cb97dd60901674a98
kolibri manage importcontent network 95a52b386f2c485cb97dd60901674a98
kolibri manage generateuserdata --classes 10 --num-lessons 5 --num-exams 5

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.

rtibbles avatar Mar 02 '26 04:03 rtibbles

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

rtibblesbot avatar Jun 09 '26 15:06 rtibblesbot

Plan: Migrate LearnerClassroomViewset from an explicit values = ("id", "name") tuple to the serializer-derived pattern by adding a LearnerClassroomSerializer and removing the values attribute.

  • [ ] Pre-migration test verification
  • [ ] Benchmark baseline (pre-migration)
  • [ ] Add serializer and migrate viewset
  • [ ] Post-migration test verification and simplify pass
  • [ ] Benchmark comparison
  • [ ] Open pull request

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 10 '26 20:06 rtibblesbot

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

rtibblesbot avatar Jun 11 '26 01:06 rtibblesbot

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

rtibblesbot avatar Jun 11 '26 07:06 rtibblesbot