|
@ -1,24 +1,32 @@ |
|
|
|
|
|
# Copyright 2020 Coop IT Easy SCRL fs |
|
|
|
|
|
# Robin Keunen <robin@coopiteasy.be> |
|
|
|
|
|
# Houssine Bakkali <houssine@coopiteasy.be> |
|
|
|
|
|
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). |
|
|
|
|
|
|
|
|
from datetime import datetime |
|
|
from datetime import datetime |
|
|
|
|
|
|
|
|
from openerp import api, fields, models |
|
|
|
|
|
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT as OE_DFORMAT |
|
|
|
|
|
|
|
|
from odoo import api, fields, models |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ResPartner(models.Model): |
|
|
class ResPartner(models.Model): |
|
|
_inherit = "res.partner" |
|
|
_inherit = "res.partner" |
|
|
|
|
|
|
|
|
|
|
|
age = fields.Integer( |
|
|
|
|
|
string="Age", compute="_compute_age", search="_search_age" |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
def _search_age(self, operator, value): |
|
|
def _search_age(self, operator, value): |
|
|
if operator not in ("=", "!=", "<", "<=", ">", ">=", "in", "not in"): |
|
|
if operator not in ("=", "!=", "<", "<=", ">", ">=", "in", "not in"): |
|
|
return [] |
|
|
return [] |
|
|
# pylint: disable=sql-injection |
|
|
# pylint: disable=sql-injection |
|
|
# fixme while you're here, please fix the query to pass |
|
|
|
|
|
# pylint sql-injection |
|
|
|
|
|
query = """SELECT id |
|
|
|
|
|
FROM "%s" |
|
|
|
|
|
WHERE extract(year from age(CURRENT_DATE, |
|
|
|
|
|
birthdate_date)) %s %%s""" % ( |
|
|
|
|
|
self._table, |
|
|
|
|
|
operator, |
|
|
|
|
|
|
|
|
# the value of operator is checked, no risk of injection |
|
|
|
|
|
query = """ |
|
|
|
|
|
SELECT id |
|
|
|
|
|
FROM res_partner |
|
|
|
|
|
WHERE extract(year from age(CURRENT_DATE, birthdate_date)) |
|
|
|
|
|
{operator} %s |
|
|
|
|
|
""".format( |
|
|
|
|
|
operator=operator |
|
|
) |
|
|
) |
|
|
self.env.cr.execute(query, (value,)) |
|
|
self.env.cr.execute(query, (value,)) |
|
|
ids = [t[0] for t in self.env.cr.fetchall()] |
|
|
ids = [t[0] for t in self.env.cr.fetchall()] |
|
@ -27,18 +35,15 @@ class ResPartner(models.Model): |
|
|
@api.multi |
|
|
@api.multi |
|
|
@api.depends("birthdate_date") |
|
|
@api.depends("birthdate_date") |
|
|
def _compute_age(self): |
|
|
def _compute_age(self): |
|
|
self.ensure_one() |
|
|
|
|
|
if self.birthdate_date: |
|
|
|
|
|
dBday = datetime.strptime( |
|
|
|
|
|
str(self.birthdate_date), OE_DFORMAT |
|
|
|
|
|
).date() |
|
|
|
|
|
dToday = datetime.now().date() |
|
|
|
|
|
self.age = ( |
|
|
|
|
|
dToday.year |
|
|
|
|
|
- dBday.year |
|
|
|
|
|
- ((dToday.month, dToday.day) < (dBday.month, dBday.day)) |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
age = fields.Integer( |
|
|
|
|
|
string="Age", compute="_compute_age", search="_search_age" |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
for partner in self: |
|
|
|
|
|
if partner.birthdate_date: |
|
|
|
|
|
birthday = partner.birthdate_date |
|
|
|
|
|
today = datetime.now().date() |
|
|
|
|
|
partner.age = ( |
|
|
|
|
|
today.year |
|
|
|
|
|
- birthday.year |
|
|
|
|
|
- ( |
|
|
|
|
|
(today.month, today.day) |
|
|
|
|
|
< (birthday.month, birthday.day) |
|
|
|
|
|
) |
|
|
|
|
|
) |