You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.6 KiB

  1. # Copyright 2019-2020: Druidoo (<https://www.druidoo.io>)
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  3. from odoo import _, api, fields, models
  4. from odoo.exceptions import ValidationError
  5. class ResPartnerDateRange(models.Model):
  6. _name = "res.partner.age.range"
  7. _description = "Partner Age Range"
  8. def _default_age_from(self):
  9. age_from = 0
  10. last_age_range = self.env["res.partner.age.range"].search(
  11. [], order="age_to desc", limit=1
  12. )
  13. if last_age_range:
  14. age_from = last_age_range.age_to + 1
  15. return age_from
  16. name = fields.Char(string="Name", required=True)
  17. age_from = fields.Integer(
  18. string="From", required=True, default=lambda self: self._default_age_from()
  19. )
  20. age_to = fields.Integer(string="To", required=True)
  21. _sql_constraints = [("name_uniq", "unique (name)", "A name must be unique !")]
  22. @api.constrains("age_from", "age_to")
  23. def _validate_range(self):
  24. for rec in self:
  25. if rec.age_from >= rec.age_to:
  26. raise ValidationError(
  27. _("%s is not a valid range (%s >= %s)")
  28. % (rec.name, rec.age_from, rec.age_to)
  29. )
  30. range_id = rec.search(
  31. [
  32. ("age_from", "<=", rec.age_to),
  33. ("age_to", ">=", rec.age_from),
  34. ("id", "!=", rec.id),
  35. ],
  36. limit=1,
  37. )
  38. if range_id:
  39. raise ValidationError(
  40. _("%s is overalapping with range %s") % (rec.name, range_id.name)
  41. )