kolibri icon indicating copy to clipboard operation
kolibri copied to clipboard

Migrate FacilitySearchUsernameViewSet to serializer-derived values pattern

Open rtibbles opened this issue 9 months ago • 3 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 FacilitySearchUsernameViewSet from its explicit values tuple to the serializer-derived pattern, and relocate it into kolibri/core/auth/viewsets/facility_user.py.

Complexity: Low Target branch: develop

Context

FacilitySearchUsernameViewSet (kolibri/core/public/api.py:368) has values = ("id", "username") and no serializer_class, field_map, consolidate() or annotate_queryset(). It overrides list() entirely: when the facility's learner_can_login_with_no_password is set it serializes through the standard path, otherwise it does a case-insensitive exact match and returns a hand-built [{"username": ..., "id": None}].

The Change

Add a serializer with id and username, and remove the explicit values tuple. The custom list(), its hand-built response path and the get_queryset() filters are unchanged — that path never reaches serialize(), so it is unaffected by the migration.

Relocation

Fold the viewset and its serializer into kolibri/core/auth/viewsets/facility_user.py, which already hosts FacilityUserViewSet, DeletedFacilityUserViewSet and PublicFacilityUserViewSet — it queries FacilityUser, so it belongs with them, and closely-related viewsets share a module. public/api_urls.py imports it from there, as it already does for the other public FacilityUser viewsets (public/api_urls.py:24-26).

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.

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 custom list() logic or the get_queryset() filters
  • kolibri/core/public/api.py keeps its function-based endpoints, InfoViewSet and SyncQueueAPIView; there is no public/viewsets/ package

Acceptance Criteria

  • [ ] Explicit values tuple removed from FacilitySearchUsernameViewSet
  • [ ] Serializer declares id and username
  • [ ] Both code paths in list() continue to produce identical output
  • [ ] The viewset and its serializer live in kolibri/core/auth/viewsets/facility_user.py, moved as a pure-move commit separate from import updates
  • [ ] Existing API tests pass unchanged
  • [ ] Benchmark comparison passes (data hash match + no performance regression)
  • [ ] Comments citing the originating PR/issue added where blame digestion found non-obvious, load-bearing rationale

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:
    # before, from the old path
    python integration_testing/scripts/viewset_serialization_benchmark.py \
        kolibri.core.public.api.FacilitySearchUsernameViewSet \
        --inherit-kolibri-home -o baseline.json
    
    # ... make changes ...
    
    # after, from the new module
    python integration_testing/scripts/viewset_serialization_benchmark.py \
        kolibri.core.auth.viewsets.facility_user.FacilitySearchUsernameViewSet \
        --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: generate user data:

kolibri manage generateuserdata

References

  • Pattern documentation: docs/backend_architecture/api_patterns.rst
  • Example migration: kolibri/core/auth/viewsets/facility_user.py
  • Benchmark script: integration_testing/scripts/viewset_serialization_benchmark.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#14302.

rtibblesbot avatar Jun 09 '26 15:06 rtibblesbot

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

rtibblesbot avatar Jul 16 '26 23:07 rtibblesbot

Plan: Migrate FacilitySearchUsernameViewSet from an explicit values tuple to the serializer-derived pattern, and relocate it to kolibri/core/auth/viewsets/facility_user.py.

  • [ ] Pin the public username-search response shape
  • [ ] Derive the search values from a serializer
  • [ ] Carry forward the rationale the relocation would strip
  • [ ] Move the viewset and serializer into auth/viewsets/facility_user.py
  • [ ] Point the public router at the new module
  • [ ] Self-review the produced code

@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 Aug 18 '26 18:08 rtibblesbot