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.

107 lines
3.6 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.one
  34. @api.depends("firstname", "lastname")
  35. def _compute_name(self):
  36. """Write the 'name' field according to splitted data."""
  37. self.name = u" ".join((p for p in (self.lastname,
  38. self.firstname) if p))
  39. @api.one
  40. def _inverse_name_after_cleaning_whitespace(self):
  41. """Clean whitespace in ``name`` and call :meth:`._inverse_name`."""
  42. # Remove unneeded whitespace
  43. clean = u" ".join(self.name.split(None))
  44. # Clean name avoiding infinite recursion
  45. if self.name != clean:
  46. self.name = clean
  47. # Save name in the real fields
  48. else:
  49. self._inverse_name()
  50. @api.one
  51. def _inverse_name(self):
  52. """Try to revert the effect of :meth:`._compute_name`.
  53. - If the partner is a company, save it in the lastname.
  54. - Otherwise, make a guess.
  55. This method can be easily overriden by other submodules.
  56. When this method is called, ``self.name`` already has unified and
  57. trimmed whitespace.
  58. """
  59. # Company name goes to the lastname
  60. if self.is_company:
  61. parts = [self.name, False]
  62. # Guess name splitting
  63. else:
  64. parts = self.name.split(" ", 1)
  65. while len(parts) < 2:
  66. parts.append(False)
  67. self.lastname, self.firstname = parts
  68. @api.one
  69. @api.constrains("firstname", "lastname")
  70. def _check_name(self):
  71. """Ensure at least one name is set."""
  72. if not (self.firstname or self.lastname):
  73. raise exceptions.EmptyNamesError(self)
  74. @api.model
  75. def _install_partner_firstname(self):
  76. """Save names correctly in the database.
  77. Before installing the module, field ``name`` contains all full names.
  78. When installing it, this method parses those names and saves them
  79. correctly into the database. This can be called later too if needed.
  80. """
  81. # Find records with empty firstname and lastname
  82. records = self.search([("firstname", "=", False),
  83. ("lastname", "=", False)])
  84. # Force calculations there
  85. records._inverse_name()
  86. _logger.info("%d partners updated installing module.", len(records))