GOLEM/golem_member/models/golem_member_age_range.py

66 lines
2.7 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# Copyright 2019 Fabien Bourgeois <fabien@yaltik.com>
# Copyright 2019 Youssef El Ouahby <youssef@yaltik.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
""" GOLEM Member Age Range Management """
from odoo import models, fields, api, _
from odoo.exceptions import ValidationError
class GolemMemberAgeRange(models.Model):
""" GOLEM Member Age Range Management """
_name = 'golem.member.age.range'
_description = 'GOLEM Member Age Range Management'
_order = 'lower_bound asc'
_sql_constraints = [(
'golem_age_range_rule_name_uniq', 'UNIQUE (name)',
_('This name has already been used. It must be unique.')
)]
name = fields.Char(required=True)
lower_bound = fields.Integer(required=True)
higher_bound = fields.Integer(required=True)
@api.constrains('lower_bound', 'higher_bound')
def check_bounds(self):
""" Check member age bounds coherence and conflicts """
for rng in self:
if rng.lower_bound > rng.higher_bound:
verr = _(u'The higher bound age must be higher than the lower '
'bound.')
raise ValidationError(verr)
rngs = self.env['golem.member.age.range'].search([])
for each_rng in rngs:
if each_rng.lower_bound < rng.lower_bound < each_rng.higher_bound:
verr = _(u'Lower bound age in range of an existing age '
'range.')
raise ValidationError(verr)
if each_rng.lower_bound < rng.higher_bound < each_rng.higher_bound:
verr = _(u'Higher bound age in range of an existing age '
'range.')
raise ValidationError(verr)
@api.onchange('lower_bound', 'higher_bound')
def onchange_name(self):
""" If no name, computes age range name """
for rec in self:
if not rec.name and rec.lower_bound and rec.higher_bound:
rec.name = _(u'{}-{} years').format(
rec.lower_bound, rec.higher_bound
)