From a652676e4fa10bfa12d7c06ad03244f9ffa1eba3 Mon Sep 17 00:00:00 2001 From: fkantelberg Date: Wed, 20 Nov 2024 09:22:23 +0100 Subject: [PATCH] [FIX] base_ical: Fix problem when users are created and one calendar is set to auto. Fix problem with rrule already applied by Odoo with creation of multiple records. --- base_ical/models/base_ical.py | 21 +++++++++++++++++++-- base_ical/models/res_users.py | 2 +- base_ical/views/base_ical.xml | 2 ++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/base_ical/models/base_ical.py b/base_ical/models/base_ical.py index 4be59b8df..0f332bc48 100644 --- a/base_ical/models/base_ical.py +++ b/base_ical/models/base_ical.py @@ -39,6 +39,10 @@ def _get_operating_modes(self): ) preview = fields.Text(compute="_compute_preview") code = fields.Text() + remove_repetition_rule = fields.Boolean( + help="Automatically remove the repetition rules. Useful if the repetition " + "already created multiple records" + ) expression_dtstamp = fields.Char( vevent_field="dtstamp", string="DTSTAMP", @@ -89,6 +93,7 @@ def _valid_field_parameter(self, field, name): "expression_summary", "code", "mode", + "remove_repetition_rule", ) def _compute_preview(self): for this in self: @@ -194,6 +199,9 @@ def _get_items(self, limit=None): def _get_ical(self, records=None, limit=None): """Return the vcalendar as text""" + if not self.model_id: + return "" + if self.mode == "simple": return self._get_ical_simple(records=records, limit=limit) @@ -239,6 +247,8 @@ def _get_ical_advanced(self, records=None, limit=None): } ) + to_remove = ["rrule"] if self.remove_repetition_rule else [] + calendar = vobject.iCalendar() tz = pytz.timezone(self.env.user.tz or "UTC") calendar.add(vobject.icalendar.TimezoneComponent(tz)) @@ -260,7 +270,7 @@ def _get_ical_advanced(self, records=None, limit=None): if isinstance(cal, str): cal = vobject.readOne(cal) - self._copy_ical_calendar(calendar, cal) + self._copy_ical_calendar(calendar, cal, to_remove=to_remove) event, todo = map(context.get, ("event", "todo")) if event: @@ -275,9 +285,16 @@ def _dict_to_ical_component(self, component, data): for key, value in data.items(): component.add(key).value = self._format_ical_value(value) - def _copy_ical_calendar(self, dst_calendar, src_calendar): + def _copy_ical_calendar(self, dst_calendar, src_calendar, *, to_remove=None): + """Copy all events and todos from an calendar. Optionally remove children + of the items (like rrule)""" for item in src_calendar.getChildren(): if item.name.lower() in ("vevent", "vtodo"): + if to_remove: + for attr in list(item.getChildren()): + if attr.name.lower() in to_remove: + item.remove(attr) + dst_calendar.add(item) def _format_ical_value(self, value, field=None): diff --git a/base_ical/models/res_users.py b/base_ical/models/res_users.py index e229b8976..f8c9645f1 100644 --- a/base_ical/models/res_users.py +++ b/base_ical/models/res_users.py @@ -22,5 +22,5 @@ def SELF_READABLE_FIELDS(self): def create(self, vals_list): result = super().create(vals_list) calendars = self.env["base.ical"].search([("auto", "=", True)]) - calendars.sudo().write({"allowed_users_ids": [(4, result.id)]}) + calendars.sudo().write({"allowed_users_ids": [(4, r.id) for r in result]}) return result diff --git a/base_ical/views/base_ical.xml b/base_ical/views/base_ical.xml index d717a1116..dfde0216d 100644 --- a/base_ical/views/base_ical.xml +++ b/base_ical/views/base_ical.xml @@ -105,9 +105,11 @@ string="Advanced Configuration" attrs="{'invisible': [('mode', '!=', 'advanced')]}" > +