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.

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