From 01b63e553bb332a02ebaa56f53f834acc1df72f7 Mon Sep 17 00:00:00 2001 From: RemiFr82 Date: Thu, 29 Aug 2024 19:37:03 +0200 Subject: [PATCH] [FIX] update calendar event info --- event_to_calendar/models/event_event.py | 117 ++++++++++++++---------- 1 file changed, 67 insertions(+), 50 deletions(-) diff --git a/event_to_calendar/models/event_event.py b/event_to_calendar/models/event_event.py index 3d1f40f..5aa7128 100644 --- a/event_to_calendar/models/event_event.py +++ b/event_to_calendar/models/event_event.py @@ -29,48 +29,51 @@ class EventEvent(models.Model): for event in self: event.in_user_agenda = uid in event.meeting_ids.mapped("user_id").ids + def _prepare_agenda_vals(self, user_id=False): + self.ensure_one() + self_model_id = self.env["ir.model"]._get_id(self._name) + uid = user_id or self.env.uid + groupped_tags = {} + for tag in self.tag_ids: + categ = tag.category_id.name + if categ in groupped_tags: + groupped_tags[categ] += ", {}".format(tag.name) + else: + groupped_tags[categ] = tag.name + event_tz = pytz.timezone(self.date_tz) + return { + "name": self.name + " (event Odoo)", + "description": _("

Organizer: {}{}
Note:\n

").format( + self.organizer_id.display_name, + "".join( + [ + "
{} : {}".format(tag_group[0], tag_group[1]) + for tag_group in groupped_tags.items() + ] + ), + self.note, + ), + "user_id": uid, + "location": self.address_inline, + "start": event_tz.localize(self.date_begin) + .astimezone(pytz.timezone(self.env.user.tz)) + .replace(tzinfo=None), + "stop": event_tz.localize(self.date_end) + .astimezone(pytz.timezone(self.env.user.tz)) + .replace(tzinfo=None), + "res_id": self.id, + "res_model_id": self_model_id, + # "event_tz": event.date_tz, + } + def add_to_agenda(self, user_id=False): vals_list = [] - self_model_id = self.env["ir.model"]._get_id(self._name) uid = user_id or self.env.uid to_add = self.filtered( lambda ee: uid not in ee.meeting_ids.mapped("user_id.id") ) for event in to_add: - groupped_tags = {} - for tag in event.tag_ids: - categ = tag.category_id.name - if categ in groupped_tags: - groupped_tags[categ] += ", {}".format(tag.name) - else: - groupped_tags[categ] = tag.name - event_tz = pytz.timezone(event.date_tz) - vals_list.append( - { - "name": event.name + " (event Odoo)", - "description": _("

Organizer: {}{}
Note:\n

").format( - event.organizer_id.display_name, - "".join( - [ - "
{} : {}".format(tag_group[0], tag_group[1]) - for tag_group in groupped_tags.items() - ] - ), - event.note, - ), - "user_id": uid, - "location": event.address_inline, - "start": event_tz.localize(event.date_begin) - .astimezone(pytz.timezone(self.env.user.tz)) - .replace(tzinfo=None), - "stop": event_tz.localize(event.date_end) - .astimezone(pytz.timezone(self.env.user.tz)) - .replace(tzinfo=None), - "res_id": event.id, - "res_model_id": self_model_id, - # "event_tz": event.date_tz, - } - ) + vals_list.append(event._prepare_agenda_vals()) if vals_list: self.env["calendar.event"].create(vals_list) @@ -99,23 +102,37 @@ class EventEvent(models.Model): } @api.model - def agenda_fields(self): - return [ - "name", - "date_begin_located", - "date_end_located", - "tag_ids", - "organizer_id", - "adress_inline", - "note", - ] + def agenda_fields_mapping(self): + return { + "name": "name", + "adress_inline": "location", + "date_begin_located": "start", + "date_end_located": "stop", + "organizer_id": "description", + "tag_ids": "description", + "note": "description", + } + + def update_agenda_events(self, ce_fields): + self.ensure_one() + ce_vals = self._prepare_agenda_vals() + self.meeting_ids.write(dict([(field, ce_vals[field]) for field in ce_fields])) def write(self, vals): res = super().write(vals) - agenda_fields = self.agenda_fields() + agenda_fields_map = self.agenda_fields_mapping() + agenda_fields = agenda_fields_map.keys() meetings = self.mapped("meeting_ids") - if any(field in agenda_fields for field in vals.keys()) and meetings: - meetings.write( - dict([(field, vals[field]) for field in agenda_fields & vals.keys()]) - ) + if meetings and any(field in agenda_fields for field in vals.keys()): + for event in self.filtered("meeting_ids"): + event.update_agenda_events( + ce_fields=set( + agenda_fields_map[agenda_field] + for agenda_field in agenda_fields & vals.keys() + ) + ) + + # meetings.write( + # dict([(field, vals[field]) for field in agenda_fields & vals.keys()]) + # ) return res