shcool/school/wizards/progress_wizard.py
2026-08-03 12:03:31 +08:00

70 lines
2.2 KiB
Python
Raw Permalink 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
class ProgressWizard(models.TransientModel):
"""批量更新学习进度向导TransientModel 演示)
TransientModel 的特点:
1. 数据存储在临时表中,定期自动清理
2. 用于弹出式向导交互,不保留持久数据
3. 权限模型与普通 Model 不同(通常不需要 unlink 权限)
"""
_name = 'yuthon.school.progress.wizard'
_description = '批量更新学习进度'
class_id = fields.Many2one(
'yuthon.school.class',
string='班级',
required=True,
)
chapter_id = fields.Many2one(
'yuthon.school.chapter',
string='章节',
required=True,
)
textbook_id = fields.Many2one(
'yuthon.school.textbook',
string='所属教材',
related='chapter_id.textbook_id',
readonly=True,
)
progress_value = fields.Float(
string='设置进度(%)',
default=100.0,
help='将该班级所有学生此章节的进度设置为该值0-100',
)
completed = fields.Boolean(
string='标记为已完成',
default=True,
)
def action_apply(self):
"""批量更新该班级所有学生的指定章节进度"""
self.ensure_one()
Progress = self.env['yuthon.school.progress']
students = self.class_id.student_ids
now = fields.Datetime.now()
for student in students:
existing = Progress.search([
('student_id', '=', student.id),
('chapter_id', '=', self.chapter_id.id),
], limit=1)
if existing:
existing.write({
'progress': self.progress_value,
'completed': self.completed,
'last_study_date': now,
})
else:
Progress.create({
'student_id': student.id,
'chapter_id': self.chapter_id.id,
'textbook_id': self.chapter_id.textbook_id.id,
'progress': self.progress_value,
'completed': self.completed,
'last_study_date': now,
})
return {'type': 'ir.actions.act_window_close'}