80 lines
3.9 KiB
Python
80 lines
3.9 KiB
Python
from odoo import api, fields, models
|
|
|
|
class CourseChapterResource(models.Model):
|
|
_name = 'course.chapter.resource'
|
|
_description = '章节资源'
|
|
_rec_name = 'name'
|
|
_order = 'chapter_id, sequence, id'
|
|
|
|
name = fields.Char(string='资源名称', required=True)
|
|
sequence = fields.Integer(string='序号', default=10)
|
|
description = fields.Text(string='备注说明')
|
|
chapter_id = fields.Many2one('course.chapter', string='所属章节', required=True)
|
|
course_id = fields.Many2one(related="chapter_id.course_id", string='所属课程', required=True, ondelete='cascade')
|
|
chapter_name = fields.Char(related="chapter_id.name", string="章节名称", readonly=True)
|
|
chapter_sequence = fields.Integer(related="chapter_id.sequence", string="章节序号", readonly=True)
|
|
chapter_is_published = fields.Boolean(related="chapter_id.is_published", string="章节发布状态", readonly=True)
|
|
resource_type = fields.Selection([
|
|
('video', '视频'),
|
|
('pdf', 'PDF文档'),
|
|
('ppt', 'PPT演示文稿'),
|
|
('other', '其他'),
|
|
], string='资源类型', required=True)
|
|
video_file = fields.Binary(string='视频文件', attachment=True)
|
|
video_filename = fields.Char(string='视频文件名')
|
|
video_url = fields.Char(string='视频链接')
|
|
video_duration = fields.Integer(string='时长(秒)')
|
|
doc_file = fields.Binary(string='文档文件', attachment=True)
|
|
doc_filename = fields.Char(string='文档文件名')
|
|
doc_page_count = fields.Integer(string='页数')
|
|
# 添加教学班关联(可选,资源可共享给所有教学班)
|
|
teaching_class_id = fields.Many2one('course.teaching_class', string='教学班',
|
|
ondelete='cascade')
|
|
preview_url = fields.Char(string='预览链接', compute='_compute_preview_url')
|
|
is_hidden = fields.Boolean(string='是否隐藏', default=False)
|
|
|
|
course_name = fields.Char(related="chapter_id.course_id.name", string="课程名称", readonly=True)
|
|
course_code = fields.Char(related="chapter_id.course_id.code", string="课程代码", readonly=True)
|
|
course_credit = fields.Float(related="chapter_id.course_id.credit", string="学分", readonly=True)
|
|
|
|
# 新增:批量上传附件字段(核心)
|
|
resource_attachment_ids = fields.Many2many(
|
|
comodel_name='ir.attachment',
|
|
relation='course_chapter_resource_attachment_rel',
|
|
column1='resource_id',
|
|
column2='attachment_id',
|
|
string='批量上传资源附件',
|
|
)
|
|
# 新增:附件数量统计(可选,用于列表显示)
|
|
attachment_count = fields.Integer(string="附件数", compute="_compute_attachment_count")
|
|
|
|
@api.depends('resource_attachment_ids')
|
|
def _compute_attachment_count(self):
|
|
for rec in self:
|
|
rec.attachment_count = len(rec.resource_attachment_ids)
|
|
|
|
def _compute_preview_url(self):
|
|
for record in self:
|
|
# 优先使用批量附件中的第一个文件作为预览链接
|
|
if record.resource_attachment_ids:
|
|
first_attachment = record.resource_attachment_ids[0]
|
|
record.preview_url = f'/web/content/{first_attachment.id}?download=false'
|
|
elif record.resource_type == 'video' and record.video_file:
|
|
record.preview_url = f'/web/content/course.chapter.resource/{record.id}/video_file'
|
|
elif record.resource_type in ['pdf', 'ppt'] and record.doc_file:
|
|
record.preview_url = f'/web/content/course.chapter.resource/{record.id}/doc_file'
|
|
else:
|
|
record.preview_url = False
|
|
|
|
def action_toggle_hide(self):
|
|
self.is_hidden = not self.is_hidden
|
|
|
|
def action_preview(self):
|
|
self.ensure_one()
|
|
if self.preview_url:
|
|
return {
|
|
'type': 'ir.actions.act_url',
|
|
'url': self.preview_url,
|
|
'target': 'new'
|
|
}
|
|
return False |