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

# For license information, please see license.txt


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


class RenameTool(Document):
	pass


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


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

	if not select_doctype:
		select_doctype = influxframework.form_dict.select_doctype

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

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

	return bulk_rename(select_doctype, rows=rows)
