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.

98 lines
3.9 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Odoo, an open source suite of business apps
  5. # This module copyright (C) 2013-2015 Therp BV (<http://therp.nl>).
  6. #
  7. # @authors: Stefan Rijnhart, Ronald Portier
  8. #
  9. # This program is free software: you can redistribute it and/or modify
  10. # it under the terms of the GNU Affero General Public License as
  11. # published by the Free Software Foundation, either version 3 of the
  12. # License, or (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU Affero General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU Affero General Public License
  20. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. #
  22. ##############################################################################
  23. import re
  24. from odoo import models, fields, api
  25. class ResPartner(models.Model):
  26. _inherit = 'res.partner'
  27. @api.multi
  28. @api.depends('street_name', 'street_number')
  29. def _get_street(self):
  30. for partner in self:
  31. partner.street = ' '.join(
  32. filter(None, [partner.street_name, partner.street_number]))
  33. def _write_street(self):
  34. """
  35. Simplistically try to parse in case a value should get written
  36. to the 'street' field (for instance at import time, which provides
  37. us with a way of easily restoring the data when this module is
  38. installed on a database that already contains addresses).
  39. """
  40. for partner in self:
  41. street_name = partner.street.strip() if partner.street else False
  42. street_number = False
  43. if street_name:
  44. match = re.search(r'(.+)\s+(\d.*)', street_name)
  45. if match and len(match.group(2)) < 6:
  46. street_name = match.group(1)
  47. street_number = match.group(2)
  48. partner.street_name = street_name
  49. partner.street_number = street_number
  50. @api.multi
  51. def _display_address(self, without_company=False):
  52. """
  53. Inject a context key to prevent the 'street' name to be
  54. deleted from the result of _address_fields when called from
  55. the super.
  56. """
  57. return super(ResPartner, self.with_context(display_address=True)).\
  58. _display_address(without_company=without_company)
  59. @api.model
  60. def _address_fields(self):
  61. """
  62. Pass on the fields for address synchronisation to contacts.
  63. This method is used on at least two occassions:
  64. [1] when address fields are synced to contacts, and
  65. [2] when addresses are formatted
  66. We want to prevent the 'street' field to be passed in the
  67. first case, as it has a fallback write method which should
  68. not be triggered in this case, while leaving the field in
  69. in the second case. Therefore, we remove the field
  70. name from the list of address fields unless we find the context
  71. key that this module injects when formatting an address.
  72. Could have checked for the occurrence of the synchronisation
  73. method instead, leaving the field in by default but that could
  74. lead to silent data corruption should the synchronisation API
  75. ever change.
  76. """
  77. res = super(ResPartner, self)._address_fields()
  78. if 'street' in res and not (
  79. self._context.get('display_address')):
  80. res.remove('street')
  81. return res + ['street_name', 'street_number']
  82. street_name = fields.Char('Street name')
  83. street_number = fields.Char('Street number')
  84. # Must be stored as per https://bugs.launchpad.net/bugs/1253200
  85. street = fields.Char(
  86. compute='_get_street', store=True, inverse='_write_street')