diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
| commit | 3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch) | |
| tree | a44932296ef4a9b71d5f010906253d8c53727726 /addons/microsoft_calendar/utils | |
| parent | 0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff) | |
initial commit 2
Diffstat (limited to 'addons/microsoft_calendar/utils')
| -rw-r--r-- | addons/microsoft_calendar/utils/__init__.py | 4 | ||||
| -rw-r--r-- | addons/microsoft_calendar/utils/microsoft_calendar.py | 105 | ||||
| -rw-r--r-- | addons/microsoft_calendar/utils/microsoft_event.py | 223 |
3 files changed, 332 insertions, 0 deletions
diff --git a/addons/microsoft_calendar/utils/__init__.py b/addons/microsoft_calendar/utils/__init__.py new file mode 100644 index 00000000..15a47c3f --- /dev/null +++ b/addons/microsoft_calendar/utils/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. +from . import microsoft_calendar +from . import microsoft_event diff --git a/addons/microsoft_calendar/utils/microsoft_calendar.py b/addons/microsoft_calendar/utils/microsoft_calendar.py new file mode 100644 index 00000000..04029468 --- /dev/null +++ b/addons/microsoft_calendar/utils/microsoft_calendar.py @@ -0,0 +1,105 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +import requests +import json +import logging + +from werkzeug import urls + +from odoo import api, _ +from odoo.addons.microsoft_calendar.utils.microsoft_event import MicrosoftEvent +from odoo.addons.microsoft_account.models.microsoft_service import TIMEOUT + + +_logger = logging.getLogger(__name__) + +def requires_auth_token(func): + def wrapped(self, *args, **kwargs): + if not kwargs.get('token'): + raise AttributeError("An authentication token is required") + return func(self, *args, **kwargs) + return wrapped + +class InvalidSyncToken(Exception): + pass + +class MicrosoftCalendarService(): + + def __init__(self, microsoft_service): + self.microsoft_service = microsoft_service + + @requires_auth_token + def get_events(self, sync_token=None, token=None, timeout=TIMEOUT): + url = "/v1.0/me/calendarView/delta" + headers = {'Content-type': 'application/json', 'Authorization': 'Bearer %s' % token} + params = {} + if sync_token: + params['$deltatoken'] = sync_token + else: + params['startDateTime'] = '2016-12-01T00:00:00Z' + params['endDateTime'] = '2030-1-01T00:00:00Z' + try: + status, data, time = self.microsoft_service._do_request(url, params, headers, method='GET', timeout=timeout) + except requests.HTTPError as e: + if e.response.status_code == 410 and 'fullSyncRequired' in str(e.response.content): + raise InvalidSyncToken("Invalid sync token. Full sync required") + raise e + + events = data.get('value', []) + next_page_token = data.get('@odata.nextLink') + while next_page_token: + status, data, time = self.microsoft_service._do_request(next_page_token, {}, headers, preuri='', method='GET', timeout=timeout) + next_page_token = data.get('@odata.nextLink') + events += data.get('value', []) + + next_sync_token_url = data.get('@odata.deltaLink') + next_sync_token = urls.url_parse(next_sync_token_url).decode_query().get('$deltatoken', False) + + default_reminders = data.get('defaultReminders') + + return MicrosoftEvent(events), next_sync_token, default_reminders + + @requires_auth_token + def insert(self, values, token=None, timeout=TIMEOUT): + url = "/v1.0/me/calendar/events" + headers = {'Content-type': 'application/json', 'Authorization': 'Bearer %s' % token} + if not values.get('id'): + values.pop('id', None) + status, data, time = self.microsoft_service._do_request(url, json.dumps(values), headers, method='POST', timeout=timeout) + return data['id'] + + @requires_auth_token + def patch(self, event_id, values, token=None, timeout=TIMEOUT): + url = "/v1.0/me/calendar/events/%s" % event_id + headers = {'Content-type': 'application/json', 'Authorization': 'Bearer %s' % token} + self.microsoft_service._do_request(url, json.dumps(values), headers, method='PATCH', timeout=timeout) + + @requires_auth_token + def delete(self, event_id, token=None, timeout=TIMEOUT): + url = "/v1.0/me/calendar/events/%s" % event_id + headers = {'Authorization': 'Bearer %s' % token} + params = {} + try: + self.microsoft_service._do_request(url, params, headers=headers, method='DELETE', timeout=timeout) + except requests.HTTPError as e: + # For some unknown reason Microsoft can also return a 403 response when the event is already cancelled. + if e.response.status_code not in (410, 403): + raise e + _logger.info("Microsoft event %s was already deleted" % event_id) + + ##################################### + ## MANAGE CONNEXION TO MICROSOFT ## + ##################################### + + def is_authorized(self, user): + return bool(user.sudo().microsoft_calendar_rtoken) + + def _get_calendar_scope(self): + return 'offline_access openid Calendars.ReadWrite' + + def _microsoft_authentication_url(self, from_url='http://www.odoo.com'): + return self.microsoft_service._get_authorize_uri(from_url, service='calendar', scope=self._get_calendar_scope()) + + def _can_authorize_microsoft(self, user): + return user.has_group('base.group_erp_manager') diff --git a/addons/microsoft_calendar/utils/microsoft_event.py b/addons/microsoft_calendar/utils/microsoft_event.py new file mode 100644 index 00000000..333c4511 --- /dev/null +++ b/addons/microsoft_calendar/utils/microsoft_event.py @@ -0,0 +1,223 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. +from odoo.api import model +from typing import Iterator, Mapping +from collections import abc + + +class MicrosoftEvent(abc.Set): + """This helper class holds the values of a Microsoft event. + Inspired by Odoo recordset, one instance can be a single Microsoft event or a + (immutable) set of Microsoft events. + All usual set operations are supported (union, intersection, etc). + + :param iterable: iterable of MicrosoftCalendar instances or iterable of dictionnaries + + """ + + def __init__(self, iterable=()): + self._events = {} + for item in iterable: + if isinstance(item, self.__class__): + self._events[item.id] = item._events[item.id] + elif isinstance(item, Mapping): + self._events[item.get('id')] = item + else: + raise ValueError("Only %s or iterable of dict are supported" % self.__class__.__name__) + + def __iter__(self) -> Iterator['MicrosoftEvent']: + return iter(MicrosoftEvent([vals]) for vals in self._events.values()) + + def __contains__(self, microsoft_event): + return microsoft_event.id in self._events + + def __len__(self): + return len(self._events) + + def __bool__(self): + return bool(self._events) + + def __getattr__(self, name): + # ensure_one + try: + event, = self._events.keys() + except ValueError: + raise ValueError("Expected singleton: %s" % self) + event_id = list(self._events.keys())[0] + return self._events[event_id].get(name) + + def __repr__(self): + return '%s%s' % (self.__class__.__name__, self.ids) + + @property + def ids(self): + return tuple(e.id for e in self) + + def microsoft_ids(self): + return tuple(e.id for e in self) + + def odoo_id(self, env): + self.odoo_ids(env) # load ids + return self._odoo_id + + def _meta_odoo_id(self, microsoft_guid): + """Returns the Odoo id stored in the Microsoft Event metadata. + This id might not actually exists in the database. + """ + if self.singleValueExtendedProperties: + o_id = [prop['value'] for prop in self.singleValueExtendedProperties if prop['id'] == 'String {%s} Name odoo_id' % microsoft_guid][0] + return int(o_id) + + def odoo_ids(self, env): + ids = tuple(e._odoo_id for e in self if e._odoo_id) + if len(ids) == len(self): + return ids + found = self._load_odoo_ids_from_db(env) + unsure = self - found + if unsure: + unsure._load_odoo_ids_from_metadata(env) + + return tuple(e._odoo_id for e in self) + + def _load_odoo_ids_from_metadata(self, env): + model_env = self._get_model(env) + microsoft_guid = env['ir.config_parameter'].sudo().get_param('microsoft_calendar.microsoft_guid', False) + unsure_odoo_ids = tuple(e._meta_odoo_id(microsoft_guid) for e in self) + odoo_events = model_env.browse(_id for _id in unsure_odoo_ids if _id) + + # Extended properties are copied when splitting a recurrence Microsoft side. + # Hence, we may have two Microsoft recurrences linked to the same Odoo id. + # Therefore, we only consider Odoo records without microsoft id when trying + # to match events. + o_ids = odoo_events.exists().filtered(lambda e: not e.microsoft_id).ids + for e in self: + odoo_id = e._meta_odoo_id(microsoft_guid) + if odoo_id in o_ids: + e._events[e.id]['_odoo_id'] = odoo_id + + def _load_odoo_ids_from_db(self, env): + model_env = self._get_model(env) + odoo_events = model_env.with_context(active_test=False)._from_microsoft_ids(self.ids).with_env(env) + mapping = {e.microsoft_id: e.id for e in odoo_events} + existing_microsoft_ids = odoo_events.mapped('microsoft_id') + for e in self: + odoo_id = mapping.get(e.id) + if odoo_id: + e._events[e.id]['_odoo_id'] = odoo_id + return self.filter(lambda e: e.id in existing_microsoft_ids) + + def owner(self, env): + # Owner/organizer could be desynchronised between Microsoft and Odoo. + # Let userA, userB be two new users (never synced to Microsoft before). + # UserA creates an event in Odoo (he is the owner) but userB syncs first. + # There is no way to insert the event into userA's calendar since we don't have + # any authentication access. The event is therefore inserted into userB's calendar + # (he is the orginizer in Microsoft). The "real" owner (in Odoo) is stored as an + # extended property. There is currently no support to "transfert" ownership when + # userA syncs his calendar the first time. + if self.singleValueExtendedProperties: + microsoft_guid = env['ir.config_parameter'].sudo().get_param('microsoft_calendar.microsoft_guid', False) + real_owner_id = [prop['value'] for prop in self.singleValueExtendedProperties if prop['id'] == 'String {%s} Name owner_odoo_id' % microsoft_guid][0] + real_owner = real_owner_id and env['res.users'].browse(int(real_owner_id)) + else: + real_owner_id = False + + if real_owner_id and real_owner.exists(): + return real_owner + elif self.isOrganizer: + return env.user + elif self.organizer and self.organizer.get('emailAddress') and self.organizer.get('emailAddress').get('address'): + # In Microsoft: 1 email = 1 user; but in Odoo several users might have the same email + return env['res.users'].search([('email', '=', self.organizer.get('emailAddress').get('address'))], limit=1) + else: + return env['res.users'] + + def filter(self, func) -> 'MicrosoftEvent': + return MicrosoftEvent(e for e in self if func(e)) + + def is_recurrence(self): + return self.type == 'seriesMaster' + + def is_recurrent(self): + return bool(self.seriesMasterId or self.is_recurrence()) + + def is_recurrent_not_master(self): + return bool(self.seriesMasterId) + + def get_recurrence(self): + if not self.recurrence: + return {} + pattern = self.recurrence['pattern'] + range = self.recurrence['range'] + end_type_dict = { + 'endDate': 'end_date', + 'noEnd': 'forever', + 'numbered': 'count', + } + type_dict = { + 'absoluteMonthly': 'monthly', + 'relativeMonthly': 'monthly', + 'absoluteYearly': 'yearly', + 'relativeYearly': 'yearly', + } + index_dict = { + 'first': '1', + 'second': '2', + 'third': '3', + 'fourth': '4', + 'last': '-1', + } + rrule_type = type_dict.get(pattern['type'], pattern['type']) + interval = pattern['interval'] + if rrule_type == 'yearly': + interval *= 12 + result = { + 'rrule_type': rrule_type, + 'end_type': end_type_dict.get(range['type'], False), + 'interval': interval, + 'count': range['numberOfOccurrences'], + 'day': pattern['dayOfMonth'], + 'byday': index_dict.get(pattern['index'], False), + 'until': range['type'] == 'endDate' and range['endDate'], + } + + month_by_dict = { + 'absoluteMonthly': 'date', + 'relativeMonthly': 'day', + 'absoluteYearly': 'date', + 'relativeYearly': 'day', + } + month_by = month_by_dict.get(pattern['type'], False) + if month_by: + result['month_by'] = month_by + + week_days = [x[:2] for x in pattern.get('daysOfWeek', [])] + for week_day in ['mo', 'tu', 'we', 'th', 'fr', 'sa', 'su']: + result[week_day] = week_day in week_days + if week_days: + result['weekday'] = week_days[0].upper() + return result + + def is_cancelled(self): + return bool(self.isCancelled or (self.__getattr__('@removed') and self.__getattr__('@removed').get('reason') == 'deleted')) + + def is_recurrence_outlier(self): + return bool(self.originalStartTime) + + def cancelled(self): + return self.filter(lambda e: e.is_cancelled()) + + def exists(self, env) -> 'MicrosoftEvent': + recurrences = self.filter(MicrosoftEvent.is_recurrence) + events = self - recurrences + recurrences.odoo_ids(env) + events.odoo_ids(env) + + return self.filter(lambda e: e._odoo_id) + + def _get_model(self, env): + if all(e.is_recurrence() for e in self): + return env['calendar.recurrence'] + if all(not e.is_recurrence() for e in self): + return env['calendar.event'] + raise TypeError("Mixing Microsoft events and Microsoft recurrences") |
