kolibri icon indicating copy to clipboard operation
kolibri copied to clipboard

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

This viewset was relocated to kolibri/plugins/user_auth/viewsets.py and given cursor pagination in #14820 (its sole consumer is the sign-in page). Only the serializer migration remains — the serializer goes inline in that module, so there is no code-movement step.

Complexity: Low Target branch: develop

Context

FacilityUsernameViewSet in kolibri/plugins/user_auth/viewsets.py has a single-field values = ("username",) tuple with no serializer_class, field_map, consolidate(), or annotate_queryset(). The get_queryset() has branching logic based on valid_app_key_on_request — returning all users in app context, or filtering to no-password learners otherwise. It paginates via FacilityUsernamePagination (cursor pagination) and rejects searches shorter than 3 characters, both added in #14820.

The Change

Add a serializer for FacilityUsernameViewSet with the username field, inline in kolibri/plugins/user_auth/viewsets.py. Remove the explicit values tuple.

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

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 get_queryset() branching logic
  • Changes to the cursor pagination or minimum-search-length behaviour added in #14820

Acceptance Criteria

  • [ ] Explicit values tuple removed from FacilityUsernameViewSet
  • [ ] Serializer (inline in kolibri/plugins/user_auth/viewsets.py) has the username field
  • [ ] 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.user_auth.viewsets.FacilityUsernameViewSet \
        --inherit-kolibri-home -o baseline.json
    
    # ... make changes ...
    
    python integration_testing/scripts/viewset_serialization_benchmark.py \
        kolibri.plugins.user_auth.viewsets.FacilityUsernameViewSet \
        --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
  • Pagination and relocation precursor: #14820
  • Viewset location: kolibri/plugins/user_auth/viewsets.py
  • 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.

  • The benchmark comparison run uses the dotted path kolibri.plugins.user_auth.viewsets.FacilityUsernameViewSet.

rtibbles avatar Mar 02 '26 04:03 rtibbles

Plan: Migrate FacilityUsernameViewSet from an explicit values = ("username",) tuple to the serializer-derived pattern by adding an inline FacilityUsernameSerializer.

  • [ ] Verify baseline tests pass
  • [ ] Capture benchmark baseline (before the migration)
  • [ ] Add serializer and migrate viewset
  • [ ] Run 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

Code migration complete — benchmark blocked by script incompatibility

The serializer migration (Tasks 1 and 3) is done and committed:

  • FacilityUsernameSerializer added inline in kolibri/plugins/user_auth/viewsets.py
  • serializer_class = FacilityUsernameSerializer set on the viewset
  • values = ("username",) removed
  • All 5 existing tests pass unchanged

Benchmark blocker: The benchmark script (integration_testing/scripts/viewset_serialization_benchmark.py) cannot run against FacilityUsernameViewSet. Its get_queryset_for_viewset() function first checks for a class-level queryset attribute (none exists on this viewset), then falls back to calling viewset.get_queryset() on a bare viewset instance — which fails because get_queryset() calls valid_app_key_on_request(self.request) and self.request is not set on a bare instance.

This failure is pre-existing — the benchmark exits with code 1 against the unmigrated viewset too. The fix would require either:

  1. Adding a queryset = FacilityUser.objects.all() class attribute to the viewset so the benchmark can pick it up via getattr(viewset_class, "queryset", None), or
  2. Updating get_queryset_for_viewset in the benchmark script to set up a mock request before calling get_queryset()

Should I add the queryset class attribute to FacilityUsernameViewSet? That would allow the benchmark to run without changing the API behaviour (the get_queryset() override still controls actual request handling).

rtibblesbot avatar Jun 09 '26 15:06 rtibblesbot

Let's go with 2 - it's more generalizable.

rtibbles avatar Jun 09 '26 16:06 rtibbles