From 58f6dfe50a761847fb3b8713f8409bfe74ccf8a6 Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Thu, 14 Nov 2013 14:28:37 +0530 Subject: [PATCH] [minor] expand partial links when sending email --- core/doctype/communication/communication.py | 9 +++++---- webnotes/model/bean.py | 16 ++++++++-------- webnotes/model/doc.py | 1 - webnotes/utils/__init__.py | 16 ++++++++++++++++ webnotes/utils/email_lib/smtp.py | 8 ++++---- webnotes/utils/file_manager.py | 2 +- 6 files changed, 34 insertions(+), 18 deletions(-) diff --git a/core/doctype/communication/communication.py b/core/doctype/communication/communication.py index e8124ff013..504e64c367 100644 --- a/core/doctype/communication/communication.py +++ b/core/doctype/communication/communication.py @@ -59,11 +59,12 @@ def make(doctype=None, name=None, content=None, subject=None, sent_or_received = d.communication_medium = communication_medium - if send_email: - send_comm_email(d, name, sent_via, print_html, attachments, send_me_a_copy) - comm.ignore_permissions = True comm.insert() + + if send_email: + d = comm.doc + send_comm_email(d, name, sent_via, print_html, attachments, send_me_a_copy) @webnotes.whitelist() def get_customer_supplier(args=None): @@ -98,7 +99,7 @@ def send_comm_email(d, name, sent_via=None, print_html=None, attachments='[]', s d.content = sent_via.get_content(d) footer = set_portal_link(sent_via, d) - + from webnotes.utils.email_lib.smtp import get_email mail = get_email(d.recipients, sender=d.sender, subject=d.subject, msg=d.content, footer=footer) diff --git a/webnotes/model/bean.py b/webnotes/model/bean.py index 68e804f5d9..abe9730266 100644 --- a/webnotes/model/bean.py +++ b/webnotes/model/bean.py @@ -190,12 +190,17 @@ class Bean: def prepare_for_save(self, method): self.check_if_latest(method) + self.update_timestamps_and_docstatus() + self.update_parent_info() + + if self.doc.fields.get("__islocal"): + # set name before validate + self.doc.set_new_name() + self.run_method('before_insert') + if method != "cancel": self.extract_images_from_text_editor() self.check_links() - - self.update_timestamps_and_docstatus() - self.update_parent_info() def update_parent_info(self): idx_map = {} @@ -286,11 +291,6 @@ class Bean: if self.ignore_permissions or webnotes.has_permission(self.doc.doctype, perm_to_check, self.doc): self.to_docstatus = 0 self.prepare_for_save("save") - if self.doc.fields.get("__islocal"): - # set name before validate - self.doc.set_new_name() - self.run_method('before_insert') - if not self.ignore_validate: self.run_method('validate') if not self.ignore_mandatory: diff --git a/webnotes/model/doc.py b/webnotes/model/doc.py index 3590be9fc2..04dd929e38 100755 --- a/webnotes/model/doc.py +++ b/webnotes/model/doc.py @@ -213,7 +213,6 @@ class Document: return r else: if not webnotes.conn.exists(self.doctype, self.name): - print self.fields webnotes.msgprint(webnotes._("Cannot update a non-exiting record, try inserting.") + ": " + self.doctype + " / " + self.name, raise_exception=1) diff --git a/webnotes/utils/__init__.py b/webnotes/utils/__init__.py index babbfd6ee9..62ad116efa 100644 --- a/webnotes/utils/__init__.py +++ b/webnotes/utils/__init__.py @@ -916,3 +916,19 @@ def get_disk_usage(): return 0 err, out = execute_in_shell("du -hsm {files_path}".format(files_path=files_path)) return cint(out.split("\n")[-2].split("\t")[0]) + +import re +# compiled regex for efficiency of expand_partial_links +# returns a set of tuples of length 4 +link_regex = re.compile('(href|src){1}([\s]*=[\s]*[\'"]?)([^\'" >]+)([\'"]?)') + +def expand_partial_links(html): + from webnotes.utils import get_url + url_tuples = link_regex.findall(html) + for parts in sorted(url_tuples, key=lambda *x: len(x[0][2]), reverse=True): + if all(parts) and not parts[2].startswith("http"): + new_parts = list(parts) + new_parts[2] = get_url(new_parts[2]) + html = html.replace("".join(parts), "".join(new_parts)) + + return html \ No newline at end of file diff --git a/webnotes/utils/email_lib/smtp.py b/webnotes/utils/email_lib/smtp.py index 8be5c668f0..d80dec7caa 100644 --- a/webnotes/utils/email_lib/smtp.py +++ b/webnotes/utils/email_lib/smtp.py @@ -10,7 +10,7 @@ Allows easy adding of Attachments of "File" objects import webnotes from webnotes import conf from webnotes import msgprint -from webnotes.utils import cint +from webnotes.utils import cint, expand_partial_links def get_email(recipients, sender='', msg='', subject='[No Subject]', text_content = None, footer=None): """send an html email as multipart with attachments and all""" @@ -51,13 +51,13 @@ class EMail: self.html_set = False def set_html(self, message, text_content = None, footer=None): - """Attach message in the html portion of multipart/alternative""" message = message + self.get_footer(footer) + message = expand_partial_links(message) # this is the first html part of a multi-part message, # convert to text well - if not self.html_set: + if not self.html_set: if text_content: self.set_text(text_content) else: @@ -65,7 +65,7 @@ class EMail: self.set_part_html(message) self.html_set = True - + def set_text(self, message): """ Attach message in the text portion of multipart/alternative diff --git a/webnotes/utils/file_manager.py b/webnotes/utils/file_manager.py index 0e1bd9eccd..b29513389a 100644 --- a/webnotes/utils/file_manager.py +++ b/webnotes/utils/file_manager.py @@ -79,7 +79,7 @@ def extract_images_from_html(doc, fieldname): content = re.sub('