from odoo import models, fields, api from odoo.exceptions import UserError class SchoolSubject(models.Model): _name = 'school.subject' _description = 'Subject' name = fields.Selection([ ('math', 'Math'), ('english', 'English'), ('science', 'Science'), ('physical_science', 'Physical Science'), ('life_science', 'Life Science'), ('geography', 'Geography'), ('history', 'History'), ('political_science', 'Political Science'), ('economic_science', 'Economic Science'), ('physics', 'Physics'), ('chemistry', 'Chemistry'), ('biology', 'Biology'), ('it', 'IT'), ('computer_science', 'Computer Science'), ('mil', 'MIL'), ('odia', 'Odia'), ('english_grammar', 'English Grammar'), ('drawing', 'Drawing'), ('odia_grammar', 'Odia Grammar'), ], string="Subject", required=True) class_name = fields.Selection([ ('Class 1', 'Class 1'), ('Class 2', 'Class 2'), ('Class 3', 'Class 3'), ('Class 4', 'Class 4'), ('Class 5', 'Class 5'), ('Class 6', 'Class 6'), ('Class 7', 'Class 7'), ('Class 8', 'Class 8'), ('Class 9', 'Class 9'), ('Class 10', 'Class 10'), ('Class 11', 'Class 11'), ('Class 12', 'Class 12'), ], string="Class Name", required=True) lesson_plan = fields.Text(string='Lesson Plan') teacher = fields.Many2one('res.users', string='Teacher') lesson_plan_line_ids = fields.One2many('school.subject.lesson', 'subject_id', string='Lesson Plans') teacher_info_line_ids = fields.One2many('hr.employee', 'subject_id', string="Teacher Info") @api.model def get_subjects_by_class(self): return { 'Class 1': ['math', 'english', 'science', 'mil'], 'Class 2': ['math', 'english', 'science', 'drawing', 'mil'], 'Class 3': ['math', 'english', 'science', 'odia', 'drawing', 'english_grammar'], 'Class 4': ['math', 'english', 'science', 'odia', 'drawing', 'english_grammar'], 'Class 5': ['math', 'english', 'science', 'odia', 'history', 'drawing', 'english_grammar'], 'Class 6': ['math', 'english', 'science', 'geography', 'mil', 'english_grammar'], 'Class 7': ['math', 'english', 'science', 'political_science', 'mil', 'english_grammar', 'history', 'geography'], 'Class 8': ['math', 'english', 'science', 'political_science', 'mil', 'english_grammar', 'history', 'geography'], 'Class 9': ['math', 'english', 'physical_science', 'life_science', 'english_grammar', 'economic_science', 'history', 'geography'], 'Class 10': ['math', 'english', 'physical_science', 'life_science', 'english_grammar', 'economic_science', 'history', 'geography'], 'Class 11': ['physics', 'chemistry', 'math', 'biology', 'english', 'english_grammar', 'computer_science', 'mil', 'economic_science'], 'Class 12': ['physics', 'chemistry', 'math', 'biology', 'computer_science', 'english_grammar', 'english', 'mil', 'economic_science'], } def action_generate_subjects(self): if not self.class_name: raise UserError("Please select a class name first.") # Get existing subjects for this class existing_subjects = self.search([ ('class_name', '=', self.class_name) ]).mapped('name') # Get list of subjects by key (not label) subject_list = self.get_subjects_by_class().get(self.class_name, []) # Filter out already existing ones new_subjects = [sub for sub in subject_list if sub not in existing_subjects] # Create new records for subject in new_subjects: self.create({ 'class_name': self.class_name, 'name': subject, }) if not new_subjects: raise UserError("All subjects for this class already exist.")