From 51c4c6088bf1d987771ff32888eed3b1f2dd4613 Mon Sep 17 00:00:00 2001 From: Antonio Espinosa Date: Thu, 21 Jul 2016 14:41:58 +0200 Subject: [PATCH 1/5] [FIX] Avoid writes if no changes in name, firstname, lastname ... --- partner_firstname/models/res_partner.py | 12 +++++++++++- partner_second_lastname/models/res_partner.py | 11 ++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/partner_firstname/models/res_partner.py b/partner_firstname/models/res_partner.py index 912b7d95f..2a04a4c75 100644 --- a/partner_firstname/models/res_partner.py +++ b/partner_firstname/models/res_partner.py @@ -189,7 +189,10 @@ class ResPartner(models.Model): def _inverse_name(self): """Try to revert the effect of :meth:`._compute_name`.""" parts = self._get_inverse_name(self.name, self.is_company) - self.lastname, self.firstname = parts["lastname"], parts["firstname"] + if parts["lastname"] != self.lastname: + self.lastname = parts["lastname"] + if parts["firstname"] != self.firstname: + self.firstname = parts["firstname"] @api.one @api.constrains("firstname", "lastname") @@ -236,3 +239,10 @@ class ResPartner(models.Model): # Force calculations there records._inverse_name() _logger.info("%d partners updated installing module.", len(records)) + + @api.multi + def write(self, vals): + if vals.get('name') == self.name: + vals.pop('name', None) + if vals: + return super(ResPartner, self).write(vals) diff --git a/partner_second_lastname/models/res_partner.py b/partner_second_lastname/models/res_partner.py index 0628dcdb9..8d4640dfa 100644 --- a/partner_second_lastname/models/res_partner.py +++ b/partner_second_lastname/models/res_partner.py @@ -45,9 +45,8 @@ class ResPartner(models.Model): @api.depends("firstname", "lastname", "lastname2") def _compute_name(self): """Write :attr:`~.name` according to splitted data.""" - self.name = self._get_computed_name(self.lastname, - self.firstname, - self.lastname2) + self.name = self._get_computed_name( + self.lastname, self.firstname, self.lastname2) @api.one def _inverse_name(self): @@ -58,8 +57,10 @@ class ResPartner(models.Model): before, after = dict(), dict() for key, value in parts.iteritems(): (before if value else after)[key] = value - self.update(before) - self.update(after) + if any([before[k] != self[k] for k in before.keys()]): + self.update(before) + if any([after[k] != self[k] for k in after.keys()]): + self.update(after) @api.model def _get_inverse_name(self, name, is_company=False): From fee7de9a5ffc7651b252e85120031dd8bbaea10a Mon Sep 17 00:00:00 2001 From: Antonio Espinosa Date: Thu, 21 Jul 2016 16:30:30 +0200 Subject: [PATCH 2/5] Fix Travis error --- partner_firstname/models/res_partner.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/partner_firstname/models/res_partner.py b/partner_firstname/models/res_partner.py index 2a04a4c75..69c77098b 100644 --- a/partner_firstname/models/res_partner.py +++ b/partner_firstname/models/res_partner.py @@ -242,7 +242,8 @@ class ResPartner(models.Model): @api.multi def write(self, vals): - if vals.get('name') == self.name: + name = vals.get('name') + if name and all(name == partner.name for partner in self): vals.pop('name', None) if vals: return super(ResPartner, self).write(vals) From d2520417189718638dc29a79c15bacf566796e8e Mon Sep 17 00:00:00 2001 From: Antonio Espinosa Date: Thu, 21 Jul 2016 17:25:07 +0200 Subject: [PATCH 3/5] Fix Travis error on partner_changeset --- partner_firstname/models/res_partner.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/partner_firstname/models/res_partner.py b/partner_firstname/models/res_partner.py index 69c77098b..cf893a508 100644 --- a/partner_firstname/models/res_partner.py +++ b/partner_firstname/models/res_partner.py @@ -245,5 +245,5 @@ class ResPartner(models.Model): name = vals.get('name') if name and all(name == partner.name for partner in self): vals.pop('name', None) - if vals: - return super(ResPartner, self).write(vals) + this = self.sudo() if not vals else self + return super(ResPartner, this).write(vals) From afd4d30f4307db8e5ffd0bdce721f1a5b9ac868d Mon Sep 17 00:00:00 2001 From: Antonio Espinosa Date: Thu, 21 Jul 2016 18:06:56 +0200 Subject: [PATCH 4/5] Some changes for relaunching Travis --- partner_firstname/models/res_partner.py | 33 +++++++++---------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/partner_firstname/models/res_partner.py b/partner_firstname/models/res_partner.py index cf893a508..f59a0abe9 100644 --- a/partner_firstname/models/res_partner.py +++ b/partner_firstname/models/res_partner.py @@ -1,23 +1,9 @@ # -*- coding: utf-8 -*- - -# Author: Nicolas Bessi. Copyright Camptocamp SA -# Copyright (C) -# 2014: Agile Business Group () -# 2015: Grupo ESOC -# 2015: Antiun Ingenieria S.L. - Antonio Espinosa -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . +# Copyright 2014 Agile Business Group () +# Copyright 2015 Grupo ESOC +# Copyright 2015 Antiun Ingenieria S.L. - Antonio Espinosa +# Copyright 2016 Antonio Espinosa +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). import logging from openerp import api, fields, models @@ -29,6 +15,7 @@ _logger = logging.getLogger(__name__) class ResPartner(models.Model): """Adds last name and first name; name becomes a stored function field.""" + _inherit = 'res.partner' firstname = fields.Char("First name") @@ -96,16 +83,20 @@ class ResPartner(models.Model): @api.model def _get_names_order(self): """Get names order configuration from system parameters. + You can override this method to read configuration from language, - country, company or other""" + country, company or other + """ return self.env['ir.config_parameter'].get_param( 'partner_names_order', self._names_order_default()) @api.model def _get_computed_name(self, lastname, firstname): """Compute the 'name' field according to splitted data. + You can override this method to change the order of lastname and - firstname the computed name""" + firstname the computed name + """ order = self._get_names_order() if order == 'last_first_comma': return u", ".join((p for p in (lastname, firstname) if p)) From aa8d63fe32c803610c6e3a112fe4a5d6657c4b2a Mon Sep 17 00:00:00 2001 From: Antonio Espinosa Date: Thu, 21 Jul 2016 18:32:45 +0200 Subject: [PATCH 5/5] Add a comment to remember why we call write with sudo --- partner_firstname/models/res_partner.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/partner_firstname/models/res_partner.py b/partner_firstname/models/res_partner.py index f59a0abe9..5131b1398 100644 --- a/partner_firstname/models/res_partner.py +++ b/partner_firstname/models/res_partner.py @@ -236,5 +236,10 @@ class ResPartner(models.Model): name = vals.get('name') if name and all(name == partner.name for partner in self): vals.pop('name', None) + # If vals is empty (only write name field and with the same value) + # Avoid access checking here + # https://github.com/odoo/odoo/blob/ + # 8b83119fad7ccae9f091f12b6ac89c2c31e4bac3/openerp/addons/base/res/ + # res_partner.py#L569 this = self.sudo() if not vals else self return super(ResPartner, this).write(vals)