From 7dd7944590d4e3b021a6e83982904ef012e111a0 Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Tue, 9 Sep 2014 12:26:35 +0530 Subject: [PATCH] [refactor] frappe.utils.email_lib > frappe.email --- frappe/__init__.py | 10 ++--- .../doctype/communication/communication.py | 4 +- frappe/core/doctype/event/event.py | 2 +- frappe/email/__init__.py | 40 +++++++++++++++++++ frappe/{utils/email_lib => email}/bulk.py | 8 ++-- .../outgoing_email_settings.py | 2 +- .../{utils/email_lib => email}/email_body.py | 2 +- .../{utils/email_lib => email}/html2text.py | 0 frappe/{utils/email_lib => email}/receive.py | 0 frappe/{utils/email_lib => email}/smtp.py | 0 frappe/hooks.py | 4 +- frappe/templates/includes/comments.py | 2 +- frappe/templates/pages/contact.py | 2 +- frappe/templates/website_group/settings.py | 2 +- frappe/tests/test_email.py | 10 ++--- frappe/utils/backups.py | 2 +- frappe/utils/email_lib/__init__.py | 40 ------------------- frappe/website/doctype/post/post.py | 4 +- 18 files changed, 67 insertions(+), 67 deletions(-) rename frappe/{utils/email_lib => email}/bulk.py (95%) rename frappe/{utils/email_lib => email}/email_body.py (99%) rename frappe/{utils/email_lib => email}/html2text.py (100%) rename frappe/{utils/email_lib => email}/receive.py (100%) rename frappe/{utils/email_lib => email}/smtp.py (100%) delete mode 100644 frappe/utils/email_lib/__init__.py diff --git a/frappe/__init__.py b/frappe/__init__.py index 88bfbb7ddc..9ee6f0124d 100644 --- a/frappe/__init__.py +++ b/frappe/__init__.py @@ -228,18 +228,18 @@ def sendmail(recipients=(), sender="", subject="No Subject", message="No Message add_unsubscribe_link=False, attachments=None): if bulk: - import frappe.utils.email_lib.bulk - frappe.utils.email_lib.bulk.send(recipients=recipients, sender=sender, + import frappe.email.bulk + frappe.email.bulk.send(recipients=recipients, sender=sender, subject=subject, message=message, ref_doctype = ref_doctype, ref_docname = ref_docname, add_unsubscribe_link=add_unsubscribe_link, attachments=attachments) else: - import frappe.utils.email_lib + import frappe.email if as_markdown: - frappe.utils.email_lib.sendmail_md(recipients, sender=sender, + frappe.email.sendmail_md(recipients, sender=sender, subject=subject, msg=message, attachments=attachments) else: - frappe.utils.email_lib.sendmail(recipients, sender=sender, + frappe.email.sendmail(recipients, sender=sender, subject=subject, msg=message, attachments=attachments) logger = None diff --git a/frappe/core/doctype/communication/communication.py b/frappe/core/doctype/communication/communication.py index 47546c221b..511f95592c 100644 --- a/frappe/core/doctype/communication/communication.py +++ b/frappe/core/doctype/communication/communication.py @@ -8,8 +8,8 @@ import urllib from email.utils import formataddr from frappe.website.utils import is_signup_enabled from frappe.utils import get_url, cstr -from frappe.utils.email_lib.email_body import get_email -from frappe.utils.email_lib.smtp import send +from frappe.email.email_body import get_email +from frappe.email.smtp import send from frappe.utils import scrub_urls, cint from frappe import _ diff --git a/frappe/core/doctype/event/event.py b/frappe/core/doctype/event/event.py index b154256c23..590d441051 100644 --- a/frappe/core/doctype/event/event.py +++ b/frappe/core/doctype/event/event.py @@ -62,7 +62,7 @@ def send_event_digest(): text += '

'\ + frappe._("Daily Event Digest is sent for Calendar Events where reminders are set.")+'

' - from frappe.utils.email_lib import sendmail + from frappe.email import sendmail sendmail(recipients=user.email, subject=frappe._("Upcoming Events for Today"), msg = text) diff --git a/frappe/email/__init__.py b/frappe/email/__init__.py index e69de29bb2..c77b420eeb 100644 --- a/frappe/email/__init__.py +++ b/frappe/email/__init__.py @@ -0,0 +1,40 @@ +# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors +# MIT License. See license.txt + +from __future__ import unicode_literals +import frappe + +from frappe.email.email_body import get_email +from frappe.email.smtp import send + +def sendmail_md(recipients, sender=None, msg=None, subject=None, attachments=None): + """send markdown email""" + import markdown2 + sendmail(recipients, sender, markdown2.markdown(msg), subject, attachments) + +def sendmail(recipients, sender='', msg='', subject='[No Subject]', attachments=None): + """send an html email as multipart with attachments and all""" + send(get_email(recipients, sender, msg, subject, attachments=attachments)) + +def sendmail_to_system_managers(subject, content): + send(get_email(get_system_managers(), None, content, subject)) + +@frappe.whitelist() +def get_contact_list(): + """Returns contacts (from autosuggest)""" + cond = ['`%s` like "%s%%"' % (f, + frappe.form_dict.get('txt')) for f in frappe.form_dict.get('where').split(',')] + cl = frappe.db.sql("select `%s` from `tab%s` where %s" % ( + frappe.form_dict.get('select') + ,frappe.form_dict.get('from') + ,' OR '.join(cond) + ) + ) + frappe.response['cl'] = filter(None, [c[0] for c in cl]) + +def get_system_managers(): + return frappe.db.sql_list("""select parent FROM tabUserRole + WHERE role='System Manager' + AND parent!='Administrator' + AND parent IN + (SELECT email FROM tabUser WHERE enabled=1)""") diff --git a/frappe/utils/email_lib/bulk.py b/frappe/email/bulk.py similarity index 95% rename from frappe/utils/email_lib/bulk.py rename to frappe/email/bulk.py index 8cbdab6f9a..1b5b970ea9 100644 --- a/frappe/utils/email_lib/bulk.py +++ b/frappe/email/bulk.py @@ -6,9 +6,9 @@ import frappe import HTMLParser import urllib from frappe import msgprint, throw, _ -from frappe.utils.email_lib.smtp import SMTPServer -from frappe.utils.email_lib.email_body import get_email, get_formatted_html -from frappe.utils.email_lib.html2text import html2text +from frappe.email.smtp import SMTPServer +from frappe.email.email_body import get_email, get_formatted_html +from frappe.email.html2text import html2text from frappe.utils import cint, get_url, nowdate class BulkLimitCrossedError(frappe.ValidationError): pass @@ -45,7 +45,7 @@ def send(recipients=None, sender=None, doctype='User', email_field='email', Unsubscribe from this list.""" % (get_url(), urllib.urlencode({ - "cmd": "frappe.utils.email_lib.bulk.unsubscribe", + "cmd": "frappe.email.bulk.unsubscribe", "email": doc.get(email_field), "type": doctype, "email_field": email_field diff --git a/frappe/email/doctype/outgoing_email_settings/outgoing_email_settings.py b/frappe/email/doctype/outgoing_email_settings/outgoing_email_settings.py index dd61262559..fda44b6154 100644 --- a/frappe/email/doctype/outgoing_email_settings/outgoing_email_settings.py +++ b/frappe/email/doctype/outgoing_email_settings/outgoing_email_settings.py @@ -17,7 +17,7 @@ class OutgoingEmailSettings(Document): if self.mail_server and not frappe.local.flags.in_patch: from frappe.utils import cint - from frappe.utils.email_lib.smtp import SMTPServer + from frappe.email.smtp import SMTPServer smtpserver = SMTPServer(login = self.mail_login, password = self.mail_password, server = self.mail_server, diff --git a/frappe/utils/email_lib/email_body.py b/frappe/email/email_body.py similarity index 99% rename from frappe/utils/email_lib/email_body.py rename to frappe/email/email_body.py index 9951623da0..6faca96f0e 100644 --- a/frappe/utils/email_lib/email_body.py +++ b/frappe/email/email_body.py @@ -86,7 +86,7 @@ class EMail: def set_html_as_text(self, html): """return html2text""" import HTMLParser - from frappe.utils.email_lib.html2text import html2text + from frappe.email.html2text import html2text try: self.set_text(html2text(html)) except HTMLParser.HTMLParseError: diff --git a/frappe/utils/email_lib/html2text.py b/frappe/email/html2text.py similarity index 100% rename from frappe/utils/email_lib/html2text.py rename to frappe/email/html2text.py diff --git a/frappe/utils/email_lib/receive.py b/frappe/email/receive.py similarity index 100% rename from frappe/utils/email_lib/receive.py rename to frappe/email/receive.py diff --git a/frappe/utils/email_lib/smtp.py b/frappe/email/smtp.py similarity index 100% rename from frappe/utils/email_lib/smtp.py rename to frappe/email/smtp.py diff --git a/frappe/hooks.py b/frappe/hooks.py index cb6e2ca793..2497101d03 100644 --- a/frappe/hooks.py +++ b/frappe/hooks.py @@ -71,9 +71,9 @@ doc_events = { } scheduler_events = { - "all": ["frappe.utils.email_lib.bulk.flush"], + "all": ["frappe.email.bulk.flush"], "daily": [ - "frappe.utils.email_lib.bulk.clear_outbox", + "frappe.email.bulk.clear_outbox", "frappe.core.doctype.notification_count.notification_count.clear_notifications", "frappe.core.doctype.event.event.send_event_digest", "frappe.sessions.clear_expired_sessions", diff --git a/frappe/templates/includes/comments.py b/frappe/templates/includes/comments.py index 6fe8840b33..2b7e1e13e2 100644 --- a/frappe/templates/includes/comments.py +++ b/frappe/templates/includes/comments.py @@ -46,7 +46,7 @@ def add_comment(args=None): recipients = list(set(commentors if owner=="Administrator" else (commentors + [owner]))) - from frappe.utils.email_lib.bulk import send + from frappe.email.bulk import send send(recipients=recipients, doctype='Comment', email_field='comment_by', diff --git a/frappe/templates/pages/contact.py b/frappe/templates/pages/contact.py index 29180a2d6c..c4ed69db05 100644 --- a/frappe/templates/pages/contact.py +++ b/frappe/templates/pages/contact.py @@ -47,7 +47,7 @@ def send_message(subject="Website Query", message="", sender=""): # send email forward_to_email = frappe.db.get_value("Contact Us Settings", None, "forward_to_email") if forward_to_email: - from frappe.utils.email_lib import sendmail + from frappe.email import sendmail sendmail(forward_to_email, sender, message, subject) return "okay" diff --git a/frappe/templates/website_group/settings.py b/frappe/templates/website_group/settings.py index 8c8a649fa0..8073c40e9f 100644 --- a/frappe/templates/website_group/settings.py +++ b/frappe/templates/website_group/settings.py @@ -4,7 +4,7 @@ from __future__ import unicode_literals import frappe from frappe.website.permissions import get_access -from frappe.utils.email_lib.bulk import send +from frappe.email.bulk import send @frappe.whitelist() def suggest_user(term, group): diff --git a/frappe/tests/test_email.py b/frappe/tests/test_email.py index a658848f0f..b23478ee09 100644 --- a/frappe/tests/test_email.py +++ b/frappe/tests/test_email.py @@ -15,11 +15,11 @@ class TestEmail(unittest.TestCase): frappe.db.sql("""delete from `tabBulk Email`""") def test_send(self): - from frappe.utils.email_lib import sendmail + from frappe.email import sendmail #sendmail('test@example.com', subject='Test Mail', msg="Test Content") def test_bulk(self): - from frappe.utils.email_lib.bulk import send + from frappe.email.bulk import send send(recipients = ['test@example.com', 'test1@example.com'], sender="admin@example.com", doctype='User', email_field='email', @@ -33,7 +33,7 @@ class TestEmail(unittest.TestCase): def test_flush(self): self.test_bulk() - from frappe.utils.email_lib.bulk import flush + from frappe.email.bulk import flush flush(from_test=True) bulk = frappe.db.sql("""select * from `tabBulk Email` where status='Sent'""", as_dict=1) self.assertEquals(len(bulk), 2) @@ -41,7 +41,7 @@ class TestEmail(unittest.TestCase): self.assertTrue('test1@example.com' in [d['recipient'] for d in bulk]) def test_unsubscribe(self): - from frappe.utils.email_lib.bulk import unsubscribe, send + from frappe.email.bulk import unsubscribe, send frappe.local.form_dict = frappe._dict({ 'email':'test@example.com', 'type':'User', @@ -63,7 +63,7 @@ class TestEmail(unittest.TestCase): self.assertTrue('Unsubscribe' in bulk[0]['message']) def test_bulk_limit(self): - from frappe.utils.email_lib.bulk import unsubscribe, send, BulkLimitCrossedError + from frappe.email.bulk import unsubscribe, send, BulkLimitCrossedError self.assertRaises(BulkLimitCrossedError, send, recipients=['test@example.com']*1000, sender="admin@example.com", diff --git a/frappe/utils/backups.py b/frappe/utils/backups.py index 066abc3620..a01508f058 100644 --- a/frappe/utils/backups.py +++ b/frappe/utils/backups.py @@ -95,7 +95,7 @@ class BackupGenerator: """ Sends the link to backup file located at erpnext/backups """ - from frappe.utils.email_lib import sendmail, get_system_managers + from frappe.email import sendmail, get_system_managers recipient_list = get_system_managers() db_backup_url = get_url(os.path.join('backups', os.path.basename(self.backup_path_db))) diff --git a/frappe/utils/email_lib/__init__.py b/frappe/utils/email_lib/__init__.py deleted file mode 100644 index 57b14bb874..0000000000 --- a/frappe/utils/email_lib/__init__.py +++ /dev/null @@ -1,40 +0,0 @@ -# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors -# MIT License. See license.txt - -from __future__ import unicode_literals -import frappe - -from frappe.utils.email_lib.email_body import get_email -from frappe.utils.email_lib.smtp import send - -def sendmail_md(recipients, sender=None, msg=None, subject=None, attachments=None): - """send markdown email""" - import markdown2 - sendmail(recipients, sender, markdown2.markdown(msg), subject, attachments) - -def sendmail(recipients, sender='', msg='', subject='[No Subject]', attachments=None): - """send an html email as multipart with attachments and all""" - send(get_email(recipients, sender, msg, subject, attachments=attachments)) - -def sendmail_to_system_managers(subject, content): - send(get_email(get_system_managers(), None, content, subject)) - -@frappe.whitelist() -def get_contact_list(): - """Returns contacts (from autosuggest)""" - cond = ['`%s` like "%s%%"' % (f, - frappe.form_dict.get('txt')) for f in frappe.form_dict.get('where').split(',')] - cl = frappe.db.sql("select `%s` from `tab%s` where %s" % ( - frappe.form_dict.get('select') - ,frappe.form_dict.get('from') - ,' OR '.join(cond) - ) - ) - frappe.response['cl'] = filter(None, [c[0] for c in cl]) - -def get_system_managers(): - return frappe.db.sql_list("""select parent FROM tabUserRole - WHERE role='System Manager' - AND parent!='Administrator' - AND parent IN - (SELECT email FROM tabUser WHERE enabled=1)""") diff --git a/frappe/website/doctype/post/post.py b/frappe/website/doctype/post/post.py index 556aa08884..59a8a6174a 100644 --- a/frappe/website/doctype/post/post.py +++ b/frappe/website/doctype/post/post.py @@ -8,8 +8,8 @@ from __future__ import unicode_literals import frappe from frappe import _ from frappe.utils import get_fullname -from frappe.utils.email_lib.bulk import send -from frappe.utils.email_lib import sendmail +from frappe.email.bulk import send +from frappe.email import sendmail from frappe.model.document import Document