Odoo modules related to events management
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.

138 lines
4.7 KiB

  1. import pytz
  2. from datetime import timedelta
  3. from odoo import models, fields, api, _
  4. class EventEvent(models.Model):
  5. _inherit = "event.event"
  6. meeting_ids = fields.One2many(
  7. comodel_name="calendar.event",
  8. inverse_name="res_id",
  9. string="Linked meetings",
  10. )
  11. meeting_count = fields.Integer(
  12. string="Meeting count",
  13. compute="_count_meetings",
  14. )
  15. in_user_agenda = fields.Boolean(
  16. "In User agenda",
  17. compute="_get_in_user_agenda",
  18. )
  19. def _count_meetings(self):
  20. for event in self:
  21. event.meeting_count = len(event.meeting_ids)
  22. def _get_in_user_agenda(self):
  23. uid = self.env.uid
  24. for event in self:
  25. event.in_user_agenda = uid in event.meeting_ids.mapped("user_id").ids
  26. def _prepare_agenda_vals(self, user_id=False):
  27. self.ensure_one()
  28. self_model_id = self.env["ir.model"]._get_id(self._name)
  29. uid = user_id or self.env.uid
  30. groupped_tags = {}
  31. for tag in self.tag_ids:
  32. categ = tag.category_id.name
  33. if categ in groupped_tags:
  34. groupped_tags[categ] += ", {}".format(tag.name)
  35. else:
  36. groupped_tags[categ] = tag.name
  37. event_tz = pytz.timezone(self.date_tz)
  38. return {
  39. "name": self.name + " (event Odoo)",
  40. "description": _("<p>Organizer: {}{}<br/>Note:\n</p>").format(
  41. self.organizer_id.display_name,
  42. "".join(
  43. [
  44. "<br/>{} : {}".format(tag_group[0], tag_group[1])
  45. for tag_group in groupped_tags.items()
  46. ]
  47. ),
  48. self.note,
  49. ),
  50. "user_id": uid,
  51. "location": self.address_inline,
  52. "start": event_tz.localize(self.date_begin)
  53. .astimezone(pytz.timezone(self.env.user.tz))
  54. .replace(tzinfo=None),
  55. "stop": event_tz.localize(self.date_end)
  56. .astimezone(pytz.timezone(self.env.user.tz))
  57. .replace(tzinfo=None),
  58. "res_id": self.id,
  59. "res_model_id": self_model_id,
  60. # "event_tz": event.date_tz,
  61. }
  62. def add_to_agenda(self, user_id=False):
  63. vals_list = []
  64. uid = user_id or self.env.uid
  65. to_add = self.filtered(
  66. lambda ee: uid not in ee.meeting_ids.mapped("user_id.id")
  67. )
  68. for event in to_add:
  69. vals_list.append(event._prepare_agenda_vals())
  70. if vals_list:
  71. self.env["calendar.event"].create(vals_list)
  72. def remove_from_agenda(self, user_id=False):
  73. to_unlink = self.mapped("meeting_ids").filtered(
  74. lambda meeting: meeting.user_id.id == user_id or self.env.uid
  75. )
  76. if to_unlink:
  77. to_unlink.unlink()
  78. def display_meetings(self):
  79. self.ensure_one()
  80. date_begin = self.date_begin.date()
  81. return {
  82. "name": _("Meetings"),
  83. "type": "ir.actions.act_window",
  84. "res_model": "calendar.event",
  85. "views": [(False, "tree"), (False, "calendar"), (False, "form")],
  86. "domain": [("res_id", "=", self.id), ("res_model", "=", self._name)],
  87. "context": {
  88. "calendar_mode": "week",
  89. "initial_date": fields.Date.to_string(
  90. date_begin - timedelta(days=-date_begin.weekday())
  91. ),
  92. },
  93. }
  94. @api.model
  95. def agenda_fields_mapping(self):
  96. return {
  97. "name": "name",
  98. "adress_inline": "location",
  99. "date_begin_located": "start",
  100. "date_end_located": "stop",
  101. "organizer_id": "description",
  102. "tag_ids": "description",
  103. "note": "description",
  104. }
  105. def update_agenda_events(self, ce_fields):
  106. self.ensure_one()
  107. ce_vals = self._prepare_agenda_vals()
  108. self.meeting_ids.write(dict([(field, ce_vals[field]) for field in ce_fields]))
  109. def write(self, vals):
  110. res = super().write(vals)
  111. agenda_fields_map = self.agenda_fields_mapping()
  112. agenda_fields = agenda_fields_map.keys()
  113. meetings = self.mapped("meeting_ids")
  114. if meetings and any(field in agenda_fields for field in vals.keys()):
  115. for event in self.filtered("meeting_ids"):
  116. event.update_agenda_events(
  117. ce_fields=set(
  118. agenda_fields_map[agenda_field]
  119. for agenda_field in agenda_fields & vals.keys()
  120. )
  121. )
  122. # meetings.write(
  123. # dict([(field, vals[field]) for field in agenda_fields & vals.keys()])
  124. # )
  125. return res