From 05c88fa3f0dfa11c69777aef1ba6acb6105dc6df Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Mon, 14 Aug 2017 12:58:32 +0530 Subject: [PATCH] [minor] make email alert configurable, so we can use it list default print formats --- .../doctype/email_alert/email_alert.json | 4 +- .../email/doctype/email_alert/email_alert.py | 42 ++++++++++++------- .../doctype/email_alert/test_email_alert.js | 23 ++++++++++ frappe/model/sync.py | 2 +- frappe/modules/import_file.py | 5 ++- frappe/public/css/form.css | 9 ++++ frappe/public/js/frappe/form/control.js | 2 +- frappe/public/less/form.less | 10 +++++ 8 files changed, 77 insertions(+), 20 deletions(-) create mode 100644 frappe/email/doctype/email_alert/test_email_alert.js diff --git a/frappe/email/doctype/email_alert/email_alert.json b/frappe/email/doctype/email_alert/email_alert.json index 095f0a9c18..197705b89e 100755 --- a/frappe/email/doctype/email_alert/email_alert.json +++ b/frappe/email/doctype/email_alert/email_alert.json @@ -681,7 +681,7 @@ "collapsible": 0, "columns": 0, "default": "Add your message here", - "depends_on": "eval:!doc.is_standard", + "depends_on": "", "fieldname": "message", "fieldtype": "Code", "hidden": 0, @@ -808,7 +808,7 @@ "istable": 0, "max_attachments": 0, "menu_index": 0, - "modified": "2017-07-07 16:09:48.804218", + "modified": "2017-08-13 22:43:49.079330", "modified_by": "Administrator", "module": "Email", "name": "Email Alert", diff --git a/frappe/email/doctype/email_alert/email_alert.py b/frappe/email/doctype/email_alert/email_alert.py index 8ef2423299..4dfa877856 100755 --- a/frappe/email/doctype/email_alert/email_alert.py +++ b/frappe/email/doctype/email_alert/email_alert.py @@ -7,14 +7,18 @@ import json, os from frappe import _ from frappe.model.document import Document from frappe.core.doctype.role.role import get_emails_from_role -from frappe.utils import validate_email_add, nowdate -from frappe.utils.data import parse_val +from frappe.utils import validate_email_add, nowdate, parse_val, is_html from frappe.utils.jinja import validate_template from frappe.modules.utils import export_module_json, get_doc_module from markdown2 import markdown from six import string_types class EmailAlert(Document): + def onload(self): + '''load message''' + if self.is_standard: + self.message = self.get_template() + def autoname(self): if not self.name: self.name = self.subject @@ -31,6 +35,7 @@ class EmailAlert(Document): self.validate_forbidden_types() self.validate_condition() + self.validate_standard() def on_update(self): frappe.cache().hdel('email_alerts', self.document_type) @@ -53,6 +58,10 @@ def get_context(context): pass """) + def validate_standard(self): + if self.is_standard and not frappe.conf.developer_mode: + frappe.throw(_('Cannot edit Standard Email Alert. To edit, please disable this and duplicate it')) + def validate_condition(self): temp_doc = frappe.new_doc(self.document_type) if self.condition: @@ -165,26 +174,31 @@ def get_context(context): self.property_value, update_modified = False) doc.set(self.set_property_after_alert, self.property_value) + def get_template(self): + module = get_doc_module(self.module, self.doctype, self.name) + def load_template(extn): + template = '' + template_path = os.path.join(os.path.dirname(module.__file__), + frappe.scrub(self.name) + extn) + if os.path.exists(template_path): + with open(template_path, 'r') as f: + template = f.read() + return template + + return load_template('.html') or load_template('.md') + def load_standard_properties(self, context): + '''load templates and run get_context''' module = get_doc_module(self.module, self.doctype, self.name) if module: if hasattr(module, 'get_context'): out = module.get_context(context) if out: context.update(out) - def load_template(extn): - template_path = os.path.join(os.path.dirname(module.__file__), - frappe.scrub(self.name) + extn) - if os.path.exists(template_path): - with open(template_path, 'r') as f: - self.message = f.read() - return True - - # get template - if not load_template('.html'): - if load_template('.md'): - self.message = markdown(self.message) + self.message = self.get_template() + if not is_html(self.message): + self.message = markdown(self.message) @frappe.whitelist() def get_documents_for_today(email_alert): diff --git a/frappe/email/doctype/email_alert/test_email_alert.js b/frappe/email/doctype/email_alert/test_email_alert.js new file mode 100644 index 0000000000..58b0de5f14 --- /dev/null +++ b/frappe/email/doctype/email_alert/test_email_alert.js @@ -0,0 +1,23 @@ +/* eslint-disable */ +// rename this file from _test_[name] to test_[name] to activate +// and remove above this line + +QUnit.test("test: Email Alert", function (assert) { + let done = assert.async(); + + // number of asserts + assert.expect(1); + + frappe.run_serially([ + // insert a new Email Alert + () => frappe.tests.make('Email Alert', [ + // values to be set + {key: 'value'} + ]), + () => { + assert.equal(cur_frm.doc.key, 'value'); + }, + () => done() + ]); + +}); diff --git a/frappe/model/sync.py b/frappe/model/sync.py index c002136321..72e80a9ecc 100644 --- a/frappe/model/sync.py +++ b/frappe/model/sync.py @@ -42,7 +42,7 @@ def sync_for(app_name, force=0, sync_everything = False, verbose=False, reset_pe if l: for i, doc_path in enumerate(files): import_file_by_path(doc_path, force=force, ignore_version=True, - reset_permissions=reset_permissions) + reset_permissions=reset_permissions, for_sync=True) #print module_name + ' | ' + doctype + ' | ' + name frappe.db.commit() diff --git a/frappe/modules/import_file.py b/frappe/modules/import_file.py index 57c671b407..9a2c080d9b 100644 --- a/frappe/modules/import_file.py +++ b/frappe/modules/import_file.py @@ -33,7 +33,7 @@ def get_file_path(module, dt, dn): return path def import_file_by_path(path, force=False, data_import=False, pre_process=None, ignore_version=None, - reset_permissions=False): + reset_permissions=False, for_sync=False): try: docs = read_doc_from_file(path) except IOError: @@ -86,7 +86,8 @@ def read_doc_from_file(path): ignore_values = { "Report": ["disabled"], - "Print Format": ["disabled"] + "Print Format": ["disabled"], + "Email Alert": ["enabled"] } ignore_doctypes = [""] diff --git a/frappe/public/css/form.css b/frappe/public/css/form.css index 2dd5aaa1e2..00674934af 100644 --- a/frappe/public/css/form.css +++ b/frappe/public/css/form.css @@ -588,6 +588,15 @@ select.form-control { .password-strength-message { margin-top: -10px; } +.control-code { + height: 400px; + font-family: Monaco, "Courier New", monospace; + background-color: black; + color: #fffce7; + font-size: 12px; + line-height: 1.7em; + border: none; +} .delivery-status-indicator { display: inline-block; margin-top: -3px; diff --git a/frappe/public/js/frappe/form/control.js b/frappe/public/js/frappe/form/control.js index fa738c8190..70e6c8216c 100755 --- a/frappe/public/js/frappe/form/control.js +++ b/frappe/public/js/frappe/form/control.js @@ -1802,7 +1802,7 @@ frappe.ui.form.ControlCode = frappe.ui.form.ControlText.extend({ this._super(); $(this.input_area).find("textarea") .allowTabs() - .css({"height":"400px", "font-family": "Monaco, \"Courier New\", monospace"}); + .addClass('control-code'); } }); diff --git a/frappe/public/less/form.less b/frappe/public/less/form.less index 3440d6f4b5..e9c80cb84c 100644 --- a/frappe/public/less/form.less +++ b/frappe/public/less/form.less @@ -735,6 +735,16 @@ select.form-control { margin-top: -10px; } +.control-code { + height: 400px; + font-family: Monaco, "Courier New", monospace; + background-color: black; + color: @light-yellow; + font-size: 12px; + line-height: 1.7em; + border: none; +} + .delivery-status-indicator { display: inline-block; margin-top: -3px;