kolibri icon indicating copy to clipboard operation
kolibri copied to clipboard

Set the requesting user on serializers with CurrentUserDefault and CreateOnlyDefault

Open rtibbles opened this issue 2 weeks ago โ€ข 4 comments

๐Ÿ™‚ 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 device user, and to assigned_by on lesson, exam and course session assignments.
  • A creator field should be HiddenField(default=CreateOnlyDefault(CurrentUserDefault())). This applies to the exam creator and to created_by on attendance sessions, lessons and course sessions.
  • HiddenField is write-only, so ValuesViewset stops returning these fields in API responses. This is expected.
  • docs/backend_architecture/api_patterns.rst should 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, creator or assigned_by in 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_by set to the editing coach.
  • [ ] docs/backend_architecture/api_patterns.rst documents 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

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.

rtibbles avatar Nov 17 '26 23:11 rtibbles

Hi! ๐Ÿ‘‹

This issue is available for contribution and supports self-assignment. Here's how to get started:

  • Comment /assign to 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.

learning-equality-bot avatar Nov 17 '26 23:11 learning-equality-bot

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.

learning-equality-bot avatar Nov 18 '26 01:11 learning-equality-bot

/assign

Satish-2004 avatar Nov 18 '26 02:11 Satish-2004

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.

learning-equality-bot avatar Nov 18 '26 02:11 learning-equality-bot