Browse Source

Merge pull request #159 from acsone/8.0-imp-refactor-partner-firstname-ape

[REF][partner_firstname] refactoring allowing to reuse name/firstname concatenation algorithm in other models
pull/164/head
Stéphane Bidoul (ACSONE) 10 years ago
parent
commit
4325c4331f
  1. 5
      partner_firstname/README.rst
  2. 27
      partner_firstname/models.py

5
partner_firstname/README.rst

@ -24,6 +24,10 @@ For the same reason, after installing, previous names for contacts will stay in
the *name* field, and the first time you edit any of them you will be asked to
supply the *last name* and *first name* (just once per contact).
You can use *_get_inverse_name* method to get lastname and firstname from a simple string
and also *_get_computed_name* to get a name form the lastname and firstname.
These methods can be overridden to change the format specified above
For further information, please visit:
* https://www.odoo.com/forum/help-1
@ -39,6 +43,7 @@ Contributors
* Olivier Laurent <olivier.laurent@acsone.eu>
* Hans Henrik Gabelgaard <hhg@gabelgaard.org>
* Jairo Llopis <j.llopis@grupoesoc.es>
* Adrien Peiffer <adrien.peiffer@acsone.eu>
Maintainer
----------

27
partner_firstname/models.py

@ -38,12 +38,18 @@ class ResPartner(models.Model):
required=False,
store=True)
@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"""
return u" ".join((p for p in (lastname, firstname) if p))
@api.one
@api.depends("firstname", "lastname")
def _compute_name(self):
"""Write the 'name' field according to splitted data."""
self.name = u" ".join((p for p in (self.lastname,
self.firstname) if p))
self.name = self._get_computed_name(self.lastname, self.firstname)
@api.one
def _inverse_name_after_cleaning_whitespace(self):
@ -65,28 +71,33 @@ class ResPartner(models.Model):
else:
self._inverse_name()
@api.one
def _inverse_name(self):
@api.model
def _get_inverse_name(self, name, is_company=False):
"""Try to revert the effect of :meth:`._compute_name`.
- If the partner is a company, save it in the lastname.
- Otherwise, make a guess.
This method can be easily overriden by other submodules.
You can also override this method to change the order of name's
attributes
When this method is called, :attr:`~.name` already has unified and
trimmed whitespace.
"""
# Company name goes to the lastname
if self.is_company or not self.name:
parts = [self.name or False, False]
if is_company or not name:
parts = [name or False, False]
# Guess name splitting
else:
parts = self.name.split(" ", 1)
parts = name.split(" ", 1)
while len(parts) < 2:
parts.append(False)
return parts
@api.one
def _inverse_name(self):
parts = self._get_inverse_name(self.name, self.is_company)
self.lastname, self.firstname = parts
@api.one

Loading…
Cancel
Save