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.

68 lines
2.8 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 = list(channels) # do not alter original list
  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. Model = request.env[model]
  20. fields = Model.fields_get()
  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.browse([r[0] for r in records]).read([contact_field])
  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. Model = request.env[model]
  43. search_args = list(kwargs.get('args') or [])
  44. limit = int(kwargs.get('limit') or 100)
  45. operator = kwargs.get('operator')
  46. name = kwargs.get('name')
  47. if Model._rec_name and (not name == '' and operator == 'ilike'):
  48. search_args += [(Model._rec_name, operator, name)]
  49. records = Model.search(search_args, limit=limit, order='id desc')
  50. res = records.name_get()
  51. return self._extend_name(model, res)
  52. return self._call_kw(model, method, args, kwargs)
  53. @http.route('/web/dataset/call_kw/<model>/name_get', type='json', auth="user")
  54. def name_get(self, model, method, args, kwargs):
  55. res = self._call_kw(model, method, args, kwargs)
  56. context = kwargs.get('context')
  57. if context and context.get('extended_name_with_contact'):
  58. res = self._extend_name(model, res)
  59. return res