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.

69 lines
3.0 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. import openerp
  6. class MailChatController(openerp.addons.bus.controllers.main.BusController):
  7. # -----------------------------
  8. # Extends BUS Controller Poll
  9. # -----------------------------
  10. def _poll(self, dbname, channels, last, options):
  11. if request.session.uid:
  12. registry, cr, uid, context = request.registry, request.cr, request.session.uid, request.context
  13. channels.append((request.db, 'mail_move_message'))
  14. channels.append((request.db, 'mail_move_message.delete_message'))
  15. return super(MailChatController, self)._poll(dbname, channels, last, options)
  16. class DataSetCustom(DataSet):
  17. def _extend_name(self, model, records):
  18. cr, uid, context = request.cr, request.uid, request.context
  19. Model = request.registry[model]
  20. fields = Model.fields_get(cr, uid, False, context)
  21. contact_field = False
  22. for n, f in fields.iteritems():
  23. if f['type'] == 'many2one' and f['relation'] == 'res.partner':
  24. contact_field = n
  25. break
  26. partner_info = {}
  27. if contact_field:
  28. partner_info = Model.read(cr, uid, [r[0] for r in records], [contact_field], context)
  29. partner_info = dict([(p['id'], p[contact_field]) for p in partner_info])
  30. res = []
  31. for r in records:
  32. if partner_info.get(r[0]):
  33. res.append((r[0], _('%s [%s] ID %s') % (r[1], partner_info.get(r[0])[1], r[0])))
  34. else:
  35. res.append((r[0], _('%s ID %s') % (r[1], r[0])))
  36. return res
  37. @http.route('/web/dataset/call_kw/<model>/name_search', type='json', auth="user")
  38. def name_search(self, model, method, args, kwargs):
  39. context = kwargs.get('context')
  40. if context and context.get('extended_name_with_contact'):
  41. # add order by ID desc
  42. cr, uid = request.cr, request.uid
  43. Model = request.registry[model]
  44. search_args = list(kwargs.get('args') or [])
  45. limit = int(kwargs.get('limit') or 100)
  46. operator = kwargs.get('operator')
  47. name = kwargs.get('name')
  48. if Model._rec_name and (not name == '' and operator == 'ilike'):
  49. search_args += [(Model._rec_name, operator, name)]
  50. ids = Model.search(cr, uid, search_args, limit=limit, order='id desc', context=context)
  51. res = Model.name_get(cr, uid, ids, context)
  52. return self._extend_name(model, res)
  53. return self._call_kw(model, method, args, kwargs)
  54. @http.route('/web/dataset/call_kw/<model>/name_get', type='json', auth="user")
  55. def name_get(self, model, method, args, kwargs):
  56. res = self._call_kw(model, method, args, kwargs)
  57. context = kwargs.get('context')
  58. if context and context.get('extended_name_with_contact'):
  59. res = self._extend_name(model, res)
  60. return res