53 lines
1.8 KiB
Python
53 lines
1.8 KiB
Python
# pyrefly: ignore [missing-import]
|
|
from odoo import api, fields, models
|
|
# pyrefly: ignore [missing-import]
|
|
from odoo.exceptions import ValidationError
|
|
|
|
|
|
class EducationCampus(models.Model):
|
|
_name = 'education.campus'
|
|
_description = 'Campus'
|
|
_order = 'name'
|
|
|
|
name = fields.Char(required=True, string='Campus Name')
|
|
code = fields.Char(string='Campus Code', required=True, index=True)
|
|
logo = fields.Image(string='Campus Logo', max_width=1920, max_height=1920)
|
|
phone = fields.Char(string='Phone')
|
|
email = fields.Char(string='Email')
|
|
active = fields.Boolean(default=True)
|
|
company_id = fields.Many2one(
|
|
'res.company',
|
|
string='Company',
|
|
required=True,
|
|
default=lambda self: self.env.company,
|
|
index=True,
|
|
)
|
|
address_id = fields.Many2one('res.partner', string='Address')
|
|
department_ids = fields.One2many('education.department', 'campus_id', string='Departments')
|
|
|
|
_sql_constraints = [
|
|
('uniq_campus_code_company', 'unique(code, company_id)', 'Campus code must be unique per company.'),
|
|
]
|
|
|
|
@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),
|
|
('id', '!=', rec.id),
|
|
])
|
|
if similar:
|
|
raise ValidationError('Campus name must be unique within a company.')
|
|
|
|
@api.model
|
|
def create(self, vals):
|
|
if vals.get('code'):
|
|
vals['code'] = vals['code'].strip().upper()
|
|
return super().create(vals)
|
|
|
|
def write(self, vals):
|
|
if 'code' in vals and vals.get('code'):
|
|
vals['code'] = vals['code'].strip().upper()
|
|
return super().write(vals)
|