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.

124 lines
4.9 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # This module copyright (C) 2013-2014 Therp BV (<http://therp.nl>).
  6. #
  7. # @autors: 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.osv import orm, fields
  25. class ResPartner(orm.Model):
  26. _inherit = 'res.partner'
  27. def get_street(self, cr, uid, partner, context=None):
  28. """
  29. Allow to override the field function's value composition
  30. :param partner: res.partner browse record
  31. :rtype: string
  32. """
  33. return ' '.join(filter(None, [
  34. partner.street_name,
  35. partner.street_number,
  36. ]))
  37. def _get_street(self, cr, uid, select, field_name, args, context=None):
  38. """ Delegates the function field 'street' to an inheritable method"""
  39. res = {}
  40. for partner in self.browse(cr, uid, select, context=context):
  41. res[partner.id] = self.get_street(
  42. cr, uid, partner, context=context)
  43. return res
  44. def _write_street(self, cr, uid, ids, name, value, arg, context=None):
  45. """
  46. Simplistically try to parse in case a value should get written
  47. to the 'street' field (for instance at import time, which provides
  48. us with a way of easily restoring the data when this module is
  49. installed on a database that already contains addresses).
  50. """
  51. street_name = value and value.strip() or False
  52. street_number = False
  53. if value:
  54. match = re.search('(.+)\s+(\d.*)', value.strip())
  55. if match and len(match.group(2)) < 6:
  56. street_name = match.group(1)
  57. street_number = match.group(2)
  58. return self.write(cr, uid, ids, {
  59. 'street_name': street_name,
  60. 'street_number': street_number,
  61. }, context=context)
  62. def _display_address(
  63. self, cr, uid, address, without_company=False, context=None):
  64. """
  65. Inject a context key to prevent the 'street' name to be
  66. deleted from the result of _address_fields when called from
  67. the super.
  68. """
  69. local_context = dict(context or {}, display_address=True)
  70. return super(ResPartner, self)._display_address(
  71. cr, uid, address, without_company=without_company,
  72. context=local_context)
  73. def _address_fields(self, cr, uid, context=None):
  74. """
  75. Pass on the fields for address synchronisation to contacts.
  76. This method is used on at least two occassions:
  77. [1] when address fields are synced to contacts, and
  78. [2] when addresses are formatted
  79. We want to prevent the 'street' field to be passed in the
  80. first case, as it has a fallback write method which should
  81. not be triggered in this case, while leaving the field in
  82. in the second case. Therefore, we remove the field
  83. name from the list of address fields unless we find the context
  84. key that this module injects when formatting an address.
  85. Could have checked for the occurrence of the synchronisation
  86. method instead, leaving the field in by default but that could
  87. lead to silent data corruption should the synchronisation API
  88. ever change.
  89. """
  90. res = super(ResPartner, self)._address_fields(cr, uid, context=context)
  91. if 'street' in res and not (
  92. context and context.get('display_address')):
  93. res.remove('street')
  94. return res + ['street_name', 'street_number']
  95. _columns = {
  96. 'street_name': fields.char(
  97. 'Street name', size=118),
  98. 'street_number': fields.char(
  99. 'Street number', size=10),
  100. 'street': fields.function(
  101. _get_street, fnct_inv=_write_street,
  102. type='char', string="Street",
  103. # Must be stored as per https://bugs.launchpad.net/bugs/1253200
  104. store={
  105. 'res.partner': (
  106. lambda self, cr, uid, ids, context=None: ids,
  107. ['street_name', 'street_number'], 10),
  108. },
  109. ),
  110. }