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.

34 lines
1.3 KiB

5 years ago
5 years ago
5 years ago
5 years ago
  1. from datetime import datetime
  2. from openerp.tools import DEFAULT_SERVER_DATE_FORMAT as OE_DFORMAT
  3. from openerp import models, fields, api
  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. query = """SELECT id
  10. FROM "%s"
  11. WHERE extract(year from age(CURRENT_DATE,
  12. birthdate_date)) %s %%s""" % \
  13. (self._table, operator)
  14. self.env.cr.execute(query, (value,))
  15. ids = [t[0] for t in self.env.cr.fetchall()]
  16. return [('id', 'in', ids)]
  17. @api.one
  18. @api.depends('birthdate_date')
  19. def _compute_age(self):
  20. if self.birthdate_date:
  21. dBday = datetime.strptime(str(self.birthdate_date),
  22. OE_DFORMAT).date()
  23. dToday = datetime.now().date()
  24. self.age = dToday.year - dBday.year - ((
  25. dToday.month, dToday.day) < (dBday.month, dBday.day))
  26. age = fields.Integer(string='Age',
  27. compute='_compute_age',
  28. search='_search_age')