diff --git a/frappe/email/doctype/email_alert/email_alert.py b/frappe/email/doctype/email_alert/email_alert.py index 666ae25dbc..f9212cfe88 100755 --- a/frappe/email/doctype/email_alert/email_alert.py +++ b/frappe/email/doctype/email_alert/email_alert.py @@ -217,7 +217,14 @@ def evaluate_alert(doc, alert, event): return if event=="Value Change" and not doc.is_new(): - db_value = frappe.db.get_value(doc.doctype, doc.name, alert.value_changed) + try: + db_value = frappe.db.get_value(doc.doctype, doc.name, alert.value_changed) + except frappe.DatabaseOperationalError as e: + if e.args[0]==1054: + alert.db_set('enabled', 0) + frappe.log_error('Email Alert {0} has been disabled due to missing field'.format(alert.name)) + return + db_value = parse_val(db_value) if (doc.get(alert.value_changed) == db_value) or \ (not db_value and not doc.get(alert.value_changed)): diff --git a/frappe/email/doctype/email_alert/test_email_alert.py b/frappe/email/doctype/email_alert/test_email_alert.py index 57686dd984..1f7c457ae2 100755 --- a/frappe/email/doctype/email_alert/test_email_alert.py +++ b/frappe/email/doctype/email_alert/test_email_alert.py @@ -87,6 +87,36 @@ class TestEmailAlert(unittest.TestCase): self.assertTrue(frappe.db.get_value("Email Queue", {"reference_doctype": "Event", "reference_name": event.name, "status":"Not Sent"})) + def test_alert_disabled_on_wrong_field(self): + frappe.set_user('Administrator') + email_alert = frappe.get_doc({ + "doctype": "Email Alert", + "subject":"_Test Email Alert for wrong field", + "document_type": "Event", + "event": "Value Change", + "attach_print": 0, + "value_changed": "description1", + "message": "Description changed", + "recipients": [ + { "email_by_document_field": "owner" } + ] + }).insert() + + event = frappe.new_doc("Event") + event.subject = "test-2", + event.event_type = "Private" + event.starts_on = "2014-06-06 12:00:00" + event.insert() + event.subject = "test 1" + event.save() + + # verify that email_alert is disabled + email_alert.reload() + self.assertEqual(email_alert.enabled, 0) + email_alert.delete() + event.delete() + + def test_date_changed(self): event = frappe.new_doc("Event") event.subject = "test", diff --git a/frappe/exceptions.py b/frappe/exceptions.py index 1a5f6d688e..723c602496 100644 --- a/frappe/exceptions.py +++ b/frappe/exceptions.py @@ -7,6 +7,7 @@ from __future__ import unicode_literals from werkzeug.exceptions import NotFound from MySQLdb import ProgrammingError as SQLError, Error +from MySQLdb import OperationalError as DatabaseOperationalError class ValidationError(Exception):