<commit_before># Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt

from __future__ import unicode_literals
import frappe
import json
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.utils import scrub_urls
from frappe import _

from frappe.model.document import Document

class Communication(Document):
	def get_parent_doc(self):
		return frappe.get_doc(self.parenttype, self.parent)

	def update_parent(self):
		"""update status of parent Lead or Contact based on who is replying"""
		observer = getattr(self.get_parent_doc(), "on_communication", None)
		if observer:
			observer()

	def on_update(self):
		self.update_parent()

@frappe.whitelist()
def make(doctype=None, name=None, content=None, subject=None, sent_or_received = "Sent",
	sender=None, recipients=None, communication_medium="Email", send_email=False,
	print_html=None, attachments='[]', send_me_a_copy=False, set_lead=True, date=None):

	if doctype and name and not frappe.has_permission(doctype, "email", name):
		raise frappe.PermissionError("You are not allowed to send emails related to: {doctype} {name}".format(
			doctype=doctype, name=name))

	_make(doctype=doctype, name=name, content=content, subject=subject, sent_or_received=sent_or_received,
		sender=sender, recipients=recipients, communication_medium=communication_medium, send_email=send_email,
		print_html=print_html, attachments=attachments, send_me_a_copy=send_me_a_copy, set_lead=set_lead,
		date=date)

def _make(doctype=None, name=None, content=None, subject=None, sent_or_received = "Sent",
	sender=None, recipients=None, communication_medium="Email", send_email=False,
	print_html=None, attachments='[]', send_me_a_copy=False, set_lead=True, date=None):

	# add to Communication
	sent_via = None

	# since we are using fullname and email,
	# if the fullname has any incompatible characters,formataddr can deal with it
	try:
		sender = json.loads(sender)
	except ValueError:
		pass

	if isinstance(sender, (tuple, list)) and len(sender)==2:
		sender = formataddr(sender)

	comm = frappe.new_doc('Communication')
	d = comm
	d.subject = subject
	d.content = content
	d.sent_or_received = sent_or_received
	d.sender = sender or frappe.db.get_value("User", frappe.session.user, "email")
	d.recipients = recipients

	# add as child
	sent_via = frappe.get_doc(doctype, name)
	d.parent = name
	d.parenttype = doctype
	d.parentfield = "communications"

	if date:
		d.communication_date = date

	d.communication_medium = communication_medium

	comm.ignore_permissions = True
	comm.insert()

	if send_email:
		d = comm
		send_comm_email(d, name, sent_via, print_html, attachments, send_me_a_copy)

@frappe.whitelist()
def get_customer_supplier(args=None):
	"""
		Get Customer/Supplier, given a contact, if a unique match exists<commit_msg>Set idx while adding communication. Fixes frappe/erpnext#1645<commit_after># Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt

from __future__ import unicode_literals
import frappe
import json
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.utils import scrub_urls, cint
from frappe import _

from frappe.model.document import Document

class Communication(Document):
	def get_parent_doc(self):
		return frappe.get_doc(self.parenttype, self.parent)

	def update_parent(self):
		"""update status of parent Lead or Contact based on who is replying"""
		observer = getattr(self.get_parent_doc(), "on_communication", None)
		if observer:
			observer()

	def on_update(self):
		self.update_parent()

@frappe.whitelist()
def make(doctype=None, name=None, content=None, subject=None, sent_or_received = "Sent",
	sender=None, recipients=None, communication_medium="Email", send_email=False,
	print_html=None, attachments='[]', send_me_a_copy=False, set_lead=True, date=None):

	if doctype and name and not frappe.has_permission(doctype, "email", name):
		raise frappe.PermissionError("You are not allowed to send emails related to: {doctype} {name}".format(
			doctype=doctype, name=name))

	_make(doctype=doctype, name=name, content=content, subject=subject, sent_or_received=sent_or_received,
		sender=sender, recipients=recipients, communication_medium=communication_medium, send_email=send_email,
		print_html=print_html, attachments=attachments, send_me_a_copy=send_me_a_copy, set_lead=set_lead,
		date=date)

def _make(doctype=None, name=None, content=None, subject=None, sent_or_received = "Sent",
	sender=None, recipients=None, communication_medium="Email", send_email=False,
	print_html=None, attachments='[]', send_me_a_copy=False, set_lead=True, date=None):

	# add to Communication
	sent_via = None

	# since we are using fullname and email,
	# if the fullname has any incompatible characters,formataddr can deal with it
	try:
		sender = json.loads(sender)
	except ValueError:
		pass

	if isinstance(sender, (tuple, list)) and len(sender)==2:
		sender = formataddr(sender)

	comm = frappe.new_doc('Communication')
	d = comm
	d.subject = subject
	d.content = content
	d.sent_or_received = sent_or_received
	d.sender = sender or frappe.db.get_value("User", frappe.session.user, "email")
	d.recipients = recipients

	# add as child
	sent_via = frappe.get_doc(doctype, name)
	d.parent = name
	d.parenttype = doctype
	d.parentfield = "communications"

	if date:
		d.communication_date = date

	d.communication_medium = communication_medium

	d.idx = cint(frappe.db.sql("""select max(idx) from `tabCommunication`
		where parenttype=%s and parent=%s""", (doctype, name))[0][0]) + 1

	comm.ignore_permissions = True
	comm.insert()

	if send_email:
		d = comm
		send_comm_email(d, name, sent_via, print_html, attachments, send_me_a_copy)

@frappe.whitelist()
def get_customer_supplier(args=None):
	"""
		Get Customer/Supplier, given a contact, if a unique match exists
{'fraction_non_alphanumeric': 0.10201793721973094, 'fraction_numerical': 0.0027226137091607944, 'mean_word_length': 6.137142857142857, 'pattern_counts': {'":': 0, '<': 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': '6607808', 'n_tokens_mistral': 2191, 'n_tokens_neox': 2058, 'n_words': 532}