Add AttendanceSession and AttendanceRecord models with Morango syncing support
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 acreated_byFacilityUser - Timestamps:
session_start_datetime,date_created,date_modified(allDateTimeTzField) - Morango:
morango_model_name = "attendancesession", partition bydataset_id, dataset inferred fromcollection - Validation in
pre_save: creator required on creation; superusers from a different dataset havecreated_byset toNonerather than raising
AttendanceRecord — one record per learner per session:
- Linked to
AttendanceSessionand aFacilityUser - Single field:
present(boolean, defaultFalse) - Unique together:
(attendance_session, user) @property collection— traverses toattendance_session.collectionfor permission resolution- Morango:
morango_model_name = "attendancerecord",calculate_source_id()combines session and user IDs, partition bydataset_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
- [ ]
AttendanceSessionandAttendanceRecordmodels exist inkolibri/core/attendance/ - [ ] Both models extend
AbstractFacilityDataModeland 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 havecreated_byset toNone - [ ]
kolibri.core.attendanceis added toINSTALLED_APPSinkolibri/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_savevalidation logic is tested (missing creator, cross-dataset creator, superuser edge case)
References
Membershipmodel — example of a simpleAbstractFacilityDataModelwith a compositecalculate_source_id()Rolemodel — another minimal syncable facility data modelRoleBasedPermissionsusage — example of declarative permission configuration on a syncable model
🤖 This issue was written with AI assistance, under supervision, review and final edits by [@akolson] 🤖
A few things we probably need to add here:
INSTALLED_APPSregistration. The newkolibri.core.attendanceapp needs to be added to Django'sINSTALLED_APPSinkolibri/deployment/default/settings/base.py.- Facility deletion registration. Both models need to be registered in
kolibri/core/auth/utils/delete.pyfor facility deletion cleanup. RoleBasedPermissionsexample code link.
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:
INSTALLED_APPSregistration — Phase 2, Task 4:kolibri.core.attendanceadded tokolibri/deployment/default/settings/base.py- Facility deletion registration — Phase 2, Task 5: Both
AttendanceRecordandAttendanceSessionadded to_get_class_modelsinkolibri/core/auth/utils/delete.py RoleBasedPermissionspattern — Both models useRoleBasedPermissionsfollowing the existing patterns inExam,Lesson, andCourseSessionmodels. Admins+coaches can create/read/update; only admins can delete.
Plan Summary
Phase 1: App scaffold and models
- Create
kolibri/core/attendance/with__init__.pyandmodels.py AttendanceSession: FK toCollection+FacilityUser(created_by),DateTimeTzFieldtimestamps,pre_savevalidation (reject missing creator, handle superuser cross-dataset), partition bydataset_idAttendanceRecord: FK toAttendanceSession+FacilityUser,presentboolean (default False),unique_togetheron (session, user), compositecalculate_source_id, partition bydataset_id
Phase 2: Registration and deletion
- Add to
INSTALLED_APPS - Register both models in
delete.pyfacility 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_savevalidation (missing creator, cross-dataset non-superuser, superuser edge case) - Test
unique_togetherconstraint onAttendanceRecord
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.
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:
INSTALLED_APPSregistration —kolibri.core.attendanceadded tokolibri/deployment/default/settings/base.py- Facility deletion registration — Both
AttendanceRecordandAttendanceSessionregistered in_get_class_modelsinkolibri/core/auth/utils/delete.py(records before sessions for FK ordering) RoleBasedPermissionspattern — Both models useRoleBasedPermissionsfollowing existing patterns inExam/Lesson/CourseSession
What was implemented
New app: kolibri/core/attendance/
AttendanceSession model:
- FK to
Collection+FacilityUser(created_by) DateTimeTzFieldtimestamps (session_start, date_created, date_modified)pre_savevalidation: rejects missing creator on creation, handles superuser cross-dataset- Morango:
morango_model_name="attendancesession", partitioned bydataset_id - Permissions: admin+coach can CRUD, only admin can delete
AttendanceRecord model:
- FK to
AttendanceSession+FacilityUser(user) presentboolean (default False)unique_togetheron (session, user)- Composite
calculate_source_idfrom session+user IDs collectionproperty traversing to session's collection- Morango:
morango_model_name="attendancerecord", partitioned bydataset_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)
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/extendingAbstractFacilityDataModel - Morango registration (
morango_model_name,calculate_partition,infer_dataset,calculate_source_id) pre_savevalidation (creator required, cross-dataset handling, superuser edge case)INSTALLED_APPSregistration inbase.py- Facility deletion registration in
delete.py - Comprehensive tests covering all required scenarios
Stakeholder Feedback (@rtibbles)
All three items addressed:
- ✅
INSTALLED_APPSregistration — confirmed - ✅ Facility deletion registration — confirmed with correct ordering (records before sessions)
- ✅
RoleBasedPermissionspattern — blocking bug found and fixed (nestedtarget_fieldnot supported bygetattrin_get_target_object)
Commit: f2e1640 — Fix AttendanceRecord RoleBasedPermissions configuration and test import
Closed by #14323