Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion one_backup_manager/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
# "one_backup_manager.tasks.all"
# ],
"daily": [
"one_backup_manager.one_backup_manager.doctype.one_backup_settings.one_backup_settings.create_backup"
"one_backup_manager.one_backup_manager.doctype.one_backup_settings.one_backup_settings.auto_backup_to_gdrive"
],
# "hourly": [
# "one_backup_manager.tasks.hourly"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2023, ONE FM and contributors
// For license information, please see license.txt

frappe.ui.form.on('GDrive Upload', {
upload: function(frm) {
frappe.call({
doc: frm.doc,
method: 'upload_to_google_drive',
callback: function(r) {
frm.refresh();
},
freaze: true,
freaze_message: __("Uploading to Google Drive!")
})
},
view_in_gdrive: function(frm) {
window.open(frm.doc.google_drive_link);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
{
"actions": [],
"allow_rename": 1,
"autoname": "naming_series:",
"creation": "2023-11-21 13:17:49.581633",
"default_view": "List",
"doctype": "DocType",
"editable_grid": 1,
"engine": "InnoDB",
"field_order": [
"naming_series",
"parent_folder_name",
"file_folder_name",
"share_with",
"share_file_only",
"file",
"upload",
"google_drive_link",
"view_in_gdrive"
],
"fields": [
{
"fieldname": "naming_series",
"fieldtype": "Select",
"hidden": 1,
"label": "Series",
"options": "GDU-.MM.-.YYYY.-.####"
},
{
"fieldname": "parent_folder_name",
"fieldtype": "Data",
"label": "Parent Folder Name"
},
{
"fieldname": "file_folder_name",
"fieldtype": "Data",
"in_list_view": 1,
"label": "File Folder Name",
"reqd": 1
},
{
"fieldname": "file",
"fieldtype": "Attach",
"in_list_view": 1,
"label": "File",
"reqd": 1
},
{
"depends_on": "eval:!doc.google_drive_link",
"fieldname": "upload",
"fieldtype": "Button",
"label": "Upload to Google Drive"
},
{
"fieldname": "google_drive_link",
"fieldtype": "Data",
"label": "Google Drive Link",
"read_only": 1
},
{
"depends_on": "google_drive_link",
"fieldname": "view_in_gdrive",
"fieldtype": "Button",
"label": "View in Google Drive"
},
{
"fieldname": "share_with",
"fieldtype": "Data",
"label": "Share With",
"options": "Email",
"reqd": 1
},
{
"default": "1",
"fieldname": "share_file_only",
"fieldtype": "Check",
"label": "Share File Only"
}
],
"index_web_pages_for_search": 1,
"links": [],
"modified": "2023-11-27 08:59:31.556119",
"modified_by": "Administrator",
"module": "One Backup Manager",
"name": "GDrive Upload",
"naming_rule": "By \"Naming Series\" field",
"owner": "Administrator",
"permissions": [
{
"create": 1,
"delete": 1,
"email": 1,
"export": 1,
"print": 1,
"read": 1,
"report": 1,
"role": "System Manager",
"share": 1,
"write": 1
}
],
"sort_field": "modified",
"sort_order": "DESC",
"states": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Copyright (c) 2023, ONE FM and contributors
# For license information, please see license.txt

import frappe
from frappe.model.document import Document
from one_backup_manager.utils.gdrive import GoogleDriveUploader
import os
import datetime
from one_fm.api.api import upload_file as upload_file_to_db
from pathlib import Path
import hashlib
import base64

class GDriveUpload(Document):
@frappe.whitelist()
def upload_to_google_drive(self):
gdrive = GoogleDriveUploader()
file_path = os.getcwd()+"/"+frappe.utils.cstr(frappe.local.site)+'/private/files/'
file_name = frappe.db.get_value("File", {"file_url": self.file}, "file_name")

parent_folder_ids = []

if self.parent_folder_name:
if self.share_file_only:
parent_folder = gdrive.create_folder(self.parent_folder_name)
else:
parent_folder = gdrive.create_folder(self.parent_folder_name, self.share_with)
parent_folder_ids.append(parent_folder.get('id'))

if self.share_file_only:
file_folder = gdrive.create_folder(self.file_folder_name, parent_folder_ids)
else:
file_folder = gdrive.create_folder(self.file_folder_name, self.share_with, parent_folder_ids)

try:
# Create file in google dirve
file = gdrive.upload_file(file_name, [file_folder.get('id')], file_path, self.share_with)
# Create Google Drive Log in our system with the google drive link to the file folder
gdrive.create_log(1, url=file.get('webViewLink'))
self.db_set("google_drive_link", file.get('webViewLink'))
frappe.msgprint("Upload to Google Drive Complete", alert =1)
except:
# Create error log and notifications if any exception happened in the google drive upload
frappe.log_error(message = frappe.get_traceback(),title="Error Uploading to Google Drive")
gdrive.notify(success= False, message = "<p>Please see error log from google drive upload</p><br/>"+frappe.get_traceback())
gdrive.create_log(0)

@frappe.whitelist()
def upload_to_gdrive(file_name: str = None, file: str = None, gdrive_folder: str = None, share_with: str = None):
"""[summary]
Args:
file_name (str): Name of the file
file (str): file
gdrive_folder (str): Google drive folder name
share_with (str): email id to share the file in google drive
Returns:
dict: {
message (str): Brief message indicating the response,
status_code (int): Status code of response.
data (str): The url to the file,
error (str): Any error handled.
}
"""

if not file_name:
return response("Bad Request", 400, None, "file_name required.")

if not file:
return response("Bad Request", 400, None, "file required.")

if not gdrive_folder:
return response("Bad Request", 400, None, "gdrive_folder required.")

if not share_with:
return response("Bad Request", 400, None, "share_with required.")

if not isinstance(file_name, str):
return response("Bad Request", 400, None, "file_name must be of type str")

if not isinstance(gdrive_folder, str):
return response("Bad Request", 400, None, "gdrive_folder must be of type str")

try:
file_ext = "." + file_name.split(".")[-1]
content = base64.b64decode(file)
file_name = hashlib.md5((file_name + str(datetime.datetime.now())).encode('utf-8')).hexdigest() + file_ext
Path(frappe.utils.cstr(frappe.local.site)+f"/private/files/gdrive_upload").mkdir(parents=True, exist_ok=True)
file_path = frappe.utils.cstr(frappe.local.site)+f"/private/files/gdrive_upload/{filename}"
file_ = upload_file_to_db(doc, "attachments", filename, file_path, content, is_private=True)

gdrive_upload = frappe.new_doc('GDrive Upload')
gdrive_upload.file_folder_name = gdrive_folder
gdrive_upload.share_with = share_with
gdrive_upload.share_file_only = True
gdrive_upload.file = file_.file_url
gdrive_upload.save(ignore_permissions=True)
gdrive_upload.upload_to_google_drive()
return response("Success", 201, gdrive_upload.google_drive_link)

except Exception as error:
frappe.log_error(error, 'GDrive Upload API')
return response("Internal Server Error", 500, None, error)
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
from frappe.tests.utils import FrappeTestCase


class TestBackupLog(FrappeTestCase):
class TestGDriveUpload(FrappeTestCase):
pass
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2023, ONE FM and contributors
// For license information, please see license.txt

frappe.ui.form.on('Backup Log', {
frappe.ui.form.on('Google Drive Log', {
// refresh: function(frm) {

// }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"modified": "2023-10-15 17:49:33.678878",
"modified_by": "Administrator",
"module": "One Backup Manager",
"name": "Backup Log",
"name": "Google Drive Log",
"naming_rule": "By \"Naming Series\" field",
"owner": "Administrator",
"permissions": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
# import frappe
from frappe.model.document import Document

class BackupLog(Document):
class GoogleDriveLog(Document):
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) 2023, ONE FM and Contributors
# See license.txt

# import frappe
from frappe.tests.utils import FrappeTestCase


class TestGoogleDriveLog(FrappeTestCase):
pass
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,38 @@

frappe.ui.form.on('One Backup Settings', {
refresh: function(frm) {
frm.add_custom_button("Backup to Google Drive",function(){
frappe.confirm("Are you sure you want to send the latest backup to Google Drive?",()=>{
frappe.call({
method:"one_backup_manager.one_backup_manager.doctype.one_backup_settings.one_backup_settings.create_backup",
callback:function(r){
frappe.show_alert("Backup to Google Drive Initiated")
}
})
},
()=>{
;
})

})
if(frm.doc.enable_google_backups){
frm.events.create_backup_button(frm)
}
},
enable_google_backups: function(frm) {
if(!frm.doc.enable_google_backups){
frm.set_value('enable_auto_backup_to_google_drive', false);
}
},
create_backup_button: function(frm) {
frm.add_custom_button(__("Backup to Google Drive"),
function(){
if(frm.is_dirty()){
frappe.throw(__('Please Save the Document and Continue!'))
}
else{
frappe.confirm(
"Are you sure you want to send the latest backup to Google Drive?",
()=>{
frappe.call({
method:"one_backup_manager.one_backup_manager.doctype.one_backup_settings.one_backup_settings.create_backup",
callback:function(r){
frappe.show_alert("Backup to Google Drive Initiated")
}
});
},
()=>{
// No
}
);
}
}
);
}
});
Loading