Browse Source

[FIX] update calendar event info

16.0
RemiFr82 3 months ago
parent
commit
01b63e553b
  1. 117
      event_to_calendar/models/event_event.py

117
event_to_calendar/models/event_event.py

@ -29,48 +29,51 @@ class EventEvent(models.Model):
for event in self: for event in self:
event.in_user_agenda = uid in event.meeting_ids.mapped("user_id").ids 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": _("<p>Organizer: {}{}<br/>Note:\n</p>").format(
self.organizer_id.display_name,
"".join(
[
"<br/>{} : {}".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): def add_to_agenda(self, user_id=False):
vals_list = [] vals_list = []
self_model_id = self.env["ir.model"]._get_id(self._name)
uid = user_id or self.env.uid uid = user_id or self.env.uid
to_add = self.filtered( to_add = self.filtered(
lambda ee: uid not in ee.meeting_ids.mapped("user_id.id") lambda ee: uid not in ee.meeting_ids.mapped("user_id.id")
) )
for event in to_add: 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": _("<p>Organizer: {}{}<br/>Note:\n</p>").format(
event.organizer_id.display_name,
"".join(
[
"<br/>{} : {}".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: if vals_list:
self.env["calendar.event"].create(vals_list) self.env["calendar.event"].create(vals_list)
@ -99,23 +102,37 @@ class EventEvent(models.Model):
} }
@api.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): def write(self, vals):
res = super().write(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") 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 return res
Loading…
Cancel
Save