Session 3-A - core models: community.school.term, .level (admin-defined, so the same module fits a Grade 1-12 school or a Beginner-Advanced language school with no code change), .class (auto-creates a linked slide.channel for LMS glue), .student, .enrollment, and .attendance, plus is_teacher on res.partner. Class enrolled_count and enrollment attendance_rate are stored/non-stored computes driven by the enrollment/attendance one2many chains. Session 3-B - teacher attendance + at-risk reporting: a portal page at /school/attendance (auth='user', scoped to classes where teacher_id matches the logged-in user's partner - works whether the teacher is an internal or portal user) with a roster and batch save, built as a v19 Interaction (same pattern as event_qr_ticketing's check-in page). Marking a student absent queues a configurable notice to the parent partner. Enrollment.is_at_risk flags students below a configurable attendance threshold (School settings page, same <app>/<block>/<setting> pattern as community_membership), plus an admin pivot attendance report and an At-Risk Students list. Verified against a live Odoo 19 + Postgres 16 container: 14/14 automated tests pass (class/slide-channel creation, enrolled_count tracking incl. withdrawal, attendance_rate and at-risk computation, unique enrollment+date constraint, absence email queuing), plus a full manual live run - created a term/level/class/student/enrollment via JSON-RPC, loaded the actual /school/attendance page as the teacher, POSTed a batch save marking the student absent, and confirmed both the attendance record and the queued "Absence notice" mail.mail record. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
73 lines
3.0 KiB
Python
73 lines
3.0 KiB
Python
from odoo import fields, http
|
|
from odoo.exceptions import AccessDenied
|
|
from odoo.http import request
|
|
|
|
|
|
class SchoolAttendanceController(http.Controller):
|
|
|
|
def _get_teacher_classes(self):
|
|
partner = request.env.user.partner_id
|
|
return request.env['community.school.class'].sudo().search([('teacher_id', '=', partner.id)])
|
|
|
|
@http.route(['/school/attendance'], type='http', auth='user', website=True)
|
|
def attendance_home(self, class_id=None, date=None, **kwargs):
|
|
classes = self._get_teacher_classes()
|
|
if not classes:
|
|
return request.render('community_school.portal_no_classes', {})
|
|
|
|
selected_class = classes.filtered(lambda c: c.id == int(class_id)) if class_id else classes[:1]
|
|
if not selected_class:
|
|
selected_class = classes[:1]
|
|
selected_date = date or fields.Date.context_today(request.env.user).isoformat()
|
|
|
|
Enrollment = request.env['community.school.enrollment'].sudo()
|
|
enrollments = Enrollment.search([
|
|
('class_id', '=', selected_class.id), ('state', '=', 'enrolled'),
|
|
])
|
|
existing_by_enrollment = {
|
|
attendance.enrollment_id.id: attendance
|
|
for attendance in request.env['community.school.attendance'].sudo().search([
|
|
('class_id', '=', selected_class.id), ('date', '=', selected_date),
|
|
])
|
|
}
|
|
roster = [
|
|
{'enrollment': enrollment, 'attendance': existing_by_enrollment.get(enrollment.id)}
|
|
for enrollment in enrollments
|
|
]
|
|
|
|
return request.render('community_school.portal_attendance', {
|
|
'classes': classes,
|
|
'selected_class': selected_class,
|
|
'selected_date': selected_date,
|
|
'roster': roster,
|
|
})
|
|
|
|
@http.route(['/school/attendance/save'], type='jsonrpc', auth='user', website=True)
|
|
def attendance_save(self, class_id=None, date=None, lines=None, **kwargs):
|
|
classes = self._get_teacher_classes()
|
|
klass = classes.filtered(lambda c: c.id == int(class_id))
|
|
if not klass:
|
|
raise AccessDenied()
|
|
|
|
Attendance = request.env['community.school.attendance'].sudo()
|
|
Enrollment = request.env['community.school.enrollment'].sudo()
|
|
saved = 0
|
|
for line in (lines or []):
|
|
enrollment = Enrollment.browse(int(line.get('enrollment_id', 0)))
|
|
if not enrollment.exists() or enrollment.class_id.id != klass.id:
|
|
continue
|
|
vals = {'state': line.get('state', 'present'), 'notes': line.get('notes') or False}
|
|
record = Attendance.search([
|
|
('enrollment_id', '=', enrollment.id), ('date', '=', date),
|
|
], limit=1)
|
|
if record:
|
|
record.write(vals)
|
|
else:
|
|
vals.update({'enrollment_id': enrollment.id, 'date': date})
|
|
record = Attendance.create(vals)
|
|
saved += 1
|
|
if record.state == 'absent':
|
|
record._send_absence_notice()
|
|
|
|
return {'status': 'ok', 'saved': saved}
|