Merge branch 'master' of github.com:akhileshdarjee/erpnext

This commit is contained in:
Rushabh Mehta 2013-03-18 10:57:37 +05:30
commit 9feffd5591
4 changed files with 165 additions and 48 deletions

View File

@ -1,6 +1,7 @@
import os
import webnotes
from webnotes.utils import get_request_site_address
from webnotes.utils import get_request_site_address, get_base_path
from webnotes import _
@webnotes.whitelist()
def get_dropbox_authorize_url():
@ -10,7 +11,7 @@ def get_dropbox_authorize_url():
+ "?cmd=setup.doctype.backup_manager.backup_dropbox.dropbox_callback"
url = sess.build_authorize_url(request_token, return_address)
return {
"url": url,
"key": request_token.key,
@ -19,43 +20,43 @@ def get_dropbox_authorize_url():
@webnotes.whitelist(allow_guest=True)
def dropbox_callback(oauth_token=None, not_approved=False):
from dropbox import client
if not not_approved:
if webnotes.conn.get_value("Backup Manager", None, "dropbox_access_key")==oauth_token:
webnotes.conn.set_value("Backup Manager", "Backup Manager", "dropbox_access_allowed", 1)
allowed = 1
message = "Dropbox access allowed."
sess = get_dropbox_session()
sess.set_request_token(webnotes.conn.get_value("Backup Manager", None, "dropbox_access_key"),
webnotes.conn.get_value("Backup Manager", None, "dropbox_access_secret"))
access_token = sess.obtain_access_token()
webnotes.conn.set_value("Backup Manager", "Backup Manager", "dropbox_access_key", access_token.key)
webnotes.conn.set_value("Backup Manager", "Backup Manager", "dropbox_access_secret", access_token.secret)
webnotes.conn.set_value("Backup Manager", "Backup Manager", "dropbox_access_allowed", allowed)
dropbox_client = client.DropboxClient(sess)
dropbox_client.file_create_folder("files")
webnotes.conn.set_value("Backup Manager", "Backup Manager", "dropbox_access_key",
access_token.key)
webnotes.conn.set_value("Backup Manager", "Backup Manager", "dropbox_access_secret",
access_token.secret)
else:
webnotes.conn.set_value("Backup Manager", "Backup Manager", "dropbox_access_allowed", 0)
allowed = 0
message = "Illegal Access Token Please try again."
else:
webnotes.conn.set_value("Backup Manager", "Backup Manager", "dropbox_access_allowed", 0)
allowed = 0
message = "Dropbox Access not approved."
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'
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 webnotes.utils.backups import new_backup
if not webnotes.conn:
webnotes.connect()
sess = session.DropboxSession(dropbox_access_key, dropbox_secret_key, "app_folder")
sess.set_token(webnotes.conn.get_value("Backup Manager", None, "dropbox_access_key"),
@ -68,21 +69,19 @@ def backup_to_dropbox():
filename = backup.backup_path_db
upload_file_to_dropbox(filename, "database", dropbox_client)
# upload files
response = dropbox_client.metadata("files")
response = dropbox_client.metadata("/files")
# add missing files
for filename in os.listdir(os.path.join("public", "files")):
# upload files to files folder
filename = os.path.join(get_base_path(),"public", "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(os.path.join("public", "files", filename)).st_size==file_metadata["bytes"]:
if os.stat(filename).st_size==file_metadata["bytes"]:
found=True
if not found:
upload_file_to_dropbox(os.path.join("public", "files", filename), "files", dropbox_client)
upload_file_to_dropbox(os.path.join(get_base_path(),"public", "files", filename), "files", dropbox_client)
def get_dropbox_session():
from dropbox import session
@ -91,21 +90,18 @@ def get_dropbox_session():
except ImportError, e:
webnotes.msgprint(_("Please set Dropbox access keys in") + " conf.py",
raise_exception=True)
sess = session.DropboxSession(dropbox_access_key, dropbox_secret_key, "app_folder")
return sess
def upload_file_to_dropbox(filename, folder, dropbox_client):
if __name__=="__main__":
print "Uploading " + filename
size = os.stat(filename).st_size
f = open(filename,'r')
if size > 4194304:
uploader = dropbox_client.get_chunked_uploader(f, size)
while uploader.offset < size:
try:
uploader.upload_chunked()
finish(folder + '/' + os.path.basename(filename), overwrite='True')
except rest.ErrorResponse, e:
pass
else:

View File

@ -1,24 +1,66 @@
cur_frm.cscript.refresh = function(doc) {
cur_frm.disable_save();
}
//dropbox
cur_frm.cscript.allow_dropbox_access = function(doc) {
wn.call({
method: "setup.doctype.backup_manager.backup_dropbox.get_dropbox_authorize_url",
callback: function(r) {
if(!r.exc) {
cur_frm.set_value("dropbox_access_secret", r.message.secret);
cur_frm.set_value("dropbox_access_key", r.message.key);
cur_frm.save(null, function() {
window.open(r.message.url);
});
if (doc.send_notifications_to == '') {
msgprint("Please enter email address.")
}
else {
wn.call({
method: "setup.doctype.backup_manager.backup_dropbox.get_dropbox_authorize_url",
callback: function(r) {
if(!r.exc) {
cur_frm.set_value("dropbox_access_secret", r.message.secret);
cur_frm.set_value("dropbox_access_key", r.message.key);
cur_frm.save(null, function() {
window.open(r.message.url);
});
}
}
}
})
})
}
}
cur_frm.cscript.backup_right_now = function(doc) {
msgprint("Backing up and uploading. This may take a few minutes.")
wn.call({
method: "setup.doctype.backup_manager.backup_manager.take_backups",
method: "setup.doctype.backup_manager.backup_manager.take_backups_dropbox",
callback: function(r) {
msgprint("Backups taken. Please check your email for the response.")
}
})
}
}
//gdrive
cur_frm.cscript.allow_gdrive_access = function(doc) {
if (doc.send_notifications_to == '') {
msgprint("Please enter email address.")
}
else {
wn.call({
method: "setup.doctype.backup_manager.backup_googledrive.get_gdrive_authorize_url",
callback: function(r) {
window.open(r.message.authorize_url);
}
})
}
}
cur_frm.cscript.validate_gdrive_code = function(doc) {
wn.call({
method: "setup.doctype.backup_manager.backup_manager.gdrive_callback",
args: {
verification_code: doc.verification_code
},
});
}
// cur_frm.cscript.backup_to_gdrive = function(doc) {
// msgprint("Backing up and uploading. This may take a few minutes.")
// wn.call({
// method: "setup.doctype.backup_manager.backup_manager.take_backups_gdrive",
// callback: function(r) {
// msgprint("Backups taken. Please check your email for the response.")
// }
// })
// }

View File

@ -3,6 +3,8 @@
from __future__ import unicode_literals
import webnotes
from webnotes import _
from backup_dropbox import dropbox_callback, get_dropbox_session, get_dropbox_authorize_url
from backup_googledrive import gdrive_callback, get_gdrive_flow, get_gdrive_authorize_url
class DocType:
def __init__(self, d, dl):
@ -16,10 +18,11 @@ def take_backups_weekly():
def take_backups_if(freq):
if webnotes.conn.get_value("Backup Manager", None, "upload_backups_to_dropbox")==freq:
take_backups()
take_backups_dropbox()
take_backups_gdrive()
#backup to dropbox
@webnotes.whitelist()
def take_backups():
def take_backups_dropbox():
try:
from setup.doctype.backup_manager.backup_dropbox import backup_to_dropbox
backup_to_dropbox()
@ -27,6 +30,16 @@ def take_backups():
except Exception, e:
send_email(False, "Dropbox", e)
#backup to gdrive
@webnotes.whitelist()
def take_backups_gdrive():
try:
from setup.doctype.backup_manager.backup_googledrive import backup_to_gdrive
backup_to_gdrive()
send_email(True, "Google Drive")
except Exception, e:
send_email(False, "Google Drive", e)
def send_email(success, service_name, error_status=None):
if success:
subject = "Backup Upload Successful"
@ -44,5 +57,5 @@ def send_email(success, service_name, error_status=None):
# 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)
sendmail(webnotes.conn.get_value("Backup Manager", None, "send_notifications_to").split(","),
subject=subject, msg=message)

View File

@ -1,8 +1,8 @@
[
{
"creation": "2013-03-05 16:35:50",
"creation": "2013-03-15 11:06:59",
"docstatus": 0,
"modified": "2013-03-07 12:18:07",
"modified": "2013-03-15 16:10:05",
"modified_by": "Administrator",
"owner": "Administrator"
},
@ -56,7 +56,9 @@
"doctype": "DocField",
"fieldname": "backup_right_now",
"fieldtype": "Button",
"label": "Backup Right Now"
"hidden": 1,
"label": "Backup Right Now",
"read_only": 1
},
{
"description": "Note: Backups and files are not deleted from Dropbox, you will have to delete them manually.",
@ -102,6 +104,70 @@
"fieldtype": "Button",
"label": "Allow Dropbox Access"
},
{
"description": "Note: Backups and files are not deleted from Google Drive, you will have to delete them manually.",
"doctype": "DocField",
"fieldname": "sync_with_gdrive",
"fieldtype": "Section Break",
"label": "Sync with Google Drive"
},
{
"doctype": "DocField",
"fieldname": "upload_backups_to_gdrive",
"fieldtype": "Select",
"label": "Upload Backups to Google Drive",
"options": "Never\nDaily\nWeekly"
},
{
"doctype": "DocField",
"fieldname": "allow_gdrive_access",
"fieldtype": "Button",
"label": "Allow Google Drive Access"
},
{
"doctype": "DocField",
"fieldname": "verification_code",
"fieldtype": "Data",
"label": "Enter Verification Code"
},
{
"doctype": "DocField",
"fieldname": "validate_gdrive",
"fieldtype": "Button",
"label": "Validate"
},
{
"doctype": "DocField",
"fieldname": "gdrive_access_allowed",
"fieldtype": "Check",
"hidden": 1,
"label": "Google Drive Access Allowed",
"read_only": 1
},
{
"doctype": "DocField",
"fieldname": "gdrive_credentials",
"fieldtype": "Text",
"hidden": 1,
"label": "Credentials",
"read_only": 1
},
{
"doctype": "DocField",
"fieldname": "database_folder_id",
"fieldtype": "Data",
"hidden": 1,
"label": "Database Folder ID",
"read_only": 1
},
{
"doctype": "DocField",
"fieldname": "files_folder_id",
"fieldtype": "Data",
"hidden": 1,
"label": "Files Folder ID",
"read_only": 1
},
{
"doctype": "DocPerm"
}