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.

65 lines
2.9 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Author: Yannick Vaucher
  5. # Copyright 2013 Camptocamp SA
  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. #
  20. ##############################################################################
  21. from openerp.osv import orm, fields
  22. class ResPartner(orm.Model):
  23. _inherit = 'res.partner'
  24. def _display_name_compute(self, cr, uid, ids, name, args, context=None):
  25. return dict(self.name_get(cr, uid, ids, context=context))
  26. def name_get(self, cr, uid, ids, context=None):
  27. """ By pass of name_get to use directly firstname and lastname
  28. as we cannot ensure name as already been computed when calling this
  29. method for display_name"""
  30. if context is None:
  31. context = {}
  32. if isinstance(ids, (int, long)):
  33. ids = [ids]
  34. res = []
  35. for record in self.browse(cr, uid, ids, context=context):
  36. names = (record.lastname, record.firstname)
  37. name = u" ".join([s for s in names if s])
  38. if record.parent_id and not record.is_company:
  39. name = "%s, %s" % (record.parent_id.name, name)
  40. if context.get('show_address'):
  41. name = name + "\n" + self._display_address(cr, uid, record, without_company=True, context=context)
  42. name = name.replace('\n\n','\n')
  43. name = name.replace('\n\n','\n')
  44. if context.get('show_email') and record.email:
  45. name = "%s <%s>" % (name, record.email)
  46. res.append((record.id, name))
  47. return res
  48. _display_name_store_triggers = {
  49. 'res.partner': (lambda self,cr,uid,ids,context=None: self.search(cr, uid, [('id','child_of',ids)]),
  50. ['parent_id', 'is_company', 'name', 'firstname', 'lastname'], 10)
  51. }
  52. # indirection to avoid passing a copy of the overridable method when declaring the function field
  53. _display_name = lambda self, *args, **kwargs: self._display_name_compute(*args, **kwargs)
  54. _columns = {
  55. # extra field to allow ORDER BY to match visible names
  56. 'display_name': fields.function(_display_name, type='char', string='Name', store=_display_name_store_triggers),
  57. }