diff --git a/frappe/desk/doctype/event/event.py b/frappe/desk/doctype/event/event.py index 2f650b18fe..6b488af904 100644 --- a/frappe/desk/doctype/event/event.py +++ b/frappe/desk/doctype/event/event.py @@ -20,6 +20,7 @@ from frappe.utils import ( date_diff, format_datetime, get_datetime_str, + get_fullname, getdate, month_diff, now_datetime, @@ -38,6 +39,11 @@ communication_mapping = { "Other": "Other", } +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from frappe.core.doctype.communication.communication import Communication + class Event(Document): # begin: auto-generated types @@ -107,42 +113,49 @@ class Event(Document): def on_trash(self): communications = frappe.get_all( - "Communication", dict(reference_doctype=self.doctype, reference_name=self.name) + "Communication", + filters={"reference_doctype": self.doctype, "reference_name": self.name}, + pluck="name", ) - if communications: - for communication in communications: - frappe.delete_doc_if_exists("Communication", communication.name, force=True) + for communication in communications: + frappe.delete_doc("Communication", communication, force=True) def sync_communication(self): - if self.event_participants: - for participant in self.event_participants: - filters = [ + if not self.event_participants: + return + + for participant in self.event_participants: + if communications := frappe.get_all( + "Communication", + filters=[ ["Communication", "reference_doctype", "=", self.doctype], ["Communication", "reference_name", "=", self.name], ["Communication Link", "link_doctype", "=", participant.reference_doctype], ["Communication Link", "link_name", "=", participant.reference_docname], - ] - if comms := frappe.get_all("Communication", filters=filters, fields=["name"], distinct=True): - for comm in comms: - communication = frappe.get_doc("Communication", comm.name) - self.update_communication(participant, communication) - else: - meta = frappe.get_meta(participant.reference_doctype) - if hasattr(meta, "allow_events_in_timeline") and meta.allow_events_in_timeline == 1: - self.create_communication(participant) + ], + pluck="name", + distinct=True, + ): + for comm in communications: + communication = frappe.get_doc("Communication", comm) + self.update_communication(participant, communication) + else: + meta = frappe.get_meta(participant.reference_doctype) + if hasattr(meta, "allow_events_in_timeline") and meta.allow_events_in_timeline == 1: + self.create_communication(participant) - def create_communication(self, participant): + def create_communication(self, participant: "EventParticipants"): communication = frappe.new_doc("Communication") self.update_communication(participant, communication) self.communication = communication.name - def update_communication(self, participant, communication): + def update_communication(self, participant: "EventParticipants", communication: "Communication"): communication.communication_medium = "Event" communication.subject = self.subject communication.content = self.description if self.description else self.subject communication.communication_date = self.starts_on communication.sender = self.owner - communication.sender_full_name = frappe.utils.get_fullname(self.owner) + communication.sender_full_name = get_fullname(self.owner) communication.reference_doctype = self.doctype communication.reference_name = self.name communication.communication_medium = ( @@ -195,28 +208,24 @@ class Event(Document): @frappe.whitelist() def delete_communication(event, reference_doctype, reference_docname): - deleted_participant = frappe.get_doc(reference_doctype, reference_docname) if isinstance(event, str): event = json.loads(event) - filters = [ - ["Communication", "reference_doctype", "=", event.get("doctype")], - ["Communication", "reference_name", "=", event.get("name")], - ["Communication Link", "link_doctype", "=", deleted_participant.reference_doctype], - ["Communication Link", "link_name", "=", deleted_participant.reference_docname], - ] + deleted_participant = frappe.get_doc(reference_doctype, reference_docname) - comms = frappe.get_list("Communication", filters=filters, fields=["name"]) + comms = frappe.get_list( + "Communication", + filters=[ + ["Communication", "reference_doctype", "=", event.get("doctype")], + ["Communication", "reference_name", "=", event.get("name")], + ["Communication Link", "link_doctype", "=", deleted_participant.reference_doctype], + ["Communication Link", "link_name", "=", deleted_participant.reference_docname], + ], + pluck="name", + ) - if comms: - deletion = [] - for comm in comms: - delete = frappe.get_doc("Communication", comm.name).delete() - deletion.append(delete) - - return deletion - - return {} + for comm in comms: + frappe.delete_doc("Communication", comm) def get_permission_query_conditions(user):