calendar.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. import pytz
  4. from dateutil.parser import parse
  5. from dateutil.relativedelta import relativedelta
  6. from uuid import uuid4
  7. from odoo import api, fields, models, tools, _
  8. from odoo.addons.google_calendar.utils.google_calendar import GoogleCalendarService
  9. class Meeting(models.Model):
  10. _name = 'calendar.event'
  11. _inherit = ['calendar.event', 'google.calendar.sync']
  12. google_id = fields.Char(
  13. 'Google Calendar Event Id', compute='_compute_google_id', store=True, readonly=False)
  14. @api.depends('recurrence_id.google_id')
  15. def _compute_google_id(self):
  16. # google ids of recurring events are built from the recurrence id and the
  17. # original starting time in the recurrence.
  18. # The `start` field does not appear in the dependencies on purpose!
  19. # Event if the event is moved, the google_id remains the same.
  20. for event in self:
  21. google_recurrence_id = event.recurrence_id._get_event_google_id(event)
  22. if not event.google_id and google_recurrence_id:
  23. event.google_id = google_recurrence_id
  24. elif not event.google_id:
  25. event.google_id = False
  26. @api.model
  27. def _get_google_synced_fields(self):
  28. return {'name', 'description', 'allday', 'start', 'date_end', 'stop',
  29. 'attendee_ids', 'alarm_ids', 'location', 'privacy', 'active'}
  30. @api.model
  31. def _restart_google_sync(self):
  32. self.env['calendar.event'].search(self._get_sync_domain()).write({
  33. 'need_sync': True,
  34. })
  35. @api.model_create_multi
  36. def create(self, vals_list):
  37. notify_context = self.env.context.get('dont_notify', False)
  38. return super(Meeting, self.with_context(dont_notify=notify_context)).create([
  39. dict(vals, need_sync=False) if vals.get('recurrence_id') or vals.get('recurrency') else vals
  40. for vals in vals_list
  41. ])
  42. @api.model
  43. def _check_values_to_sync(self, values):
  44. """ Return True if values being updated intersects with Google synced values and False otherwise. """
  45. synced_fields = self._get_google_synced_fields()
  46. values_to_sync = any(key in synced_fields for key in values)
  47. return values_to_sync
  48. @api.model
  49. def _get_update_future_events_values(self):
  50. """ Add parameters for updating events within the _update_future_events function scope. """
  51. update_future_events_values = super()._get_update_future_events_values()
  52. return {**update_future_events_values, 'need_sync': False}
  53. @api.model
  54. def _get_remove_sync_id_values(self):
  55. """ Add parameters for removing event synchronization while updating the events in super class. """
  56. remove_sync_id_values = super()._get_remove_sync_id_values()
  57. return {**remove_sync_id_values, 'google_id': False}
  58. @api.model
  59. def _get_archive_values(self):
  60. """ Return the parameters for archiving events. Do not synchronize events after archiving. """
  61. archive_values = super()._get_archive_values()
  62. return {**archive_values, 'need_sync': False}
  63. def write(self, values):
  64. recurrence_update_setting = values.get('recurrence_update')
  65. if recurrence_update_setting in ('all_events', 'future_events') and len(self) == 1:
  66. values = dict(values, need_sync=False)
  67. notify_context = self.env.context.get('dont_notify', False)
  68. res = super(Meeting, self.with_context(dont_notify=notify_context)).write(values)
  69. if recurrence_update_setting in ('all_events',) and len(self) == 1 and values.keys() & self._get_google_synced_fields():
  70. self.recurrence_id.need_sync = True
  71. return res
  72. def _get_sync_domain(self):
  73. # in case of full sync, limit to a range of 1y in past and 1y in the future by default
  74. ICP = self.env['ir.config_parameter'].sudo()
  75. day_range = int(ICP.get_param('google_calendar.sync.range_days', default=365))
  76. lower_bound = fields.Datetime.subtract(fields.Datetime.now(), days=day_range)
  77. upper_bound = fields.Datetime.add(fields.Datetime.now(), days=day_range)
  78. return [
  79. ('partner_ids.user_ids', 'in', self.env.user.id),
  80. ('stop', '>', lower_bound),
  81. ('start', '<', upper_bound),
  82. # Do not sync events that follow the recurrence, they are already synced at recurrence creation
  83. '!', '&', '&', ('recurrency', '=', True), ('recurrence_id', '!=', False), ('follow_recurrence', '=', True)
  84. ]
  85. @api.model
  86. def _odoo_values(self, google_event, default_reminders=()):
  87. if google_event.is_cancelled():
  88. return {'active': False}
  89. # default_reminders is never () it is set to google's default reminder (30 min before)
  90. # we need to check 'useDefault' for the event to determine if we have to use google's
  91. # default reminder or not
  92. reminder_command = google_event.reminders.get('overrides')
  93. if not reminder_command:
  94. reminder_command = google_event.reminders.get('useDefault') and default_reminders or ()
  95. alarm_commands = self._odoo_reminders_commands(reminder_command)
  96. attendee_commands, partner_commands = self._odoo_attendee_commands(google_event)
  97. related_event = self.search([('google_id', '=', google_event.id)], limit=1)
  98. name = google_event.summary or related_event and related_event.name or _("(No title)")
  99. values = {
  100. 'name': name,
  101. 'description': google_event.description and tools.html_sanitize(google_event.description),
  102. 'location': google_event.location,
  103. 'user_id': google_event.owner(self.env).id,
  104. 'privacy': google_event.visibility or self.default_get(['privacy'])['privacy'],
  105. 'attendee_ids': attendee_commands,
  106. 'alarm_ids': alarm_commands,
  107. 'recurrency': google_event.is_recurrent(),
  108. 'videocall_location': google_event.get_meeting_url(),
  109. 'show_as': 'free' if google_event.is_available() else 'busy'
  110. }
  111. if partner_commands:
  112. # Add partner_commands only if set from Google. The write method on calendar_events will
  113. # override attendee commands if the partner_ids command is set but empty.
  114. values['partner_ids'] = partner_commands
  115. if not google_event.is_recurrence():
  116. values['google_id'] = google_event.id
  117. if google_event.is_recurrent() and not google_event.is_recurrence():
  118. # Propagate the follow_recurrence according to the google result
  119. values['follow_recurrence'] = google_event.is_recurrence_follower()
  120. if google_event.start.get('dateTime'):
  121. # starting from python3.7, use the new [datetime, date].fromisoformat method
  122. start = parse(google_event.start.get('dateTime')).astimezone(pytz.utc).replace(tzinfo=None)
  123. stop = parse(google_event.end.get('dateTime')).astimezone(pytz.utc).replace(tzinfo=None)
  124. values['allday'] = False
  125. else:
  126. start = parse(google_event.start.get('date'))
  127. stop = parse(google_event.end.get('date')) - relativedelta(days=1)
  128. # Stop date should be exclusive as defined here https://developers.google.com/calendar/v3/reference/events#resource
  129. # but it seems that's not always the case for old event
  130. if stop < start:
  131. stop = parse(google_event.end.get('date'))
  132. values['allday'] = True
  133. if related_event['start'] != start:
  134. values['start'] = start
  135. if related_event['stop'] != stop:
  136. values['stop'] = stop
  137. return values
  138. @api.model
  139. def _odoo_attendee_commands(self, google_event):
  140. attendee_commands = []
  141. partner_commands = []
  142. google_attendees = google_event.attendees or []
  143. if len(google_attendees) == 0 and google_event.organizer and google_event.organizer.get('self', False):
  144. user = google_event.owner(self.env)
  145. google_attendees += [{
  146. 'email': user.partner_id.email,
  147. 'responseStatus': 'accepted',
  148. }]
  149. emails = [a.get('email') for a in google_attendees]
  150. existing_attendees = self.env['calendar.attendee']
  151. if google_event.exists(self.env):
  152. event = google_event.get_odoo_event(self.env)
  153. existing_attendees = event.attendee_ids
  154. attendees_by_emails = {tools.email_normalize(a.email): a for a in existing_attendees}
  155. partners = self._get_sync_partner(emails)
  156. for attendee in zip(emails, partners, google_attendees):
  157. email = attendee[0]
  158. if email in attendees_by_emails:
  159. # Update existing attendees
  160. attendee_commands += [(1, attendees_by_emails[email].id, {'state': attendee[2].get('responseStatus')})]
  161. else:
  162. # Create new attendees
  163. if attendee[2].get('self'):
  164. partner = self.env.user.partner_id
  165. elif attendee[1]:
  166. partner = attendee[1]
  167. else:
  168. continue
  169. attendee_commands += [(0, 0, {'state': attendee[2].get('responseStatus'), 'partner_id': partner.id})]
  170. partner_commands += [(4, partner.id)]
  171. if attendee[2].get('displayName') and not partner.name:
  172. partner.name = attendee[2].get('displayName')
  173. for odoo_attendee in attendees_by_emails.values():
  174. # Remove old attendees but only if it does not correspond to the current user.
  175. email = tools.email_normalize(odoo_attendee.email)
  176. if email not in emails and email != self.env.user.email:
  177. attendee_commands += [(2, odoo_attendee.id)]
  178. partner_commands += [(3, odoo_attendee.partner_id.id)]
  179. return attendee_commands, partner_commands
  180. @api.model
  181. def _odoo_reminders_commands(self, reminders=()):
  182. commands = []
  183. for reminder in reminders:
  184. alarm_type = 'email' if reminder.get('method') == 'email' else 'notification'
  185. alarm_type_label = _("Email") if alarm_type == 'email' else _("Notification")
  186. minutes = reminder.get('minutes', 0)
  187. alarm = self.env['calendar.alarm'].search([
  188. ('alarm_type', '=', alarm_type),
  189. ('duration_minutes', '=', minutes)
  190. ], limit=1)
  191. if alarm:
  192. commands += [(4, alarm.id)]
  193. else:
  194. if minutes % (60*24) == 0:
  195. interval = 'days'
  196. duration = minutes / 60 / 24
  197. name = _(
  198. "%(reminder_type)s - %(duration)s Days",
  199. reminder_type=alarm_type_label,
  200. duration=duration,
  201. )
  202. elif minutes % 60 == 0:
  203. interval = 'hours'
  204. duration = minutes / 60
  205. name = _(
  206. "%(reminder_type)s - %(duration)s Hours",
  207. reminder_type=alarm_type_label,
  208. duration=duration,
  209. )
  210. else:
  211. interval = 'minutes'
  212. duration = minutes
  213. name = _(
  214. "%(reminder_type)s - %(duration)s Minutes",
  215. reminder_type=alarm_type_label,
  216. duration=duration,
  217. )
  218. commands += [(0, 0, {'duration': duration, 'interval': interval, 'name': name, 'alarm_type': alarm_type})]
  219. return commands
  220. def action_mass_archive(self, recurrence_update_setting):
  221. """ Delete recurrence in Odoo if in 'all_events' or in 'future_events' edge case, triggering one mail. """
  222. self.ensure_one()
  223. google_service = GoogleCalendarService(self.env['google.service'])
  224. archive_future_events = recurrence_update_setting == 'future_events' and self == self.recurrence_id.base_event_id
  225. if recurrence_update_setting == 'all_events' or archive_future_events:
  226. self.recurrence_id.with_context(is_recurrence=True)._google_delete(google_service, self.recurrence_id.google_id)
  227. # Increase performance handling 'future_events' edge case as it was an 'all_events' update.
  228. if archive_future_events:
  229. recurrence_update_setting = 'all_events'
  230. super(Meeting, self).action_mass_archive(recurrence_update_setting)
  231. def _google_values(self):
  232. if self.allday:
  233. start = {'date': self.start_date.isoformat()}
  234. end = {'date': (self.stop_date + relativedelta(days=1)).isoformat()}
  235. else:
  236. start = {'dateTime': pytz.utc.localize(self.start).isoformat()}
  237. end = {'dateTime': pytz.utc.localize(self.stop).isoformat()}
  238. reminders = [{
  239. 'method': "email" if alarm.alarm_type == "email" else "popup",
  240. 'minutes': alarm.duration_minutes
  241. } for alarm in self.alarm_ids]
  242. attendees = self.attendee_ids
  243. attendee_values = [{
  244. 'email': attendee.partner_id.email_normalized,
  245. 'responseStatus': attendee.state or 'needsAction',
  246. } for attendee in attendees if attendee.partner_id.email_normalized]
  247. # We sort the attendees to avoid undeterministic test fails. It's not mandatory for Google.
  248. attendee_values.sort(key=lambda k: k['email'])
  249. values = {
  250. 'id': self.google_id,
  251. 'start': start,
  252. 'end': end,
  253. 'summary': self.name,
  254. 'description': tools.html_sanitize(self.description) if not tools.is_html_empty(self.description) else '',
  255. 'location': self.location or '',
  256. 'guestsCanModify': True,
  257. 'organizer': {'email': self.user_id.email, 'self': self.user_id == self.env.user},
  258. 'attendees': attendee_values,
  259. 'extendedProperties': {
  260. 'shared': {
  261. '%s_odoo_id' % self.env.cr.dbname: self.id,
  262. },
  263. },
  264. 'reminders': {
  265. 'overrides': reminders,
  266. 'useDefault': False,
  267. }
  268. }
  269. if not self.google_id and not self.videocall_location:
  270. values['conferenceData'] = {'createRequest': {'requestId': uuid4().hex}}
  271. if self.privacy:
  272. values['visibility'] = self.privacy
  273. if not self.active:
  274. values['status'] = 'cancelled'
  275. if self.user_id and self.user_id != self.env.user and not bool(self.user_id.sudo().google_calendar_token):
  276. # The organizer is an Odoo user that do not sync his calendar
  277. values['extendedProperties']['shared']['%s_owner_id' % self.env.cr.dbname] = self.user_id.id
  278. elif not self.user_id:
  279. # We can't store on the shared properties in that case without getting a 403. It can happen when
  280. # the owner is not an Odoo user: We don't store the real owner identity (mail)
  281. # If we are not the owner, we should change the post values to avoid errors because we don't have
  282. # write permissions
  283. # See https://developers.google.com/calendar/concepts/sharing
  284. keep_keys = ['id', 'summary', 'attendees', 'start', 'end', 'reminders']
  285. values = {key: val for key, val in values.items() if key in keep_keys}
  286. # values['extendedProperties']['private] should be used if the owner is not an odoo user
  287. values['extendedProperties'] = {
  288. 'private': {
  289. '%s_odoo_id' % self.env.cr.dbname: self.id,
  290. },
  291. }
  292. return values
  293. def _cancel(self):
  294. # only owner can delete => others refuse the event
  295. user = self.env.user
  296. my_cancelled_records = self.filtered(lambda e: e.user_id == user)
  297. super(Meeting, my_cancelled_records)._cancel()
  298. attendees = (self - my_cancelled_records).attendee_ids.filtered(lambda a: a.partner_id == user.partner_id)
  299. attendees.state = 'declined'
  300. def _get_event_user(self):
  301. self.ensure_one()
  302. if self.user_id and self.user_id.sudo().google_calendar_token:
  303. return self.user_id
  304. return self.env.user