<commit_before># Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt

from __future__ import unicode_literals

import frappe
import frappe.utils
from frappe.utils import cstr
from frappe import throw, _
from frappe.model.document import Document

class Newsletter(Document):
	def onload(self):
		if self.email_sent:
			self.set("__status_count", dict(frappe.db.sql("""select status, count(*)
				from `tabBulk Email` where ref_doctype=%s and ref_docname=%s
				group by status""", (self.doctype, self.name))) or None)

	def test_send(self, doctype="Lead"):
		self.recipients = self.test_email_id.split(",")
		self.send_to_doctype = "Lead"
		self.send_bulk()
		frappe.msgprint(_("Scheduled to send to {0}").format(self.test_email_id))

	def send_emails(self):
		"""send emails to leads and customers"""
		if self.email_sent:
			throw(_("Newsletter has already been sent"))

		self.recipients = self.get_recipients()
		self.send_bulk()

		frappe.msgprint(_("Scheduled to send to {0} recipients").format(len(self.recipients)))

		frappe.db.set(self, "email_sent", 1)

	def get_recipients(self):
		self.email_field = None
		if self.send_to_type=="Contact":
			self.send_to_doctype = "Contact"
			if self.contact_type == "Customer":
				return frappe.db.sql_list("""select email_id from tabContact<commit_msg>Newsletter status display. Fixes #1593<commit_after># Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt

from __future__ import unicode_literals

import frappe
import frappe.utils
from frappe.utils import cstr
from frappe import throw, _
from frappe.model.document import Document

class Newsletter(Document):
	def onload(self):
		if self.email_sent:
			self.set("__status_count", dict(frappe.db.sql("""select status, count(*)
				from `tabBulk Email` where ref_doctype=%s and ref_docname=%s
				group by status""", (self.doctype, self.name))) or None)

	def as_dict(self, no_nulls=False):
		doc = super(Newsletter, self).as_dict(no_nulls)
		if self.get("__status_count"):
			doc["__status_count"] = self.get("__status_count")

		return doc

	def test_send(self, doctype="Lead"):
		self.recipients = self.test_email_id.split(",")
		self.send_to_doctype = "Lead"
		self.send_bulk()
		frappe.msgprint(_("Scheduled to send to {0}").format(self.test_email_id))

	def send_emails(self):
		"""send emails to leads and customers"""
		if self.email_sent:
			throw(_("Newsletter has already been sent"))

		self.recipients = self.get_recipients()
		self.send_bulk()

		frappe.msgprint(_("Scheduled to send to {0} recipients").format(len(self.recipients)))

		frappe.db.set(self, "email_sent", 1)

	def get_recipients(self):
		self.email_field = None
		if self.send_to_type=="Contact":
			self.send_to_doctype = "Contact"
			if self.contact_type == "Customer":
				return frappe.db.sql_list("""select email_id from tabContact
{'fraction_non_alphanumeric': 0.12512613521695257, 'fraction_numerical': 0.006727211570803902, 'mean_word_length': 5.1067761806981515, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7384236', 'n_tokens_mistral': 1112, 'n_tokens_neox': 1019, 'n_words': 253}