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

from __future__ import unicode_literals
import webnotes

class DocType():
	def __init__(self, doc, doclist=[]):
		self.doc = doc
		self.doclist = doclist
	
	def get_parent_bean(self):
		if self.doc.contact:
			parent_doctype = "Contact"
			parent_name = self.doc.contact
		else:
			parent_doctype = "Lead"
			parent_name = self.doc.lead
			
		return webnotes.bean(parent_doctype, parent_name)
	
	def on_update(self):
		"""update status of parent Lead or Contact based on who is replying"""
		parent = self.get_parent_bean()

		if webnotes.conn.get_value("Profile", self.doc.sender, "user_type")=="System User":
			parent.doc.status = "Replied"
		else:
			parent.doc.status = "Open"
		
		parent.bean.ignore_permissions = True
		parent.bean.save()

@webnotes.whitelist()
def make(doctype=None, name=None, content=None, subject=None, 
	sender=None, recipients=None, contact=None, lead=None, company=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:
		import json
		sender = json.loads(sender)
	except ValueError:
		pass
	
	if isinstance(sender, (tuple, list)) and len(sender)==2:
		from email.utils import formataddr
		sender = formataddr(sender)
	
	comm = webnotes.new_bean('Communication')
	d = comm.doc
	d.subject = subject
	d.content = content
	d.sender = sender or webnotes.conn.get_value("Profile", webnotes.session.user, "email")
	d.recipients = recipients
	d.lead = lead
	d.contact = contact
	d.company = company
	if date:
		d.creation = date
	if doctype:
		sent_via = webnotes.get_obj(doctype, name)
		d.fields[doctype.replace(" ", "_").lower()] = name

	if set_lead:
		set_lead_and_contact(d)
	d.communication_medium = communication_medium
	if send_email:
		send_comm_email(d, name, sent_via, print_html, attachments, send_me_a_copy)
	comm.insert()

@webnotes.whitelist()
def get_customer_supplier(args=None):
	"""
		Get Customer/Supplier, given a contact, if a unique match exists
	"""
	import webnotes
	if not args: args = webnotes.form_dict
	if not args.get('contact'):
		raise Exception, "Please specify a contact to fetch Customer/Supplier"
	result = webnotes.conn.sql("""\<commit_msg>[communications] [minor] Code cleanup so it can be extended by custom script<commit_after># Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
# MIT License. See license.txt

from __future__ import unicode_literals
import webnotes

class DocType():
	def __init__(self, doc, doclist=[]):
		self.doc = doc
		self.doclist = doclist
	
	def get_parent_bean(self):
		if self.doc.contact:
			parent_doctype = "Contact"
			parent_name = self.doc.contact
		else:
			parent_doctype = "Lead"
			parent_name = self.doc.lead
			
		return webnotes.bean(parent_doctype, parent_name)
	
	def on_update(self):
		"""update status of parent Lead or Contact based on who is replying"""
		parent = self.get_parent_bean()

		if webnotes.conn.get_value("Profile", self.doc.sender, "user_type")=="System User":
			parent.doc.status = "Replied"
		else:
			parent.doc.status = "Open"
		
		parent.bean.ignore_permissions = True
		parent.bean.save()

@webnotes.whitelist()
def make(doctype=None, name=None, content=None, subject=None, 
	sender=None, recipients=None, contact=None, lead=None, company=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:
		import json
		sender = json.loads(sender)
	except ValueError:
		pass
	
	if isinstance(sender, (tuple, list)) and len(sender)==2:
		from email.utils import formataddr
		sender = formataddr(sender)
	
	comm = webnotes.new_bean('Communication')
	d = comm.doc
	d.subject = subject
	d.content = content
	d.sender = sender or webnotes.conn.get_value("Profile", webnotes.session.user, "email")
	d.recipients = recipients
	d.lead = lead
	d.contact = contact
	d.company = company
	if date:
		d.creation = date
	if doctype:
		sent_via = webnotes.get_obj(doctype, name)
		d.fields[doctype.replace(" ", "_").lower()] = name

	if set_lead:
		set_lead_and_contact(d)
	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()

@webnotes.whitelist()
def get_customer_supplier(args=None):
	"""
		Get Customer/Supplier, given a contact, if a unique match exists
	"""
	import webnotes
	if not args: args = webnotes.form_dict
	if not args.get('contact'):
		raise Exception, "Please specify a contact to fetch Customer/Supplier"
	result = webnotes.conn.sql("""\
{'fraction_non_alphanumeric': 0.11316995417413828, 'fraction_numerical': 0.0019924287706714486, 'mean_word_length': 4.685164212910532, 'pattern_counts': {'":': 2, '<': 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': '6428576', 'n_tokens_mistral': 1828, 'n_tokens_neox': 1661, 'n_words': 446}