Merge branch 'master' of github.com:webnotes/erpnext
diff --git a/setup/doctype/backup_manager/backup_dropbox.py b/setup/doctype/backup_manager/backup_dropbox.py
index 2901638..ac07824 100644
--- a/setup/doctype/backup_manager/backup_dropbox.py
+++ b/setup/doctype/backup_manager/backup_dropbox.py
@@ -7,12 +7,14 @@
 # dropbox_access_key
 # dropbox_access_secret
 
-
+from __future__ import unicode_literals
 import os
 import webnotes
-from webnotes.utils import get_request_site_address, get_base_path
+from webnotes.utils import get_request_site_address, get_base_path, cstr
 from webnotes import _
 
+from backup_manager import ignore_list
+
 @webnotes.whitelist()
 def get_dropbox_authorize_url():
 	sess = get_dropbox_session()
@@ -58,7 +60,7 @@
 
 	webnotes.message_title = "Dropbox Approval"
 	webnotes.message = "<h3>%s</h3><p>Please close this window.</p>" % message
-
+	
 	webnotes.conn.commit()
 	webnotes.response['type'] = 'page'
 	webnotes.response['page_name'] = 'message.html'
@@ -81,12 +83,18 @@
 	backup = new_backup()
 	filename = os.path.join(get_base_path(), "public", "backups", 
 		os.path.basename(backup.backup_path_db))
-	upload_file_to_dropbox(filename, "database", dropbox_client)
+	upload_file_to_dropbox(filename, "/database", dropbox_client)
 
 	response = dropbox_client.metadata("/files")
+	
 	# upload files to files folder
+	did_not_upload = []
+	error_log = []
 	path = os.path.join(get_base_path(), "public", "files")
 	for filename in os.listdir(path):
+		if filename in ignore_list:
+			continue
+		
 		found = False
 		filepath = os.path.join(path, filename)
 		for file_metadata in response["contents"]:
@@ -94,7 +102,13 @@
 				found = True
 				break
 		if not found:
-			upload_file_to_dropbox(filepath, "files", dropbox_client)
+			try:
+				upload_file_to_dropbox(filepath, "/files", dropbox_client)
+			except Exception, e:
+				did_not_upload.append(filename)
+				error_log.append(cstr(e))
+	
+	return did_not_upload, list(set(error_log))
 
 def get_dropbox_session():
 	try:
@@ -113,21 +127,21 @@
 def upload_file_to_dropbox(filename, folder, dropbox_client):
 	from dropbox import rest
 	size = os.stat(filename).st_size
-	f = open(filename,'r')
 	
-	# if max packet size reached, use chunked uploader
-	max_packet_size = 4194304
+	with open(filename, 'r') as f:
+		# if max packet size reached, use chunked uploader
+		max_packet_size = 4194304
 	
-	if size > max_packet_size:
-		uploader = dropbox_client.get_chunked_uploader(f, size)
-		while uploader.offset < size:
-			try:
-				uploader.upload_chunked()
-				uploader.finish(folder + "/" + os.path.basename(filename), overwrite=True)
-			except rest.ErrorResponse:
-				pass
-	else:
-		dropbox_client.put_file(folder + "/" + os.path.basename(filename), f, overwrite=True)
+		if size > max_packet_size:
+			uploader = dropbox_client.get_chunked_uploader(f, size)
+			while uploader.offset < size:
+				try:
+					uploader.upload_chunked()
+					uploader.finish(folder + "/" + os.path.basename(filename), overwrite=True)
+				except rest.ErrorResponse:
+					pass
+		else:
+			dropbox_client.put_file(folder + "/" + os.path.basename(filename), f, overwrite=True)
 
 if __name__=="__main__":
 	backup_to_dropbox()
\ No newline at end of file
diff --git a/setup/doctype/backup_manager/backup_googledrive.py b/setup/doctype/backup_manager/backup_googledrive.py
index 4b2a82c..24cd857 100644
--- a/setup/doctype/backup_manager/backup_googledrive.py
+++ b/setup/doctype/backup_manager/backup_googledrive.py
@@ -10,12 +10,13 @@
 # gdrive_client_id
 # gdrive_client_secret
 
+from __future__ import unicode_literals
 import httplib2
 import os
 import mimetypes
 import webnotes
 import oauth2client.client
-from webnotes.utils import get_base_path
+from webnotes.utils import get_base_path, cstr
 from webnotes import _, msgprint
 from apiclient.discovery import build
 from apiclient.http import MediaFileUpload
@@ -30,6 +31,9 @@
 
 @webnotes.whitelist()
 def upload_files(name, mimetype, service, folder_id):
+	import logging
+	logging.basicConfig()
+	
 	if not webnotes.conn:
 		webnotes.connect()
 	file_name = os.path.basename(name)
