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.

70 lines
2.8 KiB

  1. # -*- coding: utf-8 -*-
  2. from odoo import http
  3. from odoo.http import request
  4. from odoo.tools.translate import _
  5. from odoo.addons.bus.controllers.main import BusController
  6. from odoo.addons.web.controllers.main import DataSet
  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 = {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(
  34. (r[0], _("%s [%s] ID %s") % (r[1], partner_info.get(r[0])[1], r[0]))
  35. )
  36. else:
  37. res.append((r[0], _("%s ID %s") % (r[1], r[0])))
  38. return res
  39. @http.route("/web/dataset/call_kw/<model>/name_search", type="json", auth="user")
  40. def name_search(self, model, method, args, kwargs):
  41. context = kwargs.get("context")
  42. if context and context.get("extended_name_with_contact"):
  43. # add order by ID desc
  44. Model = request.env[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. records = Model.search(search_args, limit=limit, order="id desc")
  52. res = records.name_get()
  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