diff --git a/golem_member/models/golem_member_age_range.py b/golem_member/models/golem_member_age_range.py new file mode 100644 index 0000000..39ebcab --- /dev/null +++ b/golem_member/models/golem_member_age_range.py @@ -0,0 +1,61 @@ +# -*- coding: utf-8 -*- + +# Copyright 2019 Fabien Bourgeois +# Copyright 2019 Youssef El Ouahby +# +# 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 . + +""" 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(compute='_compute_name', store=True) + lower_bound = fields.Integer(required=True, index=True) + higher_bound = fields.Integer(required=True, index=True) + + @api.constrains('lower_bound', 'higher_bound') + def check_bounds(self): + """ Check member age bounds coherence and conflits """ + 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.depends('lower_bound', 'higher_bound') + def _compute_name(self): + """ Computes age range name """ + for rec in self: + rec.name = u'{}-{} years'.format(rec.lower_bound, rec.higher_bound)