shcool/school/models/yuthon_school_course.py
2026-08-03 12:03:31 +08:00

112 lines
3.1 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, api
class Course(models.Model):
_name = 'yuthon.school.course'
_description = '课程'
_order = 'sequence, name'
name = fields.Char(
string='课程名称',
required=True,
help='Odoo 18 全栈开发工程师认证课程',
)
code = fields.Char(
string='课程编号',
help='ODOO-101',
)
sequence = fields.Integer(
string='排序',
default=10,
)
category_id = fields.Many2one(
'yuthon.school.category',
string='学习分类',
ondelete='restrict',
)
teacher_id = fields.Many2one(
'yuthon.school.teacher',
string='授课教师',
)
difficulty = fields.Selection(
[('beginner', '入门'), ('intermediate', '进阶'), ('advanced', '高级')],
string='难度等级',
default='beginner',
)
state = fields.Selection(
[('draft', '草稿'), ('recruiting', '招生中'),
('ongoing', '进行中'), ('finished', '已结课')],
string='状态',
default='draft',
)
description = fields.Text(
string='课程简介',
help='课程目标、适合人群、考核方式等',
)
textbook_ids = fields.Many2many(
'yuthon.school.textbook',
'yuthon_course_textbook_rel',
'course_id', 'textbook_id',
string='关联教材',
help='本课程涵盖的教材Many2many 演示)',
)
syllabus_ids = fields.One2many(
'yuthon.school.syllabus',
'course_id',
string='课程大纲',
)
student_ids = fields.Many2many(
'yuthon.school.student',
'yuthon_course_student_rel',
'course_id', 'student_id',
string='选课学生',
help='选修本课程的学生Many2many 演示)',
)
total_hours = fields.Float(
string='总学时',
compute='_compute_total_hours',
store=True,
help='自动汇总关联教材各章学时',
)
enrollment_count = fields.Integer(
string='选课人数',
compute='_compute_counts',
store=True,
)
syllabus_count = fields.Integer(
string='大纲条目',
compute='_compute_counts',
store=True,
)
active = fields.Boolean(
string='启用',
default=True,
)
@api.depends('textbook_ids.chapter_ids.hours')
def _compute_total_hours(self):
for record in self:
record.total_hours = sum(
chapter.hours
for textbook in record.textbook_ids
for chapter in textbook.chapter_ids
)
@api.depends('student_ids', 'syllabus_ids')
def _compute_counts(self):
for record in self:
record.enrollment_count = len(record.student_ids)
record.syllabus_count = len(record.syllabus_ids)
def action_recruit(self):
"""开始招生"""
self.write({'state': 'recruiting'})
def action_start(self):
"""开课"""
self.write({'state': 'ongoing'})
def action_finish(self):
"""结课"""
self.write({'state': 'finished'})