Commit all these files.

This commit is contained in:
webnotes 2013-03-11 10:43:33 +05:30
parent 77a6ddaf03
commit 08bd677664
2 changed files with 37 additions and 36 deletions

View File

@ -1,6 +1,9 @@
import os import os
import webnotes import webnotes
from webnotes.utils import get_request_site_address from webnotes.utils import get_request_site_address, get_base_path
from webnotes import _
filename = ''
@webnotes.whitelist() @webnotes.whitelist()
def get_dropbox_authorize_url(): def get_dropbox_authorize_url():
@ -10,7 +13,6 @@ def get_dropbox_authorize_url():
+ "?cmd=setup.doctype.backup_manager.backup_dropbox.dropbox_callback" + "?cmd=setup.doctype.backup_manager.backup_dropbox.dropbox_callback"
url = sess.build_authorize_url(request_token, return_address) url = sess.build_authorize_url(request_token, return_address)
return { return {
"url": url, "url": url,
"key": request_token.key, "key": request_token.key,
@ -49,7 +51,7 @@ def dropbox_callback(oauth_token=None, not_approved=False):
webnotes.response['page_name'] = 'message.html' webnotes.response['page_name'] = 'message.html'
def backup_to_dropbox(): def backup_to_dropbox():
from dropbox import client, session from dropbox import client, session, rest
from conf import dropbox_access_key, dropbox_secret_key from conf import dropbox_access_key, dropbox_secret_key
from webnotes.utils.backups import new_backup from webnotes.utils.backups import new_backup
if not webnotes.conn: if not webnotes.conn:
@ -67,22 +69,19 @@ def backup_to_dropbox():
backup = new_backup() backup = new_backup()
filename = backup.backup_path_db filename = backup.backup_path_db
upload_file_to_dropbox(filename, "database", dropbox_client) upload_file_to_dropbox(filename, "database", dropbox_client)
# upload files # upload files
response = dropbox_client.metadata("files") response = dropbox_client.metadata("/database")
#add missing files
# for filename in os.listdir(filename):
# found = False
# for file_metadata in response["contents"]:
# if filename==os.path.basename(file_metadata["path"]):
# if os.stat(filename).st_size==file_metadata["bytes"]:
# found=True
# add missing files # if not found:
for filename in os.listdir(os.path.join("public", "files")): # upload_file_to_dropbox(filename, "database", dropbox_client)
found = False
for file_metadata in response["contents"]:
if filename==os.path.basename(file_metadata["path"]):
if os.stat(os.path.join("public", "files", filename)).st_size==file_metadata["bytes"]:
found=True
if not found:
upload_file_to_dropbox(os.path.join("public", "files", filename), "files", dropbox_client)
def get_dropbox_session(): def get_dropbox_session():
from dropbox import session from dropbox import session
@ -91,14 +90,14 @@ def get_dropbox_session():
except ImportError, e: except ImportError, e:
webnotes.msgprint(_("Please set Dropbox access keys in") + " conf.py", webnotes.msgprint(_("Please set Dropbox access keys in") + " conf.py",
raise_exception=True) raise_exception=True)
sess = session.DropboxSession(dropbox_access_key, dropbox_secret_key, "app_folder") sess = session.DropboxSession(dropbox_access_key, dropbox_secret_key, "app_folder")
return sess return sess
def upload_file_to_dropbox(filename, folder, dropbox_client): def upload_file_to_dropbox(filename, folder, dropbox_client):
if __name__=="__main__": path = os.path.join(get_base_path(),"public", "backups")
print "Uploading " + filename for files in os.listdir(path):
size = os.stat(filename).st_size file_name = path + "/" + files
size = os.stat(file_name).st_size
f = open(filename,'r') f = open(filename,'r')
if size > 4194304: if size > 4194304:
@ -109,7 +108,7 @@ def upload_file_to_dropbox(filename, folder, dropbox_client):
except rest.ErrorResponse, e: except rest.ErrorResponse, e:
pass pass
else: else:
response = dropbox_client.put_file(folder + "/" + os.path.basename(filename), f, overwrite=True) response = dropbox_client.put_file(folder + "/" + os.path.basename(file_name), f, overwrite=True)
if __name__=="__main__": if __name__=="__main__":
backup_to_dropbox() backup_to_dropbox()

View File

@ -3,6 +3,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import webnotes import webnotes
from webnotes import _ from webnotes import _
from backup_dropbox import dropbox_callback, get_dropbox_session, get_dropbox_authorize_url
class DocType: class DocType:
def __init__(self, d, dl): def __init__(self, d, dl):
@ -26,7 +27,6 @@ def take_backups():
send_email(True, "Dropbox") send_email(True, "Dropbox")
except Exception, e: except Exception, e:
send_email(False, "Dropbox", e) send_email(False, "Dropbox", e)
def send_email(success, service_name, error_status=None): def send_email(success, service_name, error_status=None):
if success: if success:
subject = "Backup Upload Successful" subject = "Backup Upload Successful"
@ -46,3 +46,5 @@ def send_email(success, service_name, error_status=None):
from webnotes.utils.email_lib import sendmail from webnotes.utils.email_lib import sendmail
sendmail(webnotes.conn.get_value("Backup Manager", None, "send_notifications_to").split(","), sendmail(webnotes.conn.get_value("Backup Manager", None, "send_notifications_to").split(","),
subject=subject, msg=message) subject=subject, msg=message)
get_dropbox_authorize_url()