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.

67 lines
2.7 KiB

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