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.

55 lines
2.4 KiB

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