kolibri icon indicating copy to clipboard operation
kolibri copied to clipboard

Remove legacy values/field_map code paths from BaseValuesViewset

Open rtibbles opened this issue 9 months ago • 1 comments

❌ This issue is not open for contribution. Visit Contributing guidelines to learn about the contributing process and how to find suitable issues.

Overview

With every viewset migrated, remove the legacy explicit values/field_map code paths so serializer_class is the only supported way to define a viewset's serialization contract.

Complexity: Medium Target branch: develop

Context

BaseValuesViewset._do_initialize (kolibri/core/api.py:258) branches on whether the class declares a values tuple, building the engine from the legacy explicit path or from the serializer. Every legacy site carries a TODO(#14302).

The Change

Remove from kolibri/core/api.py:

  • The has_explicit_values branch in _do_initialize (api.py:259-265), leaving serializer derivation as the only path — a class with no serializer_class then raises at init.
  • generate_serializer() (api.py:294) and its caching in get_serializer_class() (api.py:337-348) — it exists only to synthesise a serializer for viewsets that declare values/field_map instead of one.

Remove from kolibri/core/utils/values_viewset/:

  • ValuesEngine.from_explicit (engine.py:228)
  • normalize_field_map (field_map.py:375)
  • _LegacyFieldMap (field_map.py:337) — the mutate-in-place map_row path that passes unclaimed raw values() keys through to output
  • _LegacyCallableFieldEntry (field_map.py:133)

Then expose values as a read-only property on the viewset, returning the engine's tuple. Doing this earlier would collide with viewsets that still declare their own tuple. ExamViewset (exams/viewsets/exam.py:470) and MasteryLogViewSet (logger/viewsets/mastery_log.py:94) read that property instead of _engine.values.

The two viewsets left without a serializer

Both inherit BaseValuesViewset but never call serialize(), so they have no serializer and would raise at init once the legacy branch goes. Neither can simply have its values tuple deleted:

  • ContentNodeProgressViewset (moved to content/viewsets/contentnode/progress.py by #14300) uses its values tuple — generate_response() applies it with .values(*self.values) to a ContentSummaryLog queryset, not to the viewset's own ContentNode queryset. A serializer_class would therefore describe the wrong model. Drop BaseValuesViewset from its bases (TreeQueryMixin, GenericViewSet, ListModelMixin are all it needs — the pagination classes do not touch the engine) and replace the class attribute with a module-level tuple that generate_response() reads.
  • NetworkLocationFacilitiesView (discovery/viewsets/network_location.py:173) has a genuinely dead values tuple: it overrides retrieve() and builds the Response by hand from _RemoteFacilitySerializer. Drop the tuple and BaseValuesViewset with it, inheriting GenericViewSet.

Both then need a serializer_class for DRF schema generation, which generate_serializer() used to supply — a bare Serializer stub, as ImportMetadataViewset already does (content/public_api.py:27).

RemoteViewSet (moved to content/viewsets/remote.py by #14306) is an abstract base that is never instantiated directly; confirm it carries no values/field_map and needs no stub.

Documentation and tests

  • docs/backend_architecture/api_patterns.rst: delete the "Migrating from Explicit Values" and "Explicit Values (Legacy)" sections, and the best-practice bullet that contrasts source with field_map (:369).
  • kolibri/core/test/test_api.py: delete TestLegacyViewset (:2045), and convert the explicit-values viewsets used for convenience in TestDevModeSafeguards (:2423, :2436, :2547) to serializer_class.

Out of Scope

  • Changes to the API response shape of any endpoint
  • Changes to annotate_queryset(), consolidate() or deferred_fields behavior

Acceptance Criteria

  • [ ] All migration issues under this refactor are closed (this issue is blocked by them)
  • [ ] Legacy values/field_map branch and generate_serializer() removed from kolibri/core/api.py
  • [ ] ValuesEngine.from_explicit, normalize_field_map, _LegacyFieldMap and _LegacyCallableFieldEntry removed
  • [ ] values exposed as a read-only property, read by ExamViewset and MasteryLogViewSet in place of _engine.values
  • [ ] ContentNodeProgressViewset no longer inherits BaseValuesViewset, and reads its column list from a module-level tuple
  • [ ] NetworkLocationFacilitiesView no longer inherits BaseValuesViewset, and its dead values tuple is gone
  • [ ] Both carry a serializer_class stub for schema generation; RemoteViewSet confirmed to need none
  • [ ] serializer_class is the only supported pattern; no TODO(#14302) markers remain
  • [ ] Legacy sections removed from docs/backend_architecture/api_patterns.rst
  • [ ] TestLegacyViewset deleted and the TestDevModeSafeguards fixtures converted
  • [ ] All existing API tests pass unchanged

References

  • Pattern documentation: docs/backend_architecture/api_patterns.rst
  • BaseValuesViewset: kolibri/core/api.py
  • Legacy engine surface: kolibri/core/utils/values_viewset/engine.py, field_map.py
  • Unit tests: kolibri/core/test/test_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

Plan: Make serializer_class the only supported way to define a viewset's serialization contract, deleting the explicit values/field_map engine path and everything that exists to support it.

  • [x] Take ContentNodeProgressViewset off BaseValuesViewset
  • [x] Take NetworkLocationFacilitiesView off BaseValuesViewset
  • [x] Expose values as a read-only viewset property
  • [x] Re-express the benchmark's explicit arms as serializer-derived
  • [x] Re-express the two legacy test fixtures whose behaviour is not legacy
  • [x] Delete the legacy explicit values/field_map path
  • [x] 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 19 '26 03:08 rtibblesbot