Migrate ContentRequestViewset to serializer-derived values pattern
❌ This issue is not open for contribution. Visit Contributing guidelines to learn about the contributing process and how to find suitable issues.
Overview
Migrate ContentRequestViewset to the serializer-derived pattern, then finish the content app's reorganisation: move everything left in kolibri/core/content/api.py, serializers.py and public_api.py into kolibri/core/content/viewsets/ (or a suitable non-viewset home) and delete those three modules.
Complexity: Medium Target branch: develop
Context
ContentRequestViewset (kolibri/core/content/api.py:1438) mixes ReadOnlyValuesViewset with CreateModelMixin and a custom delete(). It has no field_map or consolidate(), and an annotate_queryset() adding a has_removal Exists subquery it then filters on.
Its serializer_class, ContentDownloadRequestSerializer (content/serializers.py:268), is the write serializer: it declares ("id", "contentnode_id", "metadata", "source_instance_id"), while the viewset's values tuple returns eight fields — id, requested_at, reason, contentnode_id, metadata, status, facility, source_id. Deriving from it unchanged would drop five response fields and add source_instance_id.
By the time this issue runs, #14312 and #14300 have created content/viewsets/ (with channel_metadata.py and the contentnode/ subpackage) and content/utils/cache.py.
The Change
Bring the serializer up to the read contract and keep one serializer for both directions:
- Add
requested_at,reason,status,facilityandsource_idasread_only=Truefields, so the derivedvaluesmatch today's tuple exactly. - Mark
source_instance_idwrite_only=True— it is not in the current response. metadatastays a nested plainSerializerover the JSONField; the engine handles those on the regular-field path (kolibri/core/utils/values_viewset/introspect.py:102-112), so it needs no special treatment.- Then remove the explicit
valuestuple. - Verify the
create()response against the existing tests — it gains the read-only fields.
Relocation
Move the ContentRequest domain to content/viewsets/content_request.py, with ContentRequestFilter, ContentDownloadRequestSerializer and ContentDownloadRequestMetadataSerializer inline. Declare the UUIDInFilter helper it needs locally, as auth/viewsets/facility_user.py:56-64 does.
Then rehome what is left and delete the old modules:
| Code | Destination |
|---|---|
ContentNodeGranularViewset + ContentNodeGranularSerializer |
content/viewsets/contentnode/granular.py |
FileViewset + FileSerializer, LanguageSerializer, LowerCaseField |
content/viewsets/file.py |
RemoteChannelViewSet |
content/viewsets/remote_channel.py |
ShareFileView + ShareFileSerializer |
content/viewsets/share_file.py |
ImportMetadataViewset + its OptionalPagination (public_api.py) |
content/viewsets/import_metadata.py |
RemoteMixin, RemoteViewSet, REMOTE_ETAG_CACHE_KEY, REMOTE_URL_PARAM |
content/viewsets/remote.py — a non-routed module in the package, as logger/viewsets/filters.py is |
ChannelThumbnailView |
content/views.py, which already holds the app's plain Django views |
OptionalPageNumberPagination (api.py:699) |
delete — a byte-identical copy already lives at kolibri/core/utils/pagination.py:135; repoint content_request.py, file.py and the logger viewsets (attempt_log.py:9, mastery_log.py:15) at it |
DynamicFieldsModelSerializer |
delete — it has no subclasses anywhere in the tree |
Then delete content/api.py, content/serializers.py and content/public_api.py, and update every import: content/api_urls.py, public/api_urls.py, logger/viewsets/, kolibri/core/test/test_drf_mixins.py (LanguageSerializer), tests and cross-references.
Moving code breaks GitHub blame (local git blame -C -C still works), so digest the history the move obscures:
- 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).
- 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.
- 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 read API response shape — the eight fields must stay as they are
- Modifications to existing tests — if tests need to change, the migration is wrong
- Write-path behaviour —
CreateModelMixin, the serializer'screate()and the customdelete()are unaffected beyond the read-only fields appearing in the create response
Acceptance Criteria
- [ ] Explicit
valuestuple removed fromContentRequestViewset - [ ]
ContentDownloadRequestSerializercarries all eight read fields, withsource_instance_idwrite-only - [ ] The create response is verified against existing tests
- [ ] The migrated domain lives in
content/viewsets/content_request.py, moved as a pure-move commit separate from import updates - [ ] Every table row above rehomed;
DynamicFieldsModelSerializerdeleted - [ ]
content/api.py,content/serializers.pyandcontent/public_api.pydeleted, with no API module left outsidecontent/viewsets/exceptviews.py - [ ] 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.content.api.ContentRequestViewset \ --inherit-kolibri-home -o baseline.json # ... make changes ... # after, from the new module python integration_testing/scripts/viewset_serialization_benchmark.py \ kolibri.core.content.viewsets.content_request.ContentRequestViewset \ --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, then create content requests via kolibri manage shell:
kolibri manage importchannel network 95a52b386f2c485cb97dd60901674a98
kolibri manage importcontent network 95a52b386f2c485cb97dd60901674a98
Then in kolibri manage shell:
from kolibri.core.auth.models import Facility
from kolibri.core.content.models import ContentNode, ContentDownloadRequest
facility = Facility.objects.first()
for node in ContentNode.objects.all()[:50]:
ContentDownloadRequest.objects.get_or_create(
contentnode_id=node.id,
facility=facility,
defaults={"reason": 0, "source_id": facility.id, "metadata": {}},
)
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.
Watching for resolution on behalf of learningequality/kolibri#14302.
Watching for resolution on behalf of learningequality/kolibri#14302.
Plan: Derive ContentRequestViewset's values from its serializer, then empty kolibri/core/content/api.py, serializers.py and public_api.py into content/viewsets/ and delete all three.
- [ ] Pin the ContentRequest read contract and capture the benchmark baseline
- [ ] Derive ContentRequestViewset's values from its serializer
- [ ] Carry forward the rationale the relocation would strip
- [ ] Copy the ContentRequest domain into content/viewsets/content_request.py
- [ ] Point the ContentRequest importers at the new module
- [ ] Move ContentNodeGranular into content/viewsets/contentnode/granular.py
- [ ] Move the File and Language serialization into content/viewsets/file.py
- [ ] Move RemoteChannelViewSet into content/viewsets/remote_channel.py
- [ ] Move ShareFileView into content/viewsets/share_file.py
- [ ] Move ImportMetadataViewset into content/viewsets/import_metadata.py and delete public_api.py
- [ ] Move ChannelThumbnailView into views.py, repoint the pagination importers, delete api.py
- [ ] Rehome PublicChannelSerializer and delete serializers.py
- [ ] 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