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.

97 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 openerp import models, fields, api
  25. class ResPartner(models.Model):
  26. _inherit = 'res.partner'
  27. @api.one
  28. @api.depends('street_name', 'street_number')
  29. def _get_street(self):
  30. self.street = ' '.join(
  31. filter(None, [self.street_name, self.street_number]))
  32. def _write_street(self):
  33. """
  34. Simplistically try to parse in case a value should get written
  35. to the 'street' field (for instance at import time, which provides
  36. us with a way of easily restoring the data when this module is
  37. installed on a database that already contains addresses).
  38. """
  39. street_name = self.street and self.street.strip() or False
  40. street_number = False
  41. if self.street:
  42. match = re.search(r'(.+)\s+(\d.*)', self.street.strip())
  43. if match and len(match.group(2)) < 6:
  44. street_name = match.group(1)
  45. street_number = match.group(2)
  46. for partner in self:
  47. self.street_name = street_name
  48. self.street_number = street_number
  49. @api.model
  50. def _display_address(self, address, without_company=False):
  51. """
  52. Inject a context key to prevent the 'street' name to be
  53. deleted from the result of _address_fields when called from
  54. the super.
  55. """
  56. return super(ResPartner, self.with_context(display_address=True)).\
  57. _display_address(address, without_company=without_company)
  58. @api.model
  59. def _address_fields(self):
  60. """
  61. Pass on the fields for address synchronisation to contacts.
  62. This method is used on at least two occassions:
  63. [1] when address fields are synced to contacts, and
  64. [2] when addresses are formatted
  65. We want to prevent the 'street' field to be passed in the
  66. first case, as it has a fallback write method which should
  67. not be triggered in this case, while leaving the field in
  68. in the second case. Therefore, we remove the field
  69. name from the list of address fields unless we find the context
  70. key that this module injects when formatting an address.
  71. Could have checked for the occurrence of the synchronisation
  72. method instead, leaving the field in by default but that could
  73. lead to silent data corruption should the synchronisation API
  74. ever change.
  75. """
  76. res = super(ResPartner, self)._address_fields()
  77. if 'street' in res and not (
  78. self._context.get('display_address')):
  79. res.remove('street')
  80. return res + ['street_name', 'street_number']
  81. street_name = fields.Char('Street name')
  82. street_number = fields.Char('Street number')
  83. # Must be stored as per https://bugs.launchpad.net/bugs/1253200
  84. street = fields.Char(
  85. compute='_get_street', store=True, inverse='_write_street')