Browse Source

Merge pull request #97 from coopiteasy/12.0-partner-age-fix

[12.0][FIX] partner_age remove self.ensure_one
pull/98/head
Robin Keunen 5 years ago
committed by GitHub
parent
commit
e66a2541a1
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      easy_my_coop/__manifest__.py
  2. 6
      partner_age/__manifest__.py
  3. 51
      partner_age/models/partner.py
  4. 18
      partner_age/view/partner_view.xml

4
easy_my_coop/__manifest__.py

@ -1,6 +1,7 @@
# Copyright 2013-2018 Open Architects Consulting SPRL. # Copyright 2013-2018 Open Architects Consulting SPRL.
# Copyright 2018 Coop IT Easy SCRLfs (<http://www.coopiteasy.be>) # Copyright 2018 Coop IT Easy SCRLfs (<http://www.coopiteasy.be>)
# - Houssine BAKKALI - <houssine@coopiteasy.be>
# Houssine BAKKALI - <houssine@coopiteasy.be>
# Robin Keunen <robin@coopiteasy.be>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
{ {
@ -14,6 +15,7 @@
"account", "account",
"base_iban", "base_iban",
"product", "product",
"partner_age",
"partner_firstname", "partner_firstname",
"partner_contact_birthdate", "partner_contact_birthdate",
"partner_contact_address", "partner_contact_address",

6
partner_age/__manifest__.py

@ -4,11 +4,11 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).# # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).#
{ {
"name": "Partner Age", "name": "Partner Age",
"version": "12.0.1.0.0",
"depends": ["easy_my_coop", "partner_contact_birthdate"],
"version": "12.0.2.0.0",
"depends": ["partner_contact_birthdate"],
"author": "Coop IT Easy SCRLfs", "author": "Coop IT Easy SCRLfs",
"category": "Cooperative management", "category": "Cooperative management",
"website": "www.coopiteasy.be",
"website": "https://www.coopiteasy.be",
"license": "AGPL-3", "license": "AGPL-3",
"summary": "This module computes the age of the partner.", "summary": "This module computes the age of the partner.",
"data": ["view/partner_view.xml"], "data": ["view/partner_view.xml"],

51
partner_age/models/partner.py

@ -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))
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)
) )
age = fields.Integer(
string="Age", compute="_compute_age", search="_search_age"
) )

18
partner_age/view/partner_view.xml

@ -1,10 +1,14 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2020 Coop IT Easy
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
-->
<odoo> <odoo>
<record id="view_partner_from_age" model="ir.ui.view"> <record id="view_partner_from_age" model="ir.ui.view">
<field name="name">res.partner.form.age</field> <field name="name">res.partner.form.age</field>
<field name="model">res.partner</field> <field name="model">res.partner</field>
<field name="inherit_id" <field name="inherit_id"
ref="easy_my_coop.view_partner_form_easy_my_coop"/>
ref="partner_contact_birthdate.view_personal_information_birthdate_date"/>
<field name="arch" type="xml"> <field name="arch" type="xml">
<xpath expr="//field[@name='birthdate_date']" position='after'> <xpath expr="//field[@name='birthdate_date']" position='after'>
<field name="age" <field name="age"
@ -12,16 +16,4 @@
</xpath> </xpath>
</field> </field>
</record> </record>
<record id="view_partner_tree_age" model="ir.ui.view">
<field name="name">res.partner.tree.easy_my_coop</field>
<field name="model">res.partner</field>
<field name="inherit_id"
ref="easy_my_coop.view_partner_tree_easy_my_coop"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='cooperator_type']" position='after'>
<field name="age"/>
</xpath>
</field>
</record>
</odoo> </odoo>
Loading…
Cancel
Save