From 6a4ee6f71b4ddfc9f3b72a3e4b50b4d9482b0c3a Mon Sep 17 00:00:00 2001 From: nilpatel42 Date: Fri, 16 Aug 2024 17:05:54 +0530 Subject: [PATCH 1/9] add get alerts for today button --- .../whatsapp_notification.js | 24 +++++++++++++++++++ .../whatsapp_notification.py | 17 +++++++++++++ frappe_whatsapp/hooks.py | 6 ++--- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.js b/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.js index d73ad38..fc6fba8 100644 --- a/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.js +++ b/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.js @@ -107,3 +107,27 @@ frappe.ui.form.on('WhatsApp Notification', { frappe.notification.setup_fieldname_select(frm); }, }); + + +frappe.ui.form.on('WhatsApp Notification', { + refresh: function (frm) { + frm.add_custom_button(__('Get Alerts for Today'), function () { + frappe.call({ + method: 'frappe_whatsapp.frappe_whatsapp.doctype.whatsapp_notification.custom_api.call_trigger_notifications', + args: { + method: 'daily' // You can change this as needed + }, + callback: function (response) { + // Handle the response as needed + if (response.message) { + console.log(response.message); + } + }, + error: function (error) { + console.error('Error:', error); + frappe.msgprint(__('Failed to trigger notifications')); + } + }); + }); + } +}); \ No newline at end of file diff --git a/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.py b/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.py index 0200687..cdd2fd1 100644 --- a/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.py +++ b/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.py @@ -311,3 +311,20 @@ def template(): doc.mobile_no = "+919741094468" notification.send_template_message(doc) + + +@frappe.whitelist() +def call_trigger_notifications(): + """Trigger notifications.""" + try: + # Assuming trigger_notifications is defined elsewhere in the file + if 'trigger_notifications' in globals(): + trigger_notifications() # Call the function + else: + frappe.throw("trigger_notifications function is not defined.") + except Exception as e: + # Log the error but do not show any popup or alert + frappe.log_error(frappe.get_traceback(), "Error in call_trigger_notifications") + # Optionally, you could raise the exception to be handled elsewhere if needed + raise e + diff --git a/frappe_whatsapp/hooks.py b/frappe_whatsapp/hooks.py index b3d4116..8ab1dd1 100644 --- a/frappe_whatsapp/hooks.py +++ b/frappe_whatsapp/hooks.py @@ -136,9 +136,9 @@ # Overriding Methods # ------------------------------ # -# override_whitelisted_methods = { -# "frappe.desk.doctype.event.event.get_events": "frappe_whatsapp.event.get_events" -# } +override_whitelisted_methods = { + "frappe_whatsapp.frappe_whatsapp.doctype.whatsapp_notification.custom_api.call_trigger_notifications": "frappe_whatsapp.frappe_whatsapp.doctype.whatsapp_notification.whatsapp_notification.call_trigger_notifications" +} # # each overriding function accepts a `data` argument; # generated from the base implementation of the doctype dashboard, From f6ef35ca691ebfc826210810af36e654e0397e00 Mon Sep 17 00:00:00 2001 From: nilpatel42 Date: Fri, 16 Aug 2024 17:15:28 +0530 Subject: [PATCH 2/9] Message for No alerts --- .../whatsapp_notification/whatsapp_notification.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.js b/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.js index fc6fba8..c4bec38 100644 --- a/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.js +++ b/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.js @@ -119,8 +119,12 @@ frappe.ui.form.on('WhatsApp Notification', { }, callback: function (response) { // Handle the response as needed - if (response.message) { + if (response.message && response.message.length > 0) { console.log(response.message); + // Handle the alerts for today + } else { + // No alerts for today + frappe.msgprint(__('No alerts for today')); } }, error: function (error) { @@ -130,4 +134,4 @@ frappe.ui.form.on('WhatsApp Notification', { }); }); } -}); \ No newline at end of file +}); From 942a198054f39d17458b719aad06cf61d0910d95 Mon Sep 17 00:00:00 2001 From: nilpatel42 Date: Fri, 16 Aug 2024 17:16:36 +0530 Subject: [PATCH 3/9] remove override method --- frappe_whatsapp/hooks.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/frappe_whatsapp/hooks.py b/frappe_whatsapp/hooks.py index 8ab1dd1..b3d4116 100644 --- a/frappe_whatsapp/hooks.py +++ b/frappe_whatsapp/hooks.py @@ -136,9 +136,9 @@ # Overriding Methods # ------------------------------ # -override_whitelisted_methods = { - "frappe_whatsapp.frappe_whatsapp.doctype.whatsapp_notification.custom_api.call_trigger_notifications": "frappe_whatsapp.frappe_whatsapp.doctype.whatsapp_notification.whatsapp_notification.call_trigger_notifications" -} +# override_whitelisted_methods = { +# "frappe.desk.doctype.event.event.get_events": "frappe_whatsapp.event.get_events" +# } # # each overriding function accepts a `data` argument; # generated from the base implementation of the doctype dashboard, From d6b4d45883bee98e7675f0ffddb912bacea528bd Mon Sep 17 00:00:00 2001 From: nilpatel42 Date: Fri, 16 Aug 2024 17:50:57 +0530 Subject: [PATCH 4/9] sent alert using trigger_notifications funtion --- .../whatsapp_notification.py | 39 +++++++------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.py b/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.py index cdd2fd1..2a38f1e 100644 --- a/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.py +++ b/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.py @@ -285,6 +285,18 @@ def get_documents_for_today(self): # print(doc.name) +@frappe.whitelist() +def call_trigger_notifications(): + """Trigger notifications.""" + try: + # Directly call the trigger_notifications function + trigger_notifications() + except Exception as e: + # Log the error but do not show any popup or alert + frappe.log_error(frappe.get_traceback(), "Error in call_trigger_notifications") + # Optionally, you could raise the exception to be handled elsewhere if needed + raise e + def trigger_notifications(method="daily"): if frappe.flags.in_import or frappe.flags.in_patch: # don't send notifications while syncing or patching @@ -296,13 +308,10 @@ def trigger_notifications(method="daily"): ) for d in doc_list: alert = frappe.get_doc("WhatsApp Notification", d.name) - alert.get_documents_for_today() - # doc.name - # evaluate_alert(doc, alert, alert.event) - # frappe.db.commit() - - + # doc.name + # evaluate_alert(doc, alert, alert.event) + # frappe.db.commit() def template(): notification = frappe.get_doc("WhatsApp Notification", "WN-0001") @@ -310,21 +319,3 @@ def template(): doc = frappe.get_doc("User", "Administrator") doc.mobile_no = "+919741094468" notification.send_template_message(doc) - - - -@frappe.whitelist() -def call_trigger_notifications(): - """Trigger notifications.""" - try: - # Assuming trigger_notifications is defined elsewhere in the file - if 'trigger_notifications' in globals(): - trigger_notifications() # Call the function - else: - frappe.throw("trigger_notifications function is not defined.") - except Exception as e: - # Log the error but do not show any popup or alert - frappe.log_error(frappe.get_traceback(), "Error in call_trigger_notifications") - # Optionally, you could raise the exception to be handled elsewhere if needed - raise e - From 7f9cf6ea0c55028e76a61de408a0b63e2aea7f4f Mon Sep 17 00:00:00 2001 From: nilpatel42 Date: Fri, 16 Aug 2024 17:51:27 +0530 Subject: [PATCH 5/9] MInor Changes --- .../whatsapp_notification/whatsapp_notification.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.js b/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.js index c4bec38..12e19d6 100644 --- a/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.js +++ b/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.js @@ -109,21 +109,19 @@ frappe.ui.form.on('WhatsApp Notification', { }); + frappe.ui.form.on('WhatsApp Notification', { refresh: function (frm) { frm.add_custom_button(__('Get Alerts for Today'), function () { frappe.call({ - method: 'frappe_whatsapp.frappe_whatsapp.doctype.whatsapp_notification.custom_api.call_trigger_notifications', + method: 'frappe_whatsapp.frappe_whatsapp.doctype.whatsapp_notification.whatsapp_notification.call_trigger_notifications', args: { - method: 'daily' // You can change this as needed + method: 'daily' }, callback: function (response) { - // Handle the response as needed if (response.message && response.message.length > 0) { console.log(response.message); - // Handle the alerts for today } else { - // No alerts for today frappe.msgprint(__('No alerts for today')); } }, @@ -135,3 +133,8 @@ frappe.ui.form.on('WhatsApp Notification', { }); } }); + + + + + From 81c4d176de94602c7feac23e702c975fdf4ceaaf Mon Sep 17 00:00:00 2001 From: nilpatel42 Date: Mon, 19 Aug 2024 16:03:01 +0530 Subject: [PATCH 6/9] Remove Console Log and Specified Lines --- .../whatsapp_notification/whatsapp_notification.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.py b/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.py index 3635cb1..c71e5cc 100644 --- a/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.py +++ b/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.py @@ -313,13 +313,4 @@ def trigger_notifications(method="daily"): for d in doc_list: alert = frappe.get_doc("WhatsApp Notification", d.name) alert.get_documents_for_today() - # doc.name - # evaluate_alert(doc, alert, alert.event) - # frappe.db.commit() - -def template(): - notification = frappe.get_doc("WhatsApp Notification", "WN-0001") - notification.disabled = 0 - doc = frappe.get_doc("User", "Administrator") - doc.mobile_no = "+919741094468" From 10787ffb01aa5e1e0a54ad0b4b84332bcfa0e3df Mon Sep 17 00:00:00 2001 From: nilpatel42 Date: Mon, 19 Aug 2024 16:07:45 +0530 Subject: [PATCH 7/9] Remove all Console Log and Error --- .../whatsapp_notification.py | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.py b/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.py index c71e5cc..d3423b1 100644 --- a/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.py +++ b/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.py @@ -1,5 +1,3 @@ -"""Notification.""" - import json import frappe from frappe.model.document import Document @@ -21,7 +19,7 @@ def validate(self): filters={"dt": self.reference_doctype}, fields=["fieldname"] ) - if not any(field.fieldname == self.field_name for field in fields): # noqa + if not any(field.fieldname == self.field_name for field in fields): frappe.throw(f"Field name {self.field_name} does not exists") if self.custom_attachment: if not self.attach and not self.attach_from_field: @@ -56,7 +54,6 @@ def send_scheduled_message(self) -> dict: } self.content_type = template.get("header_type", "text").lower() self.notify(data) - # return _globals.frappe.flags def send_template_message(self, doc: Document): """Specific to Document Event triggered Server Scripts.""" @@ -108,8 +105,7 @@ def send_template_message(self, doc: Document): }] if self.attach_document_print: - # frappe.db.begin() - key = doc.get_document_share_key() # noqa + key = doc.get_document_share_key() frappe.db.commit() print_format = "Standard" doctype = frappe.get_doc("DocType", doc_data['doctype']) @@ -141,7 +137,6 @@ def send_template_message(self, doc: Document): if self.attach_from_field: file_url = doc_data[self.attach_from_field] if not file_url.startswith("http"): - # get share key so that private files can be sent key = doc.get_document_share_key() file_url = f'{frappe.utils.get_url()}{file_url}&key={key}' else: @@ -243,7 +238,7 @@ def on_trash(self): def after_insert(self): """After insert hook.""" if self.notification_type == "Scheduler Event": - method = f"frappe_whatsapp.utils.trigger_whatsapp_notifications_{self.event_frequency.lower().replace(' ', '_')}" # noqa + method = f"frappe_whatsapp.utils.trigger_whatsapp_notifications_{self.event_frequency.lower().replace(' ', '_')}" job = frappe.get_doc( { "doctype": "Scheduled Job Type", @@ -261,7 +256,6 @@ def format_number(self, number): return number - def get_documents_for_today(self): """get list of documents that will be triggered today""" docs = [] @@ -286,24 +280,19 @@ def get_documents_for_today(self): for d in doc_list: doc = frappe.get_doc(self.reference_doctype, d.name) self.send_template_message(doc) - # print(doc.name) @frappe.whitelist() def call_trigger_notifications(): """Trigger notifications.""" try: - # Directly call the trigger_notifications function trigger_notifications() except Exception as e: - # Log the error but do not show any popup or alert frappe.log_error(frappe.get_traceback(), "Error in call_trigger_notifications") - # Optionally, you could raise the exception to be handled elsewhere if needed raise e def trigger_notifications(method="daily"): if frappe.flags.in_import or frappe.flags.in_patch: - # don't send notifications while syncing or patching return if method == "daily": @@ -313,4 +302,3 @@ def trigger_notifications(method="daily"): for d in doc_list: alert = frappe.get_doc("WhatsApp Notification", d.name) alert.get_documents_for_today() - From ee31e73f1e766bcab0089657de15338ad056eb69 Mon Sep 17 00:00:00 2001 From: nilpatel42 Date: Mon, 19 Aug 2024 19:12:30 +0530 Subject: [PATCH 8/9] Line Revert Commets Line --- .../whatsapp_notification.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.py b/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.py index d3423b1..5caa3a7 100644 --- a/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.py +++ b/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.py @@ -1,3 +1,5 @@ +"""Notification.""" + import json import frappe from frappe.model.document import Document @@ -19,7 +21,7 @@ def validate(self): filters={"dt": self.reference_doctype}, fields=["fieldname"] ) - if not any(field.fieldname == self.field_name for field in fields): + if not any(field.fieldname == self.field_name for field in fields): # noqa frappe.throw(f"Field name {self.field_name} does not exists") if self.custom_attachment: if not self.attach and not self.attach_from_field: @@ -54,6 +56,7 @@ def send_scheduled_message(self) -> dict: } self.content_type = template.get("header_type", "text").lower() self.notify(data) + # return _globals.frappe.flags def send_template_message(self, doc: Document): """Specific to Document Event triggered Server Scripts.""" @@ -105,7 +108,8 @@ def send_template_message(self, doc: Document): }] if self.attach_document_print: - key = doc.get_document_share_key() + # frappe.db.begin() + key = doc.get_document_share_key() # noqa frappe.db.commit() print_format = "Standard" doctype = frappe.get_doc("DocType", doc_data['doctype']) @@ -137,6 +141,7 @@ def send_template_message(self, doc: Document): if self.attach_from_field: file_url = doc_data[self.attach_from_field] if not file_url.startswith("http"): + # get share key so that private files can be sent key = doc.get_document_share_key() file_url = f'{frappe.utils.get_url()}{file_url}&key={key}' else: @@ -238,7 +243,7 @@ def on_trash(self): def after_insert(self): """After insert hook.""" if self.notification_type == "Scheduler Event": - method = f"frappe_whatsapp.utils.trigger_whatsapp_notifications_{self.event_frequency.lower().replace(' ', '_')}" + method = f"frappe_whatsapp.utils.trigger_whatsapp_notifications_{self.event_frequency.lower().replace(' ', '_')}" # noqa job = frappe.get_doc( { "doctype": "Scheduled Job Type", @@ -256,6 +261,7 @@ def format_number(self, number): return number + def get_documents_for_today(self): """get list of documents that will be triggered today""" docs = [] @@ -280,19 +286,24 @@ def get_documents_for_today(self): for d in doc_list: doc = frappe.get_doc(self.reference_doctype, d.name) self.send_template_message(doc) + # print(doc.name) @frappe.whitelist() def call_trigger_notifications(): """Trigger notifications.""" try: + # Directly call the trigger_notifications function trigger_notifications() except Exception as e: + # Log the error but do not show any popup or alert frappe.log_error(frappe.get_traceback(), "Error in call_trigger_notifications") + # Optionally, you could raise the exception to be handled elsewhere if needed raise e def trigger_notifications(method="daily"): if frappe.flags.in_import or frappe.flags.in_patch: + # don't send notifications while syncing or patching return if method == "daily": @@ -302,3 +313,4 @@ def trigger_notifications(method="daily"): for d in doc_list: alert = frappe.get_doc("WhatsApp Notification", d.name) alert.get_documents_for_today() + \ No newline at end of file From a504d4e99347960f2c941dd6dc748df6f9a06c0b Mon Sep 17 00:00:00 2001 From: nilpatel42 Date: Mon, 19 Aug 2024 19:29:43 +0530 Subject: [PATCH 9/9] Remove Console Log --- .../doctype/whatsapp_notification/whatsapp_notification.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.js b/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.js index 12e19d6..a8ae7c5 100644 --- a/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.js +++ b/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.js @@ -120,13 +120,11 @@ frappe.ui.form.on('WhatsApp Notification', { }, callback: function (response) { if (response.message && response.message.length > 0) { - console.log(response.message); } else { frappe.msgprint(__('No alerts for today')); } }, error: function (error) { - console.error('Error:', error); frappe.msgprint(__('Failed to trigger notifications')); } });