odoo_18_Education_management/models/school_subject.py

92 lines
3.9 KiB
Python
Raw Permalink Normal View History

2025-08-04 07:27:45 +00:00
from odoo import models, fields, api
2025-07-29 05:25:05 +00:00
from odoo.exceptions import UserError
class SchoolSubject(models.Model):
_name = 'school.subject'
_description = 'Subject'
2025-08-04 07:27:45 +00:00
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)
2025-07-29 05:25:05 +00:00
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')
2025-08-06 09:23:00 +00:00
teacher_info_line_ids = fields.One2many('hr.employee', 'subject_id', string="Teacher Info")
2025-07-29 05:25:05 +00:00
2025-08-06 09:23:00 +00:00
@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'],
2025-08-04 07:27:45 +00:00
}
2025-08-06 09:23:00 +00:00
2025-08-04 07:27:45 +00:00
def action_generate_subjects(self):
if not self.class_name:
raise UserError("Please select a class name first.")
2025-08-06 09:23:00 +00:00
# Get existing subjects for this class
2025-08-04 07:27:45 +00:00
existing_subjects = self.search([
('class_name', '=', self.class_name)
]).mapped('name')
2025-08-06 09:23:00 +00:00
# Get list of subjects by key (not label)
2025-08-04 07:27:45 +00:00
subject_list = self.get_subjects_by_class().get(self.class_name, [])
2025-08-06 09:23:00 +00:00
# Filter out already existing ones
2025-08-04 07:27:45 +00:00
new_subjects = [sub for sub in subject_list if sub not in existing_subjects]
2025-07-29 05:25:05 +00:00
2025-08-06 09:23:00 +00:00
# Create new records
2025-08-04 07:27:45 +00:00
for subject in new_subjects:
self.create({
'class_name': self.class_name,
'name': subject,
})
2025-07-29 05:25:05 +00:00
2025-08-04 07:27:45 +00:00
if not new_subjects:
raise UserError("All subjects for this class already exist.")