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.

44 lines
1.4 KiB

  1. from datetime import datetime
  2. from openerp import api, fields, models
  3. from openerp.tools import DEFAULT_SERVER_DATE_FORMAT as OE_DFORMAT
  4. class ResPartner(models.Model):
  5. _inherit = "res.partner"
  6. def _search_age(self, operator, value):
  7. if operator not in ("=", "!=", "<", "<=", ">", ">=", "in", "not in"):
  8. return []
  9. # pylint: disable=sql-injection
  10. # fixme while you're here, please fix the query to pass
  11. # pylint sql-injection
  12. query = """SELECT id
  13. FROM "%s"
  14. WHERE extract(year from age(CURRENT_DATE,
  15. birthdate_date)) %s %%s""" % (
  16. self._table,
  17. operator,
  18. )
  19. self.env.cr.execute(query, (value,))
  20. ids = [t[0] for t in self.env.cr.fetchall()]
  21. return [("id", "in", ids)]
  22. @api.multi
  23. @api.depends("birthdate_date")
  24. def _compute_age(self):
  25. self.ensure_one()
  26. if self.birthdate_date:
  27. dBday = datetime.strptime(
  28. str(self.birthdate_date), OE_DFORMAT
  29. ).date()
  30. dToday = datetime.now().date()
  31. self.age = (
  32. dToday.year
  33. - dBday.year
  34. - ((dToday.month, dToday.day) < (dBday.month, dBday.day))
  35. )
  36. age = fields.Integer(
  37. string="Age", compute="_compute_age", search="_search_age"
  38. )