# -*- coding: utf-8 -*-
# Copyright (c) 2017, Frappe Technologies and contributors
# For license information, please see license.txt

from __future__ import unicode_literals
import frappe
from frappe import _
from frappe.model.document import Document
from frappe.integrations.doctype.gsuite_settings.gsuite_settings import run_gsuite_script

class GSuiteTemplates(Document):
	pass

@frappe.whitelist()
def create_gsuite_doc(doctype, docname, gs_template=None):
	templ = frappe.get_doc('GSuite Templates', gs_template)
	doc = frappe.get_doc(doctype, docname)

	if not doc.has_permission("read"):
		raise frappe.PermissionError

	json_data = doc.as_dict()
	filename = templ.document_name.format(**json_data)

	r = run_gsuite_script('new', filename, templ.template_id, templ.destination_id, json_data)

	_file = frappe.get_doc({
		"doctype": "File",
		"file_url": r['url'],
		"file_name": filename,
		"attached_to_doctype": doctype,
		"attached_to_name": docname,
		"attached_to_field": True,
		"folder": "Home/Attachments"})
	_file.save()

	comment = frappe.get_doc(doctype, docname).add_comment("Attachment",
		_("added {0}").format("<a href='{file_url}' target='_blank'>{file_name}</a>{icon}".format(**{
			"icon": ' <i class="fa fa-lock text-warning"></i>' if filedata.is_private else "",
			"file_url": filedata.file_url.replace("#", "%23") if filedata.file_name else filedata.file_url,
			"file_name": filedata.file_name or filedata.file_url
		})))

	return {
		"name": filedata.name,
		"file_name": filedata.file_name,
		"file_url": filedata.file_url,
		"is_private": filedata.is_private,
		"comment": comment.as_dict() if comment else {}
		}

@@ -703,16 +703,23 @@
 ata)%0A%0A%09r
+esponse
  = run_g
@@ -863,16 +863,23 @@
 _url%22: r
+esponse
 %5B'url'%5D,
@@ -1269,24 +1269,21 @@
 /i%3E' if 
+_
 file
-data
 .is_priv
@@ -1306,32 +1306,29 @@
 %22file_url%22: 
+_
 file
-data
 .file_url.re
@@ -1348,24 +1348,21 @@
 23%22) if 
+_
 file
-data
 .file_na
@@ -1369,24 +1369,21 @@
 me else 
+_
 file
-data
 .file_ur
@@ -1397,32 +1397,29 @@
 file_name%22: 
+_
 file
-data
 .file_name o
@@ -1420,24 +1420,21 @@
 name or 
+_
 file
-data
 .file_ur
@@ -1463,24 +1463,21 @@
 %22name%22: 
+_
 file
-data
 .name,%0A%09
@@ -1490,24 +1490,21 @@
 _name%22: 
+_
 file
-data
 .file_na
@@ -1521,24 +1521,21 @@
 e_url%22: 
+_
 file
-data
 .file_ur
@@ -1557,16 +1557,13 @@
 e%22: 
+_
 file
-data
 .is_

complete file-api migration