kolibri icon indicating copy to clipboard operation
kolibri copied to clipboard

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

Complexity: Medium Target branch: develop

Context

FacilityDatasetViewSet in kolibri/core/auth/api.py has an explicit values tuple (12 fields), serializer_class = FacilityDatasetSerializer, and a field_map with two lambda entries: allow_guest_access (calls a function) and is_full_facility_import (function reference). It has no consolidate() or annotate_queryset(). It also has two custom @action methods (resetsettings, update_pin) that use the serializer directly.

The Change

Remove the explicit values tuple and field_map from FacilityDatasetViewSet so that values and field mappings are derived from FacilityDatasetSerializer. The allow_guest_access and is_full_facility_import fields will need to be represented in the serializer as ValuesMethodField(sources=...) fields — plain SerializerMethodField is rejected with a TypeError by the introspection, since it cannot declare which row columns the method reads. The custom action methods remain 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 FacilityDataset domain into kolibri/core/auth/viewsets/facility_dataset.py as a pure-move commit: the viewset(s), their serializers (inline above the viewsets), and any domain-specific filters and permission classes (FacilityDatasetFilter, _is_full_facility_import). 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.

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

  • Changes to the resetsettings or update_pin actions

  • Deleting kolibri/core/auth/api.py/serializers.py — that happens in the final issue touching these files (#14303)

Acceptance Criteria

  • [ ] Explicit values tuple and field_map removed from FacilityDatasetViewSet

  • [ ] Serializer updated with all read-path fields including allow_guest_access and is_full_facility_import

  • [ ] Existing API tests pass unchanged

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

  • [ ] The migrated domain lives in kolibri/core/auth/viewsets/facility_dataset.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

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.auth.api.FacilityDatasetViewSet \
        --inherit-kolibri-home -o baseline.json
    
    # ... make changes ...
    
    python integration_testing/scripts/viewset_serialization_benchmark.py \
        kolibri.core.auth.api.FacilityDatasetViewSet \
        --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 (creates facility datasets):

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.core.auth.viewsets.facility_dataset.FacilityDatasetViewSet).

rtibbles avatar Mar 02 '26 04:03 rtibbles

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

rtibblesbot avatar Jun 09 '26 15:06 rtibblesbot

Plan: Migrate FacilityDatasetViewSet from explicit values/field_map to serializer-derived pattern, then relocate the FacilityDataset domain to kolibri/core/auth/viewsets/facility_dataset.py.

  • [ ] Add test coverage for computed response fields
  • [ ] Run benchmark baseline
  • [ ] Update FacilityDatasetSerializer with ValuesMethodField fields
  • [ ] Remove values and field_map from FacilityDatasetViewSet
  • [ ] Post-migration benchmark comparison
  • [ ] Commit the migration
  • [ ] Blame digestion
  • [ ] Create viewsets/facility_dataset.py (pure-move)
  • [ ] Lint the new file
  • [ ] Run full test suite to verify the move
  • [ ] Commit the pure-move

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 21: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