@@ -67,8 +71,11 @@
 	# upload files to database folder
 	upload_files(filename, 'application/x-gzip', drive_service, 
 		webnotes.conn.get_value("Backup Manager", None, "database_folder_id"))
-
+	
 	# upload files to files folder
+	did_not_upload = []
+	error_log = []
+	
 	path = os.path.join(get_base_path(), "public", "files")
 	for filename in os.listdir(path):
 		found = False
@@ -78,7 +85,8 @@
 		if ext == 'gz' or ext == 'gzip':
 			mimetype = 'application/x-gzip'
 		else:
-			mimetype = mimetypes.types_map["." + ext]
+			mimetype = mimetypes.types_map.get("." + ext) or "application/octet-stream"
+		
 		#Compare Local File with Server File
 		param = {}
 	  	children = drive_service.children().list(
@@ -90,7 +98,14 @@
 				found = True
 				break
 		if not found:
-			upload_files(filepath, mimetype, drive_service, webnotes.conn.get_value("Backup Manager", None, "files_folder_id"))
+			try:
+				upload_files(filepath, mimetype, drive_service, 
+					webnotes.conn.get_value("Backup Manager", None, "files_folder_id"))
+			except Exception, e:
+				did_not_upload.append(filename)
+				error_log.append(cstr(e))
+	
+	return did_not_upload, list(set(error_log))
 
 def get_gdrive_flow():
 	from oauth2client.client import OAuth2WebServerFlow
diff --git a/setup/doctype/backup_manager/backup_manager.js b/setup/doctype/backup_manager/backup_manager.js
index 28315c5..8d2d8de 100644
--- a/setup/doctype/backup_manager/backup_manager.js
+++ b/setup/doctype/backup_manager/backup_manager.js
@@ -41,7 +41,9 @@
 		wn.call({
 			method: "setup.doctype.backup_manager.backup_googledrive.get_gdrive_authorize_url",
 			callback: function(r) {
-				window.open(r.message.authorize_url);
+				if(!r.exc) {
+					window.open(r.message.authorize_url);
+				}
 			}
 		})
 	}
@@ -49,7 +51,7 @@
 
 cur_frm.cscript.validate_gdrive = function(doc) {
 	wn.call({
-		method: "setup.doctype.backup_manager.backup_manager.gdrive_callback",
+		method: "setup.doctype.backup_manager.backup_googledrive.gdrive_callback",
 		args: {
 			verification_code: doc.verification_code
 		},
diff --git a/setup/doctype/backup_manager/backup_manager.py b/setup/doctype/backup_manager/backup_manager.py
index ce0f1e1..feeddf0 100644
--- a/setup/doctype/backup_manager/backup_manager.py
+++ b/setup/doctype/backup_manager/backup_manager.py
@@ -4,6 +4,8 @@
 import webnotes
 from webnotes import _
 
+ignore_list = []
+
 class DocType:
 	def __init__(self, d, dl):
 		self.doc, self.doclist = d, dl
@@ -23,22 +25,32 @@
 	
 @webnotes.whitelist()
 def take_backups_dropbox():
+	did_not_upload, error_log = [], []
 	try:
 		from setup.doctype.backup_manager.backup_dropbox import backup_to_dropbox
-		backup_to_dropbox()
+		did_not_upload, error_log = backup_to_dropbox()
+		if did_not_upload: raise Exception
+		
 		send_email(True, "Dropbox")
 	except Exception:
-		send_email(False, "Dropbox", webnotes.getTraceback())
+		error_message = ("\n".join(error_log) + "\n" + webnotes.getTraceback())
+		print error_message
+		send_email(False, "Dropbox", error_message)
 
 #backup to gdrive 
 @webnotes.whitelist()
 def take_backups_gdrive():
+	did_not_upload, error_log = [], []
 	try:
 		from setup.doctype.backup_manager.backup_googledrive import backup_to_gdrive
-		backup_to_gdrive()
+		did_not_upload, error_log = backup_to_gdrive()
+		if did_not_upload: raise Exception
+		
 		send_email(True, "Google Drive")
 	except Exception:
-		send_email(False, "Google Drive", webnotes.getTraceback())
+		error_message = ("\n".join(error_log) + "\n" + webnotes.getTraceback())
+		print error_message
+		send_email(False, "Google Drive", error_message)
 
 def send_email(success, service_name, error_status=None):
 	if success:
@@ -58,4 +70,4 @@
 	# email system managers
 	from webnotes.utils.email_lib import sendmail
 	sendmail(webnotes.conn.get_value("Backup Manager", None, "send_notifications_to").split(","),
-	subject=subject, msg=message)
\ No newline at end of file
+		subject=subject, msg=message)