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.

178 lines
6.3 KiB

  1. # -*- coding: utf-8 -*-
  2. # Author: Nicolas Bessi. Copyright Camptocamp SA
  3. # Copyright (C)
  4. # 2014: Agile Business Group (<http://www.agilebg.com>)
  5. # 2015: Grupo ESOC <www.grupoesoc.es>
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. import logging
  20. from openerp import api, fields, models
  21. from . import exceptions
  22. _logger = logging.getLogger(__name__)
  23. class ResPartner(models.Model):
  24. """Adds last name and first name; name becomes a stored function field."""
  25. _inherit = 'res.partner'
  26. firstname = fields.Char("First name")
  27. lastname = fields.Char("Last name")
  28. name = fields.Char(
  29. compute="_compute_name",
  30. inverse="_inverse_name_after_cleaning_whitespace",
  31. required=False,
  32. store=True)
  33. @api.model
  34. def create(self, vals):
  35. """Add inverted names at creation if unavailable."""
  36. if "name" in vals:
  37. inverted = self._get_inverse_name(
  38. vals.get("name"),
  39. vals.get("is_company",
  40. self.default_get(["is_company"])["is_company"]))
  41. for key, value in inverted.iteritems():
  42. if not vals.get(key):
  43. vals[key] = value
  44. return super(ResPartner, self).create(vals)
  45. @api.model
  46. def default_get(self, fields_list):
  47. """Invert name when getting default values."""
  48. result = super(ResPartner, self).default_get(fields_list)
  49. inverted = self._get_inverse_name(
  50. result.get("name", ""),
  51. result.get("is_company", False))
  52. for field in inverted.keys():
  53. if field in fields_list:
  54. result[field] = inverted.get(field)
  55. return result
  56. @api.model
  57. def _get_computed_name(self, lastname, firstname):
  58. """Compute the 'name' field according to splitted data.
  59. You can override this method to change the order of lastname and
  60. firstname the computed name"""
  61. return u" ".join((p for p in (lastname, firstname) if p))
  62. @api.one
  63. @api.depends("firstname", "lastname")
  64. def _compute_name(self):
  65. """Write the 'name' field according to splitted data."""
  66. self.name = self._get_computed_name(self.lastname, self.firstname)
  67. @api.one
  68. def _inverse_name_after_cleaning_whitespace(self):
  69. """Clean whitespace in :attr:`~.name` and split it.
  70. Removes leading, trailing and duplicated whitespace.
  71. The splitting logic is stored separately in :meth:`~._inverse_name`, so
  72. submodules can extend that method and get whitespace cleaning for free.
  73. """
  74. # Remove unneeded whitespace
  75. clean = u" ".join(self.name.split(None)) if self.name else self.name
  76. # Clean name avoiding infinite recursion
  77. if self.name != clean:
  78. self.name = clean
  79. # Save name in the real fields
  80. else:
  81. self._inverse_name()
  82. @api.model
  83. def _get_inverse_name(self, name, is_company=False):
  84. """Compute the inverted name.
  85. - If the partner is a company, save it in the lastname.
  86. - Otherwise, make a guess.
  87. This method can be easily overriden by other submodules.
  88. You can also override this method to change the order of name's
  89. attributes
  90. When this method is called, :attr:`~.name` already has unified and
  91. trimmed whitespace.
  92. """
  93. # Company name goes to the lastname
  94. if is_company or not name:
  95. parts = [name or False, False]
  96. # Guess name splitting
  97. else:
  98. parts = name.split(" ", 1)
  99. while len(parts) < 2:
  100. parts.append(False)
  101. return {"lastname": parts[0], "firstname": parts[1]}
  102. @api.one
  103. def _inverse_name(self):
  104. """Try to revert the effect of :meth:`._compute_name`."""
  105. parts = self._get_inverse_name(self.name, self.is_company)
  106. self.lastname, self.firstname = parts["lastname"], parts["firstname"]
  107. @api.one
  108. @api.constrains("firstname", "lastname")
  109. def _check_name(self):
  110. """Ensure at least one name is set."""
  111. if not (self.firstname or self.lastname):
  112. raise exceptions.EmptyNamesError(self)
  113. @api.one
  114. @api.onchange("firstname", "lastname")
  115. def _onchange_subnames(self):
  116. """Avoid recursion when the user changes one of these fields.
  117. This forces to skip the :attr:`~.name` inversion when the user is
  118. setting it in a not-inverted way.
  119. """
  120. # Modify self's context without creating a new Environment.
  121. # See https://github.com/odoo/odoo/issues/7472#issuecomment-119503916.
  122. self.env.context = self.with_context(skip_onchange=True).env.context
  123. @api.one
  124. @api.onchange("name")
  125. def _onchange_name(self):
  126. """Ensure :attr:`~.name` is inverted in the UI."""
  127. if self.env.context.get("skip_onchange"):
  128. # Do not skip next onchange
  129. self.env.context = (
  130. self.with_context(skip_onchange=False).env.context)
  131. else:
  132. self._inverse_name_after_cleaning_whitespace()
  133. @api.model
  134. def _install_partner_firstname(self):
  135. """Save names correctly in the database.
  136. Before installing the module, field ``name`` contains all full names.
  137. When installing it, this method parses those names and saves them
  138. correctly into the database. This can be called later too if needed.
  139. """
  140. # Find records with empty firstname and lastname
  141. records = self.search([("firstname", "=", False),
  142. ("lastname", "=", False)])
  143. # Force calculations there
  144. records._inverse_name()
  145. _logger.info("%d partners updated installing module.", len(records))