Skip to content

Commit

Permalink
[FIX] base_ical: Fix problem when users are created and one calendar …
Browse files Browse the repository at this point in the history
…is set to auto. Fix problem with rrule already applied by Odoo with creation of multiple records.
  • Loading branch information
fkantelberg committed Nov 20, 2024
1 parent 165946f commit a652676
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
21 changes: 19 additions & 2 deletions base_ical/models/base_ical.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 ""

Check warning on line 203 in base_ical/models/base_ical.py

View check run for this annotation

Codecov / codecov/patch

base_ical/models/base_ical.py#L203

Added line #L203 was not covered by tests

if self.mode == "simple":
return self._get_ical_simple(records=records, limit=limit)

Expand Down Expand Up @@ -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))
Expand All @@ -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:
Expand All @@ -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)

Check warning on line 296 in base_ical/models/base_ical.py

View check run for this annotation

Codecov / codecov/patch

base_ical/models/base_ical.py#L296

Added line #L296 was not covered by tests

dst_calendar.add(item)

def _format_ical_value(self, value, field=None):
Expand Down
2 changes: 1 addition & 1 deletion base_ical/models/res_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions base_ical/views/base_ical.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,11 @@
string="Advanced Configuration"
attrs="{'invisible': [('mode', '!=', 'advanced')]}"
>
<field name="remove_repetition_rule" />
<field
name="code"
nolabel="1"
colspan="2"
widget="ace"
options="{'mode': 'python'}"
/>
Expand Down

0 comments on commit a652676

Please sign in to comment.