Set the requesting user on serializers with CurrentUserDefault and CreateOnlyDefault
๐ Looking for an issue? Welcome! This issue is open for contribution. If this is the first time youโre requesting an issue, please:
- Read Contributing guidelines carefully. Pay extra attention to Using generative AI. Pull requests and comments that donโt follow the guidelines wonโt be answered.
- Confirm that youโve read the guidelines in your comment.
Overview
Kolibri serializers set the requesting user by hand at nine sites in five serializers, using four different mechanisms: a create() override, writes to data in to_internal_value, writes to attrs in validate, and reads of self.context["request"].user in assignment helpers. DRF already covers both cases: HiddenField(default=CurrentUserDefault()) for a user set on every save, and HiddenField(default=CreateOnlyDefault(CurrentUserDefault())) for a creator set only on create.
Complexity: Low Target branch: develop
The Change
- A serializer field that holds the requesting user on every save should be
HiddenField(default=CurrentUserDefault()). This applies to the pinned deviceuser, and toassigned_byon lesson, exam and course session assignments. - A creator field should be
HiddenField(default=CreateOnlyDefault(CurrentUserDefault())). This applies to the examcreatorand tocreated_byon attendance sessions, lessons and course sessions. HiddenFieldis write-only, so ValuesViewset stops returning these fields in API responses. This is expected.docs/backend_architecture/api_patterns.rstshould document both patterns, alongside the query-parameter and body guidance from #15891.
Out of Scope
content_request.py, which uses the requesting user to build and match download requests rather than to set a field.
Acceptance Criteria
General
- [ ] No serializer sets a user field from
self.context["request"].user. - [ ] A client-supplied
user,created_by,creatororassigned_byin a request body is ignored. - [ ] When a second coach edits a lesson, course session, exam or attendance session, its creator is unchanged, and assignments added in that edit have
assigned_byset to the editing coach. - [ ]
docs/backend_architecture/api_patterns.rstdocuments both patterns, with an example of each.
Testing
- [ ] Tests cover create, and edit by a second coach, for each creator field and each
assigned_by. - [ ] Tests cover a client-supplied value being ignored.
References
- DRF advanced field defaults:
CurrentUserDefaultandCreateOnlyDefault. - User set in
create():pinned_device.py#L21-L27 - Creator set in
to_internal_value:lesson.py#L129,course_session.py#L174 - Creator set in
validate:exam.py#L205-L207,attendance_session.py#L66-L67 assigned_by:lesson.py#L261-L264,exam.py#L270-L273,course_session.py#L283-L287,course_session.py#L325-L328
AI usage
I asked Claude Code to find where serializers set the requesting user. I chose the two patterns, brought assigned_by into scope, and decided to drop the fields from responses. Claude Code checked each site, reproduced the lesson edit to confirm created_by is kept, and drafted the issue.
Hi! ๐
This issue is available for contribution and supports self-assignment. Here's how to get started:
- Comment
/assignto assign yourself to this issue - You can have up to 2 issues assigned at a time across all community repos
- Dropping an issue has a 7-day cooldown before the slot opens up
- Link your pull request to this issue when you submit it
๐ Read the Contributing guidelines before starting.
Are you preparing for Google Summer of Code? See our GSoC guidelines.
Hi! ๐
Thanks for your interest! This issue supports self-assignment. Comment /assign to assign yourself.
Visit Contributing guidelines to learn about the contributing process. ๐
Are you preparing for Google Summer of Code? See our GSoC guidelines.
/assign
Hi! ๐
You've been assigned to this issue. Here's what to do next:
- Read the issue description carefully and make sure you understand the requirements
- Link your pull request to this issue when you submit it
- If you can no longer work on this, unassign yourself so others can pick it up
Good luck! ๐
Are you preparing for Google Summer of Code? See our GSoC guidelines.