'Look into the status of Email Queue linked to this Communication and set the Delivery Status of this Communication'

def set_delivery_status(self, commit=False):

    delivery_status = None
    status_counts = Counter(frappe.db.sql_list(u'select    status    from    `tabEmail    Queue`    where    communication=%s', self.name))
    if (self.sent_or_received == u'Received'):
        return
    if (status_counts.get(u'Not    Sent') or status_counts.get(u'Sending')):
        delivery_status = u'Sending'
    elif status_counts.get(u'Error'):
        delivery_status = u'Error'
    elif status_counts.get(u'Expired'):
        delivery_status = u'Expired'
    elif status_counts.get(u'Sent'):
        delivery_status = u'Sent'
    if delivery_status:
        self.db_set(u'delivery_status', delivery_status)
        frappe.publish_realtime(u'update_communication', self.as_dict(), doctype=self.reference_doctype, docname=self.reference_name, after_commit=True)
        self.notify_update()
        if commit:
            frappe.db.commit()
