51 lines
1.7 KiB
Python
51 lines
1.7 KiB
Python
from odoo import api, fields, models
|
|
from odoo.exceptions import ValidationError
|
|
|
|
|
|
class EducationDepartment(models.Model):
|
|
_name = 'education.department'
|
|
_description = 'Department'
|
|
_order = 'name'
|
|
|
|
name = fields.Char(required=True, string='Department Name')
|
|
code = fields.Char(string='Department Code', required=True, index=True)
|
|
campus_id = fields.Many2one(
|
|
'education.campus',
|
|
string='Campus',
|
|
required=True,
|
|
ondelete='restrict',
|
|
index=True,
|
|
)
|
|
company_id = fields.Many2one(
|
|
'res.company',
|
|
related='campus_id.company_id',
|
|
store=True,
|
|
readonly=True,
|
|
index=True,
|
|
)
|
|
head_teacher_id = fields.Many2one('res.partner', string='Head Teacher')
|
|
active = fields.Boolean(default=True)
|
|
program_ids = fields.One2many('education.program', 'department_id', string='Programs')
|
|
|
|
_sql_constraints = [
|
|
('uniq_department_code_company', 'unique(code, company_id)', 'Department code must be unique per company.'),
|
|
]
|
|
|
|
@api.constrains('code')
|
|
def _check_code_format(self):
|
|
for rec in self:
|
|
if rec.code and not rec.code.strip():
|
|
raise ValidationError('Department code cannot be blank.')
|
|
|
|
@api.constrains('name', 'company_id')
|
|
def _check_name_unique(self):
|
|
for rec in self:
|
|
similar = self.search([
|
|
('company_id', '=', rec.company_id.id),
|
|
('name', '=ilike', rec.name),
|
|
('campus_id', '=', rec.campus_id.id),
|
|
('id', '!=', rec.id),
|
|
])
|
|
if similar:
|
|
raise ValidationError('Department name must be unique within a campus.')
|