calendar_attendee.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import models
  4. from odoo.addons.google_calendar.models.google_sync import google_calendar_token
  5. from odoo.addons.google_calendar.utils.google_calendar import GoogleCalendarService
  6. class Attendee(models.Model):
  7. _name = 'calendar.attendee'
  8. _inherit = 'calendar.attendee'
  9. def _send_mail_to_attendees(self, mail_template, force_send=False):
  10. """ Override
  11. If not synced with Google, let Odoo in charge of sending emails
  12. Otherwise, nothing to do: Google will send them
  13. """
  14. with google_calendar_token(self.env.user.sudo()) as token:
  15. if not token:
  16. super()._send_mail_to_attendees(mail_template, force_send)
  17. def do_tentative(self):
  18. # Synchronize event after state change
  19. res = super().do_tentative()
  20. self._sync_event()
  21. return res
  22. def do_accept(self):
  23. # Synchronize event after state change
  24. res = super().do_accept()
  25. self._sync_event()
  26. return res
  27. def do_decline(self):
  28. # Synchronize event after state change
  29. res = super().do_decline()
  30. self._sync_event()
  31. return res
  32. def _sync_event(self):
  33. # For weird reasons, we can't sync status when we are not the responsible
  34. # We can't adapt google_value to only keep ['id', 'summary', 'attendees', 'start', 'end', 'reminders']
  35. # and send that. We get a Forbidden for non-organizer error even if we only send start, end that are mandatory !
  36. if self._context.get('all_events'):
  37. service = GoogleCalendarService(self.env['google.service'].with_user(self.recurrence_id.base_event_id.user_id))
  38. self.recurrence_id.with_user(self.recurrence_id.base_event_id.user_id)._sync_odoo2google(service)
  39. else:
  40. all_events = self.mapped('event_id').filtered(lambda e: e.google_id)
  41. other_events = all_events.filtered(lambda e: e.user_id and e.user_id.id != self.env.user.id)
  42. for user in other_events.mapped('user_id'):
  43. service = GoogleCalendarService(self.env['google.service'].with_user(user))
  44. other_events.filtered(lambda ev: ev.user_id.id == user.id).with_user(user)._sync_odoo2google(service)
  45. google_service = GoogleCalendarService(self.env['google.service'])
  46. (all_events - other_events)._sync_odoo2google(google_service)