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.

32 lines
1.2 KiB

6 years ago
6 years ago
6 years ago
6 years ago
  1. # -*- coding: utf-8 -*-
  2. from datetime import datetime
  3. from openerp.tools import DEFAULT_SERVER_DATE_FORMAT as OE_DFORMAT
  4. from openerp import models, fields, api
  5. class ResPartner(models.Model):
  6. _inherit = 'res.partner'
  7. def _search_age(self, operator, value):
  8. if operator not in ('=', '!=', '<', '<=', '>', '>=', 'in', 'not in'):
  9. return []
  10. # retrieve all the messages that match with a specific SQL query
  11. query = """SELECT id FROM "%s" WHERE extract(year from age(CURRENT_DATE, birthdate_date)) %s %%s""" % \
  12. (self._table, operator)
  13. self.env.cr.execute(query, (value,))
  14. ids = [t[0] for t in self.env.cr.fetchall()]
  15. return [('id', 'in', ids)]
  16. @api.one
  17. @api.depends('birthdate_date')
  18. def _compute_age(self):
  19. if self.birthdate_date:
  20. dBday = datetime.strptime(self.birthdate_date, OE_DFORMAT).date()
  21. dToday = datetime.now().date()
  22. self.age = dToday.year - dBday.year - ((
  23. dToday.month, dToday.day) < (dBday.month, dBday.day))
  24. age = fields.Integer(string='Age',
  25. compute='_compute_age',
  26. search='_search_age')