kolibri icon indicating copy to clipboard operation
kolibri copied to clipboard

AttendanceSession.date_modified never updates after creation

Open namanjain24-sudo opened this issue 1 month ago β€’ 6 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.

Observed behavior

AttendanceSession exposes date_modified through the API, but nothing ever updates it. The field is declared with a creation-time default and no auto-update (kolibri/core/attendance/models.py:45-46):

date_created = DateTimeTzField(default=local_now, editable=False)
date_modified = DateTimeTzField(default=local_now)

It is never assigned anywhere else β€” the only other references in the module are the migration, the serializer's field list, and a not-None assertion in test/test_models.py:39. The serializer also marks it read-only, so a client cannot set it either (kolibri/core/attendance/viewsets/attendance_session.py:62):

read_only_fields = ("created_by", "date_created", "date_modified")

and AttendanceSessionSerializer.update only writes validated_data, which cannot contain it.

So date_modified is fixed at creation and reports the creation time forever, no matter how many times the session's attendance is edited afterwards.

Other "modified"/"updated" timestamps in the codebase are kept current, either with auto_now or by explicit assignment:

  • kolibri/core/device/models.py:629 β€” updated_at = models.DateTimeField(auto_now=True)
  • kolibri/core/device/models.py:517 β€” updated = models.DateTimeField(auto_now=True)
  • kolibri/core/discovery/models.py:102 β€” last_accessed = models.DateTimeField(auto_now=True)
  • kolibri/core/content/utils/channel_import.py:996 β€” channel.last_updated = local_now()

AttendanceSession.date_modified is neither. (Job.time_updated in kolibri/core/tasks/models.py:32 is also never written, but it is not exposed through an API serializer.)

Errors and logs

No error β€” the write succeeds and returns 200, the value simply does not move. Driving the real API as a coach:

created : date_created=2026-10-25 00:12:59.158269+05:30  date_modified=2026-10-25 00:12:59.158277+05:30
PATCH status=200   attendance record present flipped False -> True
after   : date_modified=2026-10-25 00:12:59.158277+05:30
date_modified CHANGED = False

Expected behavior

Editing a session β€” including flipping a learner's present flag through the nested attendance_records write path β€” should move date_modified forward, so the field means what its name says.

User-facing consequences

Anything that reads date_modified to answer "when was this attendance last changed?" gets the creation time instead. Today that is mostly latent: I could not find a frontend consumer of the field, so this is low severity right now. It matters because the field is already part of the serialized API surface, so the wrong value is what any future consumer β€” a "last edited" column in a coach report, or an audit/export β€” would be built on, and because a stale value is harder to notice than a missing one.

A note on where the fix belongs

The obvious fix, auto_now=True on the model, looks wrong here. Morango deserialization saves through the same path (app_model.save(update_dirty_bit_to=False) in morango/sync/operations.py:454), so an auto_now field would be rewritten with the receiving device's clock on every sync, diverging from the value the authoring device sent. Setting it in AttendanceSessionSerializer.update instead confines the change to genuine API edits and leaves synced-in values intact β€” which also matches how channel.last_updated is handled, the other DateTimeTzField of this kind in the tree.

Steps to reproduce

As a coach of the classroom, create a session and then edit it:

r = self.client.post(
    "/api/attendance/attendancesession/",
    {"collection": classroom.id,
     "attendance_records": [{"user": learner.id, "present": False}]},
    format="json",
)
sid = r.data["id"]
before = AttendanceSession.objects.get(id=sid)

time.sleep(1.1)
self.client.patch(
    f"/api/attendance/attendancesession/{sid}/",
    {"attendance_records": [{"user": learner.id, "present": True}]},
    format="json",
)
after = AttendanceSession.objects.get(id=sid)

assert after.date_modified != before.date_modified   # fails on develop

Result on develop: the PATCH returns 200 and the record's present really does flip to True, but date_modified is byte-for-byte what it was at creation.

Context

Affects develop. kolibri/core/attendance/ is the newest module in the tree. Separate from #15666, which is about AttendanceRecord's dataset invariant in the same module. Low severity β€” filing it because it is a small, contained correctness gap in a field that is already published through the API.

AI usage

Claude Code (on my instruction to keep scanning the newer code) noticed date_modified had no auto-update while reading the attendance models for an unrelated issue, and I asked it to confirm before anything was written up. It checked that the field is never assigned anywhere in the module, then drove the real API as a logged-in coach to show the value unchanged after a successful PATCH that did modify the attendance. It also surveyed how every other modified/updated timestamp in the tree is maintained, which is what establishes this as inconsistent rather than intentional, and checked morango's deserialization save path to work out that auto_now would be the wrong remedy. I reviewed the survey of the other timestamp fields, the reasoning about where the fix belongs, and the reproduction's result on current develop before filing, and I am deliberately flagging this as low severity rather than dressing it up.

namanjain24-sudo avatar Oct 25 '26 01:10 namanjain24-sudo

@rtibblesbot please validate this issue.

rtibbles avatar Oct 25 '26 15:10 rtibbles

Verdict: claim holds Evidence: kolibri/core/attendance/models.py:45 and kolibri/core/attendance/viewsets/attendance_session.py:62,79-90 on develop at 44a4e68aac

  • date_modified = DateTimeTzField(default=local_now) has no auto_now.
  • The serializer marks it read-only, and update() writes only validated_data plus AttendanceRecord.update_or_create. Nothing sets date_modified.
  • AbstractFacilityDataModel has no save hook that touches modified timestamps.
  • No existing issue reports this. #15666 covers a different invariant in the same module.

Two supporting statements in the issue were wrong, and I've corrected them:

  • kolibri/core/attendance/test/test_models.py:39 also references the field, but only to assert it is not None.
  • Job.time_updated (kolibri/core/tasks/models.py:32) is never written either, so date_modified is not the only timestamp of this kind that never updates.

Changes

  • Observed behavior, "only other references": added test/test_models.py:39. The list omitted that reference.
  • "Every other … timestamp is kept current" β†’ "Other …": Job.time_updated (kolibri/core/tasks/models.py:32) is never written.
  • "is the only one that is neither" β†’ "is neither", plus a note on Job.time_updated. The original sentence was false.

Written by rtibblesbot, an LLM-based coding agent.

rtibblesbot avatar Oct 25 '26 17:10 rtibblesbot

@rtibbles do i need to directly open the pull request without asking for assign

gitseraj avatar Oct 27 '26 12:10 gitseraj

Hi! πŸ‘‹

Thanks so much for your interest! This issue is already assigned. Visit Contributing guidelines to learn about the contributing process and how to find suitable issues. If there are no unassigned 'help wanted' issues available, please wait until new ones are added.

We really appreciate your willingness to help. 😊

Are you preparing for Google Summer of Code? See our GSoC guidelines.

learning-equality-bot avatar Oct 27 '26 12:10 learning-equality-bot

8. @rtibbles do i need to directly open the pull request without asking for assign

Hi @gitseraj, I'm already working on the fix for this issue, and it has been assigned to me. Thanks! πŸ™‚

namanjain24-sudo avatar Oct 27 '26 14:10 namanjain24-sudo

Note that while all the issues you have opened are valid, we do deliberately limit the number of help wanted issues to ensure we are able to review contributor PRs in a timely fashion. In future, please be mindful of opening PRs before you have been assigned to an issue by our team.

rtibbles avatar Oct 27 '26 16:10 rtibbles