shcool/yuthon_school/models/progress.py
2026-07-28 18:50:48 +08:00

85 lines
2.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from odoo import fields, models, api
from odoo.exceptions import ValidationError
class StudyProgress(models.Model):
_name = 'yuthon.school.progress'
_description = '学习进度'
_order = 'student_id, textbook_id, chapter_id, sequence'
_inherit = ['mail.thread', 'mail.activity.mixin']
student_id = fields.Many2one(
'yuthon.school.student',
string='学生',
required=True,
ondelete='cascade',
)
textbook_id = fields.Many2one(
'yuthon.school.textbook',
string='教材',
ondelete='restrict',
)
chapter_id = fields.Many2one(
'yuthon.school.chapter',
string='章节',
ondelete='restrict',
)
video_id = fields.Many2one(
'yuthon.school.video',
string='视频',
ondelete='restrict',
)
sequence = fields.Integer(
string='排序',
default=10,
)
progress = fields.Float(
string='进度(%)',
default=0.0,
help='0-100表示该视频的学习完成百分比',
)
completed = fields.Boolean(
string='已完成',
default=False,
)
state = fields.Selection(
[('not_started', '未开始'), ('in_progress', '进行中'), ('completed', '已完成')],
string='学习状态',
compute='_compute_state',
store=True,
)
last_study_date = fields.Datetime(
string='最后学习时间',
)
teacher_id = fields.Many2one(
'yuthon.school.teacher',
string='跟踪教师',
related='student_id.class_id.teacher_id',
store=True,
help='该学生所在班级的班主任,用于教师查看进度',
)
active = fields.Boolean(
string='启用',
default=True,
)
@api.depends('progress', 'completed')
def _compute_state(self):
"""根据进度和完成标记自动计算学习状态"""
for record in self:
if record.completed or record.progress >= 100:
record.state = 'completed'
elif record.progress > 0:
record.state = 'in_progress'
else:
record.state = 'not_started'
@api.constrains('progress')
def _check_progress_range(self):
"""验证进度值必须在 0-100 之间"""
for record in self:
if record.progress < 0 or record.progress > 100:
raise ValidationError(
f"进度值 {record.progress} 无效,必须在 0 到 100 之间"
)