kolibri icon indicating copy to clipboard operation
kolibri copied to clipboard

Add AttendanceSession and AttendanceRecord models with Morango syncing support

Open akolson opened this issue 9 months ago • 5 comments

Overview

Add AttendanceSession and AttendanceRecord models to a new kolibri/core/attendance app. Both models extend AbstractFacilityDataModel, making attendance data syncable across devices via Morango.

Complexity: Medium Target branch: release-v0.19.x

Context

Kolibri is an offline-first platform — coaches may record attendance on one device, and that data needs to sync to other devices in the facility. Morango is the sync framework Kolibri uses for this. All facility data models (users, memberships, roles) already sync via Morango by extending AbstractFacilityDataModel.

The attendance app is a new kolibri/core module, following the same patterns as other facility data.

The Change

Two new syncable models in kolibri/core/attendance/:

AttendanceSession — represents a single attendance-taking event for a classroom:

  • Linked to a Collection (the classroom) and a created_by FacilityUser
  • Timestamps: session_start_datetime, date_created, date_modified (all DateTimeTzField)
  • Morango: morango_model_name = "attendancesession", partition by dataset_id, dataset inferred from collection
  • Validation in pre_save: creator required on creation; superusers from a different dataset have created_by set to None rather than raising

AttendanceRecord — one record per learner per session:

  • Linked to AttendanceSession and a FacilityUser
  • Single field: present (boolean, default False)
  • Unique together: (attendance_session, user)
  • @property collection — traverses to attendance_session.collection for permission resolution
  • Morango: morango_model_name = "attendancerecord", calculate_source_id() combines session and user IDs, partition by dataset_id

Permissions (both models): Admins and coaches can create/read/update; only admins can delete.

App registration: kolibri.core.attendance added to INSTALLED_APPS in kolibri/deployment/default/settings/base.py.

Facility deletion: Both models registered in kolibri/core/auth/utils/delete.py so they are cleaned up when a facility is deleted.

Out of Scope

  • REST API endpoints for attendance data (separate issue)

Acceptance Criteria

General

  • [ ] AttendanceSession and AttendanceRecord models exist in kolibri/core/attendance/
  • [ ] Both models extend AbstractFacilityDataModel and are registered with Morango (morango_model_name, calculate_partition, infer_dataset)
  • [ ] AttendanceRecord.calculate_source_id() returns a deterministic ID combining session and user IDs
  • [ ] AttendanceSession.pre_save() rejects creation without a creator; superusers from a different dataset have created_by set to None
  • [ ] kolibri.core.attendance is added to INSTALLED_APPS in kolibri/deployment/default/settings/base.py
  • [ ] Both models are included in the facility deletion group in kolibri/core/auth/utils/delete.py

Testing

  • [ ] Model creation, field defaults, and cascade deletes are tested
  • [ ] Morango fields (morango_model_name, calculate_partition, infer_dataset, calculate_source_id) are tested
  • [ ] pre_save validation logic is tested (missing creator, cross-dataset creator, superuser edge case)

References

  • Membership model — example of a simple AbstractFacilityDataModel with a composite calculate_source_id()
  • Role model — another minimal syncable facility data model
  • RoleBasedPermissions usage — example of declarative permission configuration on a syncable model

🤖 This issue was written with AI assistance, under supervision, review and final edits by [@akolson] 🤖

akolson avatar Feb 23 '26 14:02 akolson

A few things we probably need to add here:

  • INSTALLED_APPS registration. The new kolibri.core.attendance app needs to be added to Django's INSTALLED_APPS in kolibri/deployment/default/settings/base.py.
  • Facility deletion registration. Both models need to be registered in kolibri/core/auth/utils/delete.py for facility deletion cleanup.
  • RoleBasedPermissions example code link.

rtibbles avatar Mar 02 '26 03:03 rtibbles

Implementation Plan — Attendance Models

Branch: issue-14229-53d11a (targeting release-v0.19.x)

Acknowledgment of stakeholder feedback (@rtibbles)

All three items from your comment have been incorporated into the plan:

  1. INSTALLED_APPS registration — Phase 2, Task 4: kolibri.core.attendance added to kolibri/deployment/default/settings/base.py
  2. Facility deletion registration — Phase 2, Task 5: Both AttendanceRecord and AttendanceSession added to _get_class_models in kolibri/core/auth/utils/delete.py
  3. RoleBasedPermissions pattern — Both models use RoleBasedPermissions following the existing patterns in Exam, Lesson, and CourseSession models. Admins+coaches can create/read/update; only admins can delete.

Plan Summary

Phase 1: App scaffold and models

  • Create kolibri/core/attendance/ with __init__.py and models.py
  • AttendanceSession: FK to Collection + FacilityUser (created_by), DateTimeTzField timestamps, pre_save validation (reject missing creator, handle superuser cross-dataset), partition by dataset_id
  • AttendanceRecord: FK to AttendanceSession + FacilityUser, present boolean (default False), unique_together on (session, user), composite calculate_source_id, partition by dataset_id

Phase 2: Registration and deletion

  • Add to INSTALLED_APPS
  • Register both models in delete.py facility deletion (records deleted before sessions for FK ordering)
  • Generate migration

Phase 3: Tests

  • Test model creation, field defaults, cascade deletes
  • Test Morango fields (morango_model_name, calculate_partition, infer_dataset, calculate_source_id)
  • Test pre_save validation (missing creator, cross-dataset non-superuser, superuser edge case)
  • Test unique_together constraint on AttendanceRecord

Environment Note

Python 3.14 environment — kolibri package requires <3.14 but tests pass via PYTHONPATH. The teyit pre-commit hook fails on Python 3.14 (ast._Unparser removed) but black formatting works.

rtibblesbot avatar Mar 03 '26 20:03 rtibblesbot

Implementation Progress: Attendance Models (All Phases Complete)

Branch: issue-14229-53d11a Commit: 486c291f6e

Acknowledgment of @rtibbles feedback

All three items from the stakeholder comment have been incorporated:

  1. INSTALLED_APPS registration — kolibri.core.attendance added to kolibri/deployment/default/settings/base.py
  2. Facility deletion registration — Both AttendanceRecord and AttendanceSession registered in _get_class_models in kolibri/core/auth/utils/delete.py (records before sessions for FK ordering)
  3. RoleBasedPermissions pattern — Both models use RoleBasedPermissions following existing patterns in Exam/Lesson/CourseSession

What was implemented

New app: kolibri/core/attendance/

AttendanceSession model:

  • FK to Collection + FacilityUser (created_by)
  • DateTimeTzField timestamps (session_start, date_created, date_modified)
  • pre_save validation: rejects missing creator on creation, handles superuser cross-dataset
  • Morango: morango_model_name="attendancesession", partitioned by dataset_id
  • Permissions: admin+coach can CRUD, only admin can delete

AttendanceRecord model:

  • FK to AttendanceSession + FacilityUser (user)
  • present boolean (default False)
  • unique_together on (session, user)
  • Composite calculate_source_id from session+user IDs
  • collection property traversing to session's collection
  • Morango: morango_model_name="attendancerecord", partitioned by dataset_id
  • Permissions: same as session via target_field="attendance_session__collection"

Test results

19 passed, 0 failed

Tests cover:

  • Model creation and field defaults
  • Morango fields (morango_model_name, calculate_partition, infer_dataset, calculate_source_id)
  • pre_save validation (missing creator, cross-dataset non-superuser, superuser edge case)
  • unique_together constraint on AttendanceRecord
  • Cascade deletes (collection→session, session→record)

Files changed (8 files, +467 lines)

  • kolibri/core/attendance/__init__.py (new)
  • kolibri/core/attendance/models.py (new - 124 lines)
  • kolibri/core/attendance/migrations/0001_initial_attendance_models.py (new)
  • kolibri/core/attendance/migrations/__init__.py (new)
  • kolibri/core/attendance/test/__init__.py (new)
  • kolibri/core/attendance/test/test_models.py (new - 205 lines)
  • kolibri/deployment/default/settings/base.py (+1 line)
  • kolibri/core/auth/utils/delete.py (+4 lines)

rtibblesbot avatar Mar 03 '26 20:03 rtibblesbot

Self-Review Progress Update

Completed self-review of the AttendanceSession and AttendanceRecord models implementation on branch issue-14229-53d11a.

Findings and Fixes

Blocking (fixed): AttendanceRecord.permissions used target_field="attendance_session__collection" but RoleBasedPermissions._get_target_object() uses getattr() which does not support Django ORM __ lookups (confirmed by TODO comment in base.py:128 and grep showing no other model uses nested target_field). Fixed by using target_field="collection" (resolves via @property) and collection_field="attendance_session__collection" (ORM path for queryset filtering).

Suggestion (fixed): Moved inline DevicePermissions import from test method body to file top per project conventions.

Acceptance Criteria Status

All criteria from the issue are met:

  • Models exist in kolibri/core/attendance/ extending AbstractFacilityDataModel
  • Morango registration (morango_model_name, calculate_partition, infer_dataset, calculate_source_id)
  • pre_save validation (creator required, cross-dataset handling, superuser edge case)
  • INSTALLED_APPS registration in base.py
  • Facility deletion registration in delete.py
  • Comprehensive tests covering all required scenarios

Stakeholder Feedback (@rtibbles)

All three items addressed:

  1. ✅ INSTALLED_APPS registration — confirmed
  2. ✅ Facility deletion registration — confirmed with correct ordering (records before sessions)
  3. ✅ RoleBasedPermissions pattern — blocking bug found and fixed (nested target_field not supported by getattr in _get_target_object)

Commit: f2e1640 — Fix AttendanceRecord RoleBasedPermissions configuration and test import

rtibblesbot avatar Mar 03 '26 21:03 rtibblesbot

Closed by #14323

nucleogenesis avatar Mar 06 '26 00:03 nucleogenesis