2019-01-14 03:27:30 +01:00
|
|
|
# -*- 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.')
|
|
|
|
)]
|
|
|
|
|
2019-01-21 09:42:06 +01:00
|
|
|
name = fields.Char(required=True)
|
|
|
|
lower_bound = fields.Integer(required=True)
|
|
|
|
higher_bound = fields.Integer(required=True)
|
2019-01-14 03:27:30 +01:00
|
|
|
|
|
|
|
@api.constrains('lower_bound', 'higher_bound')
|
|
|
|
def check_bounds(self):
|
2019-01-21 09:42:06 +01:00
|
|
|
""" Check member age bounds coherence and conflicts """
|
2019-01-14 03:27:30 +01:00
|
|
|
for rng in self:
|
|
|
|
if rng.lower_bound > rng.higher_bound:
|
2019-01-21 09:42:06 +01:00
|
|
|
verr = _(u'The higher bound age must be higher than the lower '
|
|
|
|
'bound.')
|
2019-01-14 03:27:30 +01:00
|
|
|
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:
|
2019-01-21 09:42:06 +01:00
|
|
|
verr = _(u'Lower bound age in range of an existing age '
|
|
|
|
'range.')
|
2019-01-14 03:27:30 +01:00
|
|
|
raise ValidationError(verr)
|
|
|
|
if each_rng.lower_bound < rng.higher_bound < each_rng.higher_bound:
|
2019-01-21 09:42:06 +01:00
|
|
|
verr = _(u'Higher bound age in range of an existing age '
|
|
|
|
'range.')
|
2019-01-14 03:27:30 +01:00
|
|
|
raise ValidationError(verr)
|
2019-01-21 09:42:06 +01:00
|
|
|
|
|
|
|
@api.onchange('lower_bound', 'higher_bound')
|
|
|
|
def onchange_name(self):
|
|
|
|
""" If no name, computes age range name """
|
2019-01-14 03:27:30 +01:00
|
|
|
for rec in self:
|
2019-01-21 09:42:06 +01:00
|
|
|
if not rec.name and rec.lower_bound and rec.higher_bound:
|
|
|
|
rec.name = _(u'{}-{} years').format(
|
|
|
|
rec.lower_bound, rec.higher_bound
|
|
|
|
)
|