Merge branch 'master' of github.com:webnotes/erpnext
diff --git a/hr/doctype/leave_application/leave_application.py b/hr/doctype/leave_application/leave_application.py
index 17f8526..0f8f21e 100755
--- a/hr/doctype/leave_application/leave_application.py
+++ b/hr/doctype/leave_application/leave_application.py
@@ -18,19 +18,19 @@
 import webnotes
 from webnotes import _
 
-from webnotes.utils import cint, cstr, date_diff, flt, formatdate, getdate
-from webnotes.model import db_exists
-from webnotes.model.wrapper import copy_doclist
-from webnotes import form, msgprint
-
-import datetime
+from webnotes.utils import cint, cstr, date_diff, flt, formatdate, getdate, get_url_to_form, get_fullname
+from webnotes import msgprint
+from webnotes.utils.email_lib import sendmail
 
 class LeaveDayBlockedError(Exception): pass
 	
-class DocType:
-	def __init__(self, doc, doclist):
-		self.doc = doc
-		self.doclist = doclist
+from webnotes.model.controller import DocListController
+class DocType(DocListController):
+	def setup(self):
+		if webnotes.conn.exists(self.doc.doctype, self.doc.name):
+			self.previous_doc = webnotes.doc(self.doc.doctype, self.doc.name)
+		else:
+			self.previous_doc = None
 		
 	def validate(self):
 		# if self.doc.leave_approver == self.doc.owner:
@@ -39,12 +39,29 @@
 		self.validate_leave_overlap()
 		self.validate_max_days()
 		self.validate_block_days()
+		
+	def on_update(self):
+		if (not self.previous_doc and self.doc.leave_approver) or (self.doc.status == "Open" \
+				and self.previous_doc.leave_approver != self.doc.leave_approver):
+			# notify leave approver about creation
+			self.notify_leave_approver()
+		elif self.previous_doc and \
+				self.previous_doc.status == "Open" and self.doc.status == "Rejected":
+			# notify employee about rejection
+			self.notify_employee(self.doc.status)
 	
 	def on_submit(self):
 		if self.doc.status != "Approved":
 			webnotes.msgprint("""Only Leave Applications with status 'Approved' can be Submitted.""",
 				raise_exception=True)
 
+		# notify leave applier about approval
+		self.notify_employee(self.doc.status)
+				
+	def on_cancel(self):
+		# notify leave applier about cancellation
+		self.notify_employee("cancelled")
+
 	def validate_block_days(self):
 		from hr.doctype.leave_block_list.leave_block_list import get_applicable_block_dates
 
@@ -121,7 +138,54 @@
 		if max_days and self.doc.total_leave_days > max_days:
 			msgprint("Sorry ! You cannot apply for %s for more than %s days" % (self.doc.leave_type, max_days))
 			raise Exception
-
+			
+	def notify_employee(self, status):
+		employee = webnotes.doc("Employee", self.doc.employee)
+		if not employee.user_id:
+			return
+			
+		def _get_message(url=False):
+			if url:
+				name = get_url_to_form(self.doc.doctype, self.doc.name)
+			else:
+				name = self.doc.name
+				
+			return (_("Leave Application") + ": %s - %s") % (name, _(status))
+		
+		self.notify({
+			# for post in messages
+			"message": _get_message(url=True),
+			"message_to": employee.user_id,
+			
+			"subject": _get_message(),
+		})
+		
+	def notify_leave_approver(self):
+		employee = webnotes.doc("Employee", self.doc.employee)
+		
+		def _get_message(url=False):
+			name = self.doc.name
+			employee_name = get_fullname(employee.user_id)
+			if url:
+				name = get_url_to_form(self.doc.doctype, self.doc.name)
+				employee_name = get_url_to_form("Employee", self.doc.employee, label=employee_name)
+			
+			return (_("New Leave Application") + ": %s - " + _("Employee") + ": %s") % (name, employee_name)
+		
+		self.notify({
+			# for post in messages
+			"message": _get_message(url=True),
+			"message_to": self.doc.leave_approver,
+			
+			# for email
+			"subject": _get_message()
+		})
+		
+	def notify(self, args):
+		args = webnotes._dict(args)
+		from utilities.page.messages.messages import post
+		post({"txt": args.message, "contact": args.message_to, "subject": args.subject,
+			"notify": True})
 
 @webnotes.whitelist()
 def get_leave_balance(employee, leave_type, fiscal_year):	
diff --git a/patches/december_2012/website_cache_refactor.py b/patches/december_2012/website_cache_refactor.py
index 5a1f22e..3c157bd 100644
--- a/patches/december_2012/website_cache_refactor.py
+++ b/patches/december_2012/website_cache_refactor.py
@@ -19,6 +19,6 @@
 		webnotes.conn.set_value("Blog", page[0], "content", m)
 
 	# delete website cache
-	webnotes.conn.commit()
 	webnotes.delete_doc("DocType", "Web Cache")
+	webnotes.conn.commit()
 	webnotes.conn.sql("""drop table if exists `tabWeb Cache`""")
\ No newline at end of file
diff --git a/utilities/page/messages/messages.py b/utilities/page/messages/messages.py
index 019cd071..90f5e91 100644
--- a/utilities/page/messages/messages.py
+++ b/utilities/page/messages/messages.py
@@ -66,12 +66,14 @@
 def post(arg=None):
 	import webnotes
 	"""post message"""
-	if arg:
-		import json
-		arg = json.loads(arg)
-	else:
+	if not arg:
 		arg = {}
 		arg.update(webnotes.form_dict)
+	
+	if isinstance(arg, basestring):
+		import json
+		arg = json.loads(arg)
+
 	from webnotes.model.doc import Document
 	d = Document('Comment')
 	d.parenttype = arg.get("parenttype")
@@ -90,16 +92,13 @@
 		webnotes.form_dict['name']);
 
 def notify(arg=None):
-	from webnotes.utils import cstr
+	from webnotes.utils import cstr, get_fullname
 	from startup import get_url
 	
-	fn = webnotes.conn.sql('select first_name, last_name from tabProfile where name=%s', webnotes.user.name)[0]
-	if fn[0] or f[1]:
-		fn = cstr(fn[0]) + (fn[0] and ' ' or '') + cstr(fn[1])
-	else:
-		fn = webnotes.user.name
-
+	fn = get_fullname(webnotes.user.name) or webnotes.user.name
+	
 	url = get_url()
+	
 	message = '''You have a message from <b>%s</b>:
 	
 	%s
@@ -108,8 +107,11 @@
 	<a href=\"%s\" target='_blank'>%s</a>
 	''' % (fn, arg['txt'], url, url)
 	
-	sender = webnotes.user.name!='Administrator' and webnotes.user.name or 'support+admin_post@erpnext.com'
+	sender = webnotes.conn.get_value("Profile", webnotes.user.name, "email") \
+		or webnotes.user.name
+	recipient = [webnotes.conn.get_value("Profile", arg["contact"], "email") \
+		or arg["contact"]]
 	
 	from webnotes.utils.email_lib import sendmail
-	sendmail([arg['contact']], sender, message, "You have a message from %s" % (fn,))
+	sendmail(recipient, sender, message, arg.get("subject") or "You have a message from %s" % (fn,))
 	
\ No newline at end of file