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.

56 lines
2.4 KiB

  1. # -*- coding: utf-8 -*-
  2. from openerp.addons.web.controllers.main import DataSet
  3. from openerp.tools.translate import _
  4. from openerp import http
  5. from openerp.http import request
  6. class DataSetCustom(DataSet):
  7. def _extend_name(self, model, records):
  8. cr, uid, context = request.cr, request.uid, request.context
  9. Model = request.registry[model]
  10. fields = Model.fields_get(cr, uid, False, context)
  11. contact_field = False
  12. for n, f in fields.iteritems():
  13. if f['type'] == 'many2one' and f['relation'] == 'res.partner':
  14. contact_field = n
  15. break
  16. partner_info = {}
  17. if contact_field:
  18. partner_info = Model.read(cr, uid, [r[0] for r in records], [contact_field], context)
  19. partner_info = dict([(p['id'], p[contact_field]) for p in partner_info])
  20. res = []
  21. for r in records:
  22. if partner_info.get(r[0]):
  23. res.append((r[0], _('%s [%s] ID %s') % (r[1], partner_info.get(r[0])[1], r[0])))
  24. else:
  25. res.append((r[0], _('%s ID %s') % (r[1], r[0])))
  26. return res
  27. @http.route('/web/dataset/call_kw/<model>/name_search', type='json', auth="user")
  28. def name_search(self, model, method, args, kwargs):
  29. context = kwargs.get('context')
  30. if context and context.get('extended_name_with_contact'):
  31. # add order by ID desc
  32. cr, uid = request.cr, request.uid
  33. Model = request.registry[model]
  34. search_args = list(kwargs.get('args') or [])
  35. limit = int(kwargs.get('limit') or 100)
  36. operator = kwargs.get('operator')
  37. name = kwargs.get('name')
  38. if Model._rec_name and (not name == '' and operator == 'ilike'):
  39. search_args += [(Model._rec_name, operator, name)]
  40. ids = Model.search(cr, uid, search_args, limit=limit, order='id desc', context=context)
  41. res = Model.name_get(cr, uid, ids, context)
  42. return self._extend_name(model, res)
  43. return self._call_kw(model, method, args, kwargs)
  44. @http.route('/web/dataset/call_kw/<model>/name_get', type='json', auth="user")
  45. def name_get(self, model, method, args, kwargs):
  46. res = self._call_kw(model, method, args, kwargs)
  47. context = kwargs.get('context')
  48. if context and context.get('extended_name_with_contact'):
  49. res = self._extend_name(model, res)
  50. return res