# Copyright (c) 2015, SolutionERP LLC and Contributors
# License: GNU General Public License v3. See license.txt

# For license information, please see license.txt


import xhiveframework
from xhiveframework.model.document import Document
from xhiveframework.model.rename_doc import bulk_rename


class RenameTool(Document):
	pass


@xhiveframework.whitelist()
def get_doctypes():
	return xhiveframework.db.sql_list(
		"""select name from tabDocType
		where allow_rename=1 and module!='Core' order by name"""
	)


@xhiveframework.whitelist()
def upload(select_doctype=None, rows=None):
	from xhiveframework.utils.csvutils import read_csv_content_from_attached_file

	if not select_doctype:
		select_doctype = xhiveframework.form_dict.select_doctype

	if not xhiveframework.has_permission(select_doctype, "write"):
		raise xhiveframework.PermissionError

	rows = read_csv_content_from_attached_file(xhiveframework.get_doc("Rename Tool", "Rename Tool"))

	return bulk_rename(select_doctype, rows=rows)